How to read and overwrite a text file in C?

I have a text.txt text file that reads (for simplicity)

this is line one this is line two this is line three 

Again, for simplicity, I'm just trying to set the first character in each line to "x", so my desired result would be

 xhis is line one xhis is line two xhis is line three 

So, I open the text.txt file and try to rewrite each line with the desired output in the same text file. In a while loop, I set the first character in each line to "x". I also set the string variable to one, because if it's on the first line, I want to rewind to the beginning of the file to overwrite at the beginning, not the end of the file. Then the line grows, so it skips rewinding for the next iteration and should continue to replace the second and third lines. It works great for the first line.

Anyone have any solutions? By the way, I researched this extensively on both stackoverflow and other sites, and no luck. Here my code and my output are also below:

 #include <stdio.h> #include <stdlib.h> #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); } 

Output:

 xhis is line one this is line two xhis is line two e x 
+6
source share
5 answers
 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 } 
+6
source

I always suggest using a different file to execute these kindda decisions.

  • Read the line
  • Put x in the new file in the line and copy the rest of the line.
  • Do it until you get EOF
  • delete old file
  • rename this new file
+2
source

try it

 #include<stdio.h> #include<stdio.h> #include<string.h> 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; } 

I worked on windows

0
source
 // 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<stdio.h> #include<stdio.h> #include<string.h> #include <conio.h> #include<stdlib.h> 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; } 
0
source
 // 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<stdio.h> #include<stdio.h> #include<string.h> //Include appropriate headers #include <conio.h> #include<stdlib.h> 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; } 
0
source

All Articles