1

所以我遇到了使用 Visual C++ 编辑文件处理的问题。我现在拥有的代码将显示文件内部的内容,在编辑它时,你必须重新输入文件的所有包含内容,包括你编辑的内​​容。但是我的教授说它不是编辑而是覆盖原始文件..所以我想知道如何在不重写整个语句的情况下编辑文件的包含。

   void edit()
    {
        char choice;
        char newdata[1000];
        char previousdata[1000];
        char filename [1000];
        int count =0;
        printf("Example: D:/sample.txt");
        printf("Enter filename:");
        scanf("%s",&filename);
        fp=fopen(filename,"r");

        if(fp==NULL)
        {
            printf("Unable to open....\n");
        }
        else
        {
            printf("Success in opening file...\n\n");
            int c;

            c = getc(fp);
            while(c!=EOF)
            {
                printf("%c",c);
                previousdata[count] = c;
                c = getc(fp);
                count++;
            }

        }
        fclose(fp);

        printf("\nPlease Type the new statement");
        printf("\nInput Statement:\n");
        scanf("%s",&newdata);


        printf("\nDo you want to save the changes?");
        printf("\n Press[Y] YES \t\t Press[N] NO");
        printf("\n Your choice:");
        scanf("%s",&choice);

        if(choice == 'Y'|| choice == 'y')
        {
            fp =fopen(filename,"w");
            fprintf(fp,"%s",newdata);
            fclose(fp);
            main();
        }
        else if(choice == 'N'||choice == 'n')
        {
            system("CLS");
            main();
        }       

    }
4

2 回答 2

1

从您的程序代码看来,您正在附加文本而不是编辑。因此,如果您可以以附加模式(a+)打开文件,对您来说将很容易。

为了使您的程序工作,您需要在将新数据写入文件之前将旧数据附加到旧数据中。

printf("\nPlease Type the new statement");
    int c;            
    printf("\nInput Statement:\n");
    c=getch();
    while(c != '`'){  //use ` to quit writing.
        count++;    
        olddata[count]=c; //extending the olddata array with new data.
        }
    printf("\nDo you want to save the changes?");
    printf("\n Press[Y] YES \t\t Press[N] NO");
    printf("\n Your choice:");
    scanf("%s",&choice);
    if(choice == 'Y'|| choice == 'y')
    {
        fp =fopen(filename,"w");
        fprintf(fp,"%s",olddata);
        fclose(fp);
        main(); //this is not good. use return instead.
    }

如果您想避免在阵列中备份当前文件数据,则需要以a+模式打开文件。

注意:这不是一个编辑程序,它用于在末尾附加文本。如果您想要真正的编辑,请查看@Basile 答案。

另一个例子

int main()
{
    FILE* f=fopen("main.c","a+");
    int c;
    c=getc(f);
    while(c!=EOF){
        putchar(c);
        c=getc(f);
    }
    prinf("\n Start entering your new content \n");
    c=getchar();
    while (c != '`'){
        fputc(c,f);
        c=getchar();
    }
    fclose(f);
    return 0;
}
于 2013-10-12T08:07:04.153 回答
1

如果您想要一个文件的真实版本,您可能希望将所有文件内容保存在内存中。这对于中小型文件(即最近的笔记本电脑上小于 1 或 2 GB 的文件)是合理的。

您可能希望保留一个行指针数组。数组本身以及指向其中一行的每个指针都将被堆分配。

您可以拥有数组、它的使用长度和分配的大小:

char** linarr; // heap-allocated array of lines
unsigned nblines; // used length, i.e. number of lines
unsigned size;  // allocated size of linarr;

在初始化时,您预先分配它,例如

#define INITIAL_SIZE 100
linarr = calloc (INITIAL_SIZE, sizeof(char*));
if (!linarr)  { perror("calloc"); exit(EXIT_FAILURE); };
size = INITIAL_SIZE;
nblines = 0;

然后你需要一个循环来读取每一行,在需要时增加数组

FILE* inputf = fopen(filename, "rt");
// code from https://stackoverflow.com/a/19331746/841108
if (!inputf) { perror("fopen"); exit(EXIT_FAILURE); };
while (!feof (inputf)) {
   // grow the array when full
   if (nblines >= size) { 
       unsigned newsize = 5*size/4 + 10;
       char**newarr = calloc(newsize, sizeof(char*));
       if (!newarr) { perror("calloc newarr"); exit (EXIT_FAILURE); };
       for (unsigned ix=0; ix<nblines; ix++) 
         newarr[ix] = linarr[ix];
       free (linarr);
       linarr = newarr;
       size = newsize;
   };
   char* curline = NULL;
   size_t curlinesize = 0;
   if (getline(&curline, &curlinesize, inputf)<0)
      { perror("getline"); exit (EXIT_FAILURE); };
   linarr[nblines] = curline;
   nblines++;
};
fclose (inputf); 
inputf = NULL;

最后,您需要适当地插入这些行(在成长linarr、更新nblines等内部)并写入新文件(或其新内容)。我把它留给你。

顺便说一句,如果你用 C++(而不是 C)编码,你可以使用std::vector<std::string>

如果您只想追加到现有文件,而不是编辑它(即在中间更改它),您可以按照Arpit的评论,只需使用追加模式"a+"fopen. 但是您的问题提到了编辑(不附加到)文件,这通常需要将所有文件内容保存在内存中,并以合适的数据结构组织。

PS:我对 Windows 一无所知,我尝试按照标准编写代码,尤其是 Posix 和 C99

于 2013-10-12T07:37:35.953 回答