写入文件时出现 malloc 问题

发布于 2024-10-06 01:44:25 字数 772 浏览 0 评论 0原文

每当我将数据写入文本文件时,就会出现垃圾数据...为什么会这样? 这是我的代码...谢谢

int main(void)
{
   unsigned int option = 0;
   int i = 0;
   }
   getch();
   while(option != 5){
      option = display(); 
      switch(option){
          case 5: save();
                  break;        
      }
       for(i = 0; i < recordCtr; i++){
         free(array[i]);}  
   } 
}  

save(){
     FILE *stream = NULL;
     stream = fopen("student.txt", "wt");
     printf("\nSaving the student list directory. Wait a moment please...");
             int i =0;
                 for (i=0; i<3; i++){
                    fprintf(stream, "%5s %30s %5s\n", array[i]->studentID, array[i]->name, array[i]->course);
                 }
     fclose(stream);                     
}

A garbage data appears whenever i write the data in the text file... Why is it like that?
Here's my code... Thanks

int main(void)
{
   unsigned int option = 0;
   int i = 0;
   }
   getch();
   while(option != 5){
      option = display(); 
      switch(option){
          case 5: save();
                  break;        
      }
       for(i = 0; i < recordCtr; i++){
         free(array[i]);}  
   } 
}  

save(){
     FILE *stream = NULL;
     stream = fopen("student.txt", "wt");
     printf("\nSaving the student list directory. Wait a moment please...");
             int i =0;
                 for (i=0; i<3; i++){
                    fprintf(stream, "%5s %30s %5s\n", array[i]->studentID, array[i]->name, array[i]->course);
                 }
     fclose(stream);                     
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执笏见 2024-10-13 01:44:25

有一些错误。

  1. recordCtr 始终递增。如果我选择添加 2 次,则填充 array[0]、array[1] 和 array[2]。但是当你释放内存时,你就释放了 recordCtr 值。在这种情况下,您恰好释放了最多 6 条学生记录。这可能是一场灾难。
  2. 一旦切换结束,您就可以释放学生记录内存。如果用户选择退出,则调用 save() 来尝试保存已释放的学生记录。从释放的内存中读取数据是个坏主意。

什么时候看到垃圾数据。我的意思是在什么输入下?

There are some bugs.

  1. recordCtr is always incremented. If I chose to add 2 times then you fill array[0], array[1] and array[2]. But when you are freeing memory, you are freeing up to recordCtr value. In this case you happen to free up to 6 student records. It could be disaster.
  2. As soon as the switch ends you are freeing student records memory. If user choose to exit then save() is caled which tries to save student records which are freed already. Its bad idea to read from freed memory.

When do you see garbage data. I mean under what Input?

逆流 2024-10-13 01:44:25

您从未为 struct Student *array[MAX]; 分配内存;

我认为这可能会覆盖您的数据?

strcpy(array[0]->studentID, dummy);

studentID 是 char[5],dummy 是 char[30]。自从使用 C for m 以来已经有一段时间了,但您可能会破坏其他数据。

You never allocate memory for struct student *array[MAX];

I think this is overwriting your data maybe?

strcpy(array[0]->studentID, dummy);

studentID is a char[5] and dummy is a char[30]. It's been a while since using C for m, but you could be smashing your other data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文