c - 执行结束时修改文件内容

标签 c file-io

我正在开发一个实用程序,它可以扫描多个文本文件,提取相关文本并将其转储到另一个文本文件中。

int main()
{
    // file I/O code
    node *temp1;
   char script[255],segName[255],ch;
   char *dev;
   FILE *fp;
   int i=0;
   void removeSpace();

   dev=getenv("DEVSET");
   devset = atoi(dev);
   //clearing the file before starting
   fp=fopen("output.txt","w");
   fclose(fp);

   memset(script,'\0',sizeof(script));
   strcpy(script,"main"); // start with main
   head=createNode(script); // head is a global node*
   head->prev=NULL;
   head->next=NULL;
   temp1 = head;

   while(temp1 !=NULL)
   {


       read(temp1, script);// reads the text file corresponding to temp1 and   returns relevant data

       memset(segName,'\0',sizeof(segName));
       if (strcmp((temp1->name),"main"))
       {
           //if not main, start on a new line
           segName[0]='\n';
       }


       if (strcmp(script,"EOF")== 0 || strcmp(script,"")== 0 )
       {
           // end of file or Call not found
           temp1 = pop(head);

       }
       else
       {
           if (strcmp(script,"exit_flow") == 0)
           {

               fp=fopen("output.txt","a");

               fprintf(fp,"//FNF");
               fclose(fp);

               temp1=pop(head);

           }
           else
           {
              //Call found

              for(i=0;i<script_index;i++) // script index is a global int
              {
                  strcat(segName,"-"); // no of dashes indicate depth of the call
              }

              strcat(segName,"<");
              i=strlen(segName);
              segName[i]=call_type;
              strcat(segName,">");
              strcat(segName,(script));

              fp=fopen("output.txt","a");
              fprintf(fp,segName);
              fclose(fp);
              script_index++;
              temp1=createNode(script);
              push(head,temp1);
           }
       }
   }// end of while

   // Adjustment to remove extra spaces from the file
   removeSpace();
   fclose(fp);
    if(!devset)
        printf("\nExecution complete");

    printf("\nResult dumped to \"output.txt\"");

    printf("\nOpen the output file? ");
    fflush(stdin);

    ch=getchar();
    if (ch == 'y' || ch == 'Y')
    {
        system("notepad output.txt");// Text file displayed has correct data here
    }

    return 0;
}

上面提到的是main()部分。

尽管看起来不太可能,但在执行“system”语句后文件的内容会被修改。 尽管我在执行“system”之前关闭了该文件,但一个字符串(不完全是随机的)被附加到文本文件中。

Environment - windows

IDE- Code::Blocks

Compiler - MinGW

这是因为我没有刷新一些缓冲区吗?

最佳答案

您的示例不完整,但您的程序可能是(a)打开一个文件进行写入,(b)向其中写入一些数据,(c)调用记事本让用户查看文件,以及(d)退出。

但是:当程序退出时,所有未写入的数据都会被刷新,所有打开的文件都会被关闭。很可能正是这个最终的隐式刷新/关闭附加了您所询问的额外数据。

在调用system之前尝试调用fflush(outputfile)fclose(outputfile)

关于c - 执行结束时修改文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30964008/

相关文章:

c - c程序中的sudo权限问题

c++ - 为什么 ios::app (append) 在 C++ 中不起作用?

c - 从 C 中的文本文件中读取 int 值

c - 修改链表中结构体的字段

c - 防止在 C 中分配类型

c - 在 strcpy 函数中使用指针作为参数。尝试理解书本上的代码

C++按行读取逗号分隔文件[其中一个部分为mm/dd/yyyy],安全地放入结构中

c - C 中指针的大小是否有所不同?

C# StreamReader, "ReadLine"用于自定义分隔符

java - 帮助查找控制台常量