如何在 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,"%sn", stock.Word);         
         fprintf(fd,"%sn", stock2.Definition);        
         count=count+1;  
         system("cls");                
    }
    while (count<11);
fclose(fd);
fclose(fw);
return 0;
}

这段代码很好,但是,我想扩展它,以便可以选择只编辑一个选定的行,而不是擦除整个文件并再次写入所有内容。

我所要做的就是使用如何在 C 中写入 txt 文件的特定行?

这不是很有帮助,因为我无法获取答案并将其导入我的代码,无论哪种方式,我所需要的只是可以轻松修复以下错误的东西。

 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,"%sn",t)   //watch closely,no 'n' before %s,This will copy without creating a extra newline in beginning of new file.
            else 
                fprintf(fc,"n%sn",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*),并逐个字符读取它并写入新文件(目标.txt),直到找到(EOF)。但它将停止在特定行,并要求用户输入一些数据,这些数据将代替错误(源文件的错误行)写入新文本文件(目标.txt)。

因此,最后您将获得没有错误的Target.txt文件。打开并检查它。

在这个程序中,我假设我们只想替换第 3 行的单个字符,但您可以更改此操作以将其替换为字符串、浮点数或整数等。

这是输出:-

输入您想要的行号 2 编辑: 3

在第 3 行输入要存储的输入:3

啪...已成功编辑错误。

目标内容.txt:-

1

阿拉伯数字

3

5

6

7

相关内容

  • 没有找到相关文章

最新更新