string - How do I write to a specific line of file in c? -
so have project doing , have created program allows user write file, shown below:
#include <stdio.h> #include <stdlib.h> file *fd; file *fw; struct store { char word[512]; char nword[512]; } stock; struct store2 { char definition[512]; } stock2; char done='y'; int count=1; int c=0; int d=0; int main(void) { fw=fopen("test z w.txt","w"); fd=fopen("test z d.txt","w"); { printf("word %d: ",count); gets(stock.word); while((c= getchar()) != '\n' && c != eof); printf("definition %d: ",count); gets(stock2.definition); while((c= getchar()) != '\n' && c != eof); fprintf(fw,"%s\n", stock.word); fprintf(fd,"%s\n", stock2.definition); count=count+1; system("cls"); } while (count<11); fclose(fd); fclose(fw); return 0; }
this code fine, however, expand there option edit 1 chosen line, rather wiping entire file , writing of contents again.
all have work on using how write specific line of txt file in c?
which wasn't helpful since couldn't take answer , import code, either way need error such 1 below, can fixed easily.
1 2 3 4 5 6 7 8 9 10
where user automatically asked line want edit.
#include <stdio.h> int main() { file *fp,*fc; int linenum; //stores line number should edited. int count=0; //count number lines in source file. int ch; //temporary place store character of source file(one @ time). int edited=0; //0=false , 1=true int t; //temporary place store input want replaced error in text file. fp=fopen("source.txt","r"); fc=fopen("target.txt","w"); if(fp==null||fc==null) { printf("\nerror...cannot open/create files"); return 1; } printf("\nenter line number want 2 edit: "); scanf("%d",&linenum); while((ch=fgetc(fp))!=eof) { if(ch=='\n') //counts number of lines count++; if(count==linenum-1 && edited==0) //if specific line(error line) found , line still not edited. more explanation:- if want edit 3rd line should start writing @ end of 2nd line(hence count==linenum-1) , edited==0 means error line still not edited. { printf("\nenter input store @ line %d:",linenum); scanf(" %c",&t); //you can replace statement input operation want replace error line. if(count==0) //if first line edit.. fprintf(fc,"%s\n",t) //watch closely,no '\n' before %s,this copy without creating newline in beginning of new file. else fprintf(fc,"\n%s\n",t); //stores input @ error line in file fc(target.txt) variable t. edited=1; //this prevents loop execute more once(as error edited),so there no need execute loop till end of program while( (ch=fgetc(fp))!=eof ) //actually loop skips existing line in source.txt(see below) { //we want skip line because error line. if(ch=='\n')//this break when next new line(after error line skipped) found. break; } } else fprintf(fc,"%c",ch); } fclose(fp); fclose(fc); if(edited==1) printf("\ncongrates...error edited successfully."); else printf("\nline not found"); return 0; }
this program open source file (which may have error,i called source.txt*) , keeps reading character character , write new file (target.txt) untill (eof) found.but stop @ specific line , ask user input data written in place of error(error line source file) in new text file( target.txt).
so @ end target.txt file free errors. open , check it.
in program have assumed want replace single character @ line 3.but can change operation replace string,float or integers etc.
here output:-
enter line number want 2 edit: 3
enter input store @ line 3: 3
congrates...errors edited successfully.
contents of target.txt:-
1
2
3
4
5
6
7
Comments
Post a Comment