[英]How do I write to a specific line of file in c?
因此,我有一個正在執行的項目,並且創建了一個程序,該程序允許用戶寫入文件,如下所示:
#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");
do
{
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;
}
這段代碼很好,但是,我想對其進行擴展,以便可以僅編輯一個選定的行,而不是擦除整個文件並再次寫入所有內容。
這不是很有幫助,因為我無法回答問題並將其導入到我的代碼中,無論哪種方式,我所需要的都是可以輕松修復以下錯誤的東西。
1
2
Three
4
5
6
7
8
9
10
系統將自動詢問用戶要編輯的行。
#include <stdio.h>
int main()
{
FILE *fp,*fc;
int lineNum; //stores line number which should be edited.
int count=0; //count number lines in source file.
int ch; //temporary place to store character of source file(one at a time).
int edited=0; //0=false and 1=true
int t; //temporary place to store input which you want to be replaced with 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 Which You 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) is found and that line is still not edited. More Explanation:- If we want to edit 3rd line than we should start writing at the end of 2nd line(hence count==lineNum-1) and edited==0 means that error line is still not edited.
{
printf("\nEnter input to store at line %d:",lineNum);
scanf(" %c",&t); //You can replace this statement with any input operation which you want to replace it with the error line.
if(count==0) //if its the first line to edit..
fprintf(fc,"%s\n",t) //watch closely,no '\n' before %s,This will copy without creating a extra newline in beginning of new file.
else
fprintf(fc,"\n%s\n",t); //stores input at error line in file fc(target.txt) from variable t.
edited=1; //this prevents loop to execute more than once(as error is already edited),so there will be no need to execute this loop till the end of program
while( (ch=fgetc(fp))!=EOF ) //Actually this loop will skips the existing line in source.txt(see below)
{ //we want to skip this line because this is error line.
if(ch=='\n')//this will break when next new line(after error line is skipped) is 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;
}
該程序將打開一個源文件( 可能有錯誤 ,我將其稱為source.txt *),並不斷逐字符讀取並寫入新文件(target.txt),直到找到(EOF)。但是它將停止在特定行,並要求用戶輸入一些數據,這些數據將代替錯誤(源文件中的錯誤行)寫入新文本文件(target.txt)中。
因此,最后您將獲得沒有錯誤的target.txt文件。 打開並檢查它。
在這個程序中,我假設我們只想替換第3行的單個字符,但是您可以更改此操作以將其替換為字符串,浮點數或整數等。
這是輸出:-
輸入您想要的行號2編輯:3
輸入輸入以存儲在第3行:3
聚合...錯誤已成功編輯。
target.txt的內容:-
1個
2
3
4
5
6
7
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.