c - 编辑文件处理 Visual C++

标签 c file-io

<分区>

所以我在使用 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();
        }       

    }

最佳答案

如果您想要一个文件的真实版本,您可能希望将所有文件内容保存在内存中。这对于中小型文件(即最近的笔记本电脑上小于 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

关于c - 编辑文件处理 Visual C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19331612/

相关文章:

objective-c - C 和 Objective-C 中指针的区别

将 char 转换为 short

Android - 如何保存其他应用程序可读的文件?

C 奇怪的错误与 strcpy 甚至更奇怪的解决方案

c++ - 我们可以在 main 的参数中添加 CV 限定符吗?

c - 如何在 C 中将 float 放入 char 数组中?

c++ - 用c++写一个循环文件

c# - 将我的临时文件写入何处?

matlab - 大量包含对象的小 .mat 文件的高效磁盘访问

algorithm - 从大型文本文件快速形成矩阵