如何在C中读取和覆盖文本文件?
我有一个文本文件text.txt读取(为简单起见)
this is line one this is line two this is line three
为了简单起见,我只是试图将每行中的第一个字符设置为’x’,所以我想要的结果是
xhis is line one xhis is line two xhis is line three
所以我打开text.txt文件并尝试用所需的输出覆盖每一行到同一文本文件。 在while循环中,我将每行中的第一个字符设置为“x”。 我还将变量“line”设置为等于1,因为如果它在第一行,我想回放到文件的开头,以便在开始时而不是在文件的末尾进行覆盖。 然后增加行,以便在下一次迭代时跳过倒带,并且应该继续覆盖第2行和第3行。 它适用于第一线。
有人有任何解决方案吗? 顺便说一句,我已经在stackoverflow和其他网站上进行了广泛的研究,但没有运气。 这是我的代码,我的输出也在下面:
#include #include #define MAX 500 int main() { char *buffer = malloc(sizeof(char) * MAX); FILE *fp = fopen("text.txt", "r+"); int line = 1; while (fgets(buffer, 500, fp) != NULL) { buffer[0] = 'x'; if (line == 1) { rewind(fp); fprintf(fp, "%s", buffer); } else { fprintf(fp, "%s", buffer); } line++; } free(buffer); fclose(fp); }
输出:
xhis is line one this is line two xhis is line two e x
long pos = ftell(fp);//Save the current position while (fgets(buffer, 500, fp) != NULL) { buffer[0] = 'x'; fseek(fp, pos, SEEK_SET);//move to beginning of line fprintf(fp, "%s", buffer); fflush(fp); pos = ftell(fp);//Save the current position }
我总是建议使用另一个文件做这个kindda解决方案。
- 阅读该行
- 将x放在一行中的新文件中,然后复制该行的其余部分。
- 这样做直到你得到EOF
- 删除旧文件
- 重命名这个新文件
试试这个
#include #include #include int main() { char buffer[500],read[50][50]; FILE *fp=fopen("text.txt","r+"); int line =1; while(fgets(buffer,500,fp)!=NULL){ buffer[0]='x'; printf("\n%d ",line); puts(buffer); strcat(read[line-1],(const char*)buffer); line++; } fclose(fp); FILE *fp1=fopen("text.txt","w"); rewind(fp1); fprintf(fp1,"%s",read); return 0; }
我在Windows上解决了这个问题
// file_overwrite.cpp : main project file. // File opens and write y value to a file // again reads same file and re-writes y value to a file #include "stdafx.h" using namespace System; #include #include #include #include #include int main(int argc, char *argv[]) { int x = 19530; FILE *fp1 = fopen("D:\\Data\\BUFF.txt","w+"); if(fp1 == NULL) printf("File not opening \n"); int y=x; fprintf(fp1, "%d \n", y); fclose(fp1); printf("\n file -> open -> write y value and close"); freopen("D:\\Data\\BUFF.txt", "w", fp1); rewind(fp1); y=100; fprintf(fp1, "%d \n", y); printf("\n file -> Reopen -> rewind write y values and close"); fclose(fp1); getch(); return 0; }
// overwrite_file.cpp // File opens and write y value to a file // again reads same file and re-writes y value to a file #include "stdafx.h" using namespace System; #include #include #include //Include appropriate headers #include #include int main(int argc, char *argv[]) { int x = 19530; // Give any value in the limit FILE *fp1 = fopen("D:\\Data\\BUFF.txt","w+"); // open file to write if(fp1 == NULL) // if the file pointer encounters a null, it may not open neither overwrite printf("File not opening \n"); int y=x; fprintf(fp1, "%d \n", y); //print y fclose(fp1); printf("\n file -> open -> write y value and close"); // close the file after writing the value of y freopen("D:\\Data\\BUFF.txt", "w", fp1); //reopen and rewind file rewind(fp1); y=100; // this value of y given within the limits gets printed on the .exe console fprintf(fp1, "%d \n", y); printf("\n file -> Reopen -> rewind write y values and close"); // rewind write values and close fclose(fp1); getch(); return 0; }