- fseek
1 //文件路径 2 char path[150] = "1.txt"; 3 4 5 //FILE *pf = fopen(path, "a+");//尾部添加,文件指针在尾部 6 //FILE *pf = fopen(path, "w+");//文件指针在头部,清空内容 7 FILE *pf = fopen(path, "r+");//文件指针在头部,不清空内容 8 fseek(pf, 0, SEEK_END); 9 fputs("0000000", pf); 10 fflush(pf);//刷新文件 11 12 13 //插入 14 int length = 10; 15 for (int i = 0; i < 10;i++) 16 { 17 fseek(pf, -7-i-1, SEEK_END); 18 int ch = fgetc(pf); 19 //#define SEEK_CUR 1 当前 20 //#define SEEK_END 2 结束 21 //#define SEEK_SET 0 开头 22 23 fseek(pf, -i-1, SEEK_END); 24 fputc(ch,pf); 25 fflush(pf);//刷新文件 26 } 27 fseek(pf, -17, SEEK_END);//a+ 文件指针移动无效 28 //r+,文件覆盖 29 fputs("abcdefg", pf); 30 fflush(pf);//刷新文件 31 32 fclose(pf);
- 以"rb+"的形式打开文件指针在头部,不清空内容
1 char path[150] = "1.txt"; 2 FILE *pf = fopen(path, "rb+");//文件指针在头部,不清空内容 3 4 fseek(pf, -20, SEEK_END);//a+ 文件指针移动无效 5 fputc('8', pf); 6 fputs("123545", pf); 7 8 rewind(pf); 9 10 char ch; 11 while ((ch=fgetc(pf))!=EOF) 12 { 13 putchar(ch); 14 } 15 16 fclose(pf);
- 宽字符读取文件
1 //设定中文 2 setlocale(LC_ALL, "zh-CN"); 3 wchar_t path[150] = L"Z:\I\百度内部员工联系方式.txt"; 4 //文件指针在头部,不清空内容 5 FILE *pf = _wfopen(path, L"r"); 6 7 if (pf==NULL) 8 { 9 printf("error"); 10 } 11 wchar_t wstr[1128] = { 0 }; 12 wchar_t *p = fgetws(wstr, 1128, pf); 13 wprintf(L"-%s- ", wstr); 14 p = fgetws(wstr, 1128, pf); 15 while (p!=NULL) 16 { 17 //输出 18 wprintf(L"-%s- ", wstr); 19 p = fgetws(wstr, 1128, pf); 20 } 21 22 fclose(pf);
- 常用宽字符文件函数
1 //fgetc fgetwc 2 //fputc fputwc 3 //fgets fgetws 4 //fputs fputws 5 //fprintf fwprintf 6 //fscanf fwscanf 7 8 //fread ,fwrite
- fgetwc fputwc putwchar用法示例
1 setlocale(LC_ALL, "zh-CN"); 2 FILE *pfr = _wfopen(L"QQ-腾讯通讯录.txt", L"rb"); 3 FILE *pfw = _wfopen(L"QQc.txt", L"wb"); 4 while (!feof(pfr)) 5 { 6 wchar_t ch = fgetwc(pfr);//获取宽字符 7 fputwc(ch, pfw); 8 putwchar(ch);//显示宽字符 9 10 } 11 fclose(pfr); 12 fclose(pfw);
- fgetws fputws wprintf用法示例
1 setlocale(LC_ALL, "zh-CN"); 2 FILE *pfr = _wfopen(L"QQ-腾讯通讯录.txt", L"rb"); 3 FILE *pfw = _wfopen(L"QQ.txt", L"wb"); 4 while (!feof(pfr)) 5 { 6 wchar_t wstr[256] = { 0 }; 7 fgetws(wstr, 256, pfr); 8 wprintf(L"%ls",wstr); 9 fputws(wstr, pfw);//宽字符写入 10 11 } 12 fclose(pfr); 13 fclose(pfw);
- fwscanf fwprintf用法示例
1 setlocale(LC_ALL, "zh-CN"); 2 wchar_t wstr[100] = { 0 }; 3 fwscanf(stdin, L"%s", wstr); 4 fwprintf(stdout, L"%s%d%s", L"你好", 100,wstr);