【问题标题】:How to write structure members at once in a file using fwrite() function?如何使用 fwrite() 函数一次在文件中写入结构成员?
【发布时间】:2016-07-19 17:54:36
【问题描述】:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

struct student{
    char *name;
    char *addr;
    int age;
   int clas;
 }*stu;

 int main()
 {
    FILE *fp;
    int choice,another;
    size_t recsize;
    size_t length;

   struct student *stu=(struct student *)malloc(sizeof(struct student));
   stu->name=(char *) malloc(sizeof(char)*20);
   stu->addr=(char*)malloc(sizeof(char)*20);

   recsize=sizeof(*stu);


           fp=fopen("student.txt","a+");
            if(fp==NULL)
               {
                 fp=fopen("student.txt","w+");
                 if(fp==NULL)
                  {
                    printf("cannot open the file");
                    exit(1);
                  }  
                }

                do
                 {
                   fseek(fp,1,SEEK_END);
                   printf("Please Enter student Details\n");

                   printf("Student Name: ");
                   scanf("%s",stu->name);

                    printf("Address: ");
                    scanf("%s",stu->addr);

                    printf("Class: ");
                    scanf("%s",&stu->clas);

                    printf("Age: ");
                    scanf("%s",&stu->age);                     

                    fwrite(stu,recsize,1,fp);

                    printf("Add another Enter 1 ?\n");
                    scanf("%d",&another);
                   }while(another==1);
                  fclose(fp);
        free(stu);


   }

我的 C 代码具有 Student 结构。我正在尝试从用户那里获取所有结构成员的值。内存分配给结构和两个成员 *name*addr。当我尝试在文件 Student.txt 中使用 fwrite() 函数写入这些值时,它会在文件中显示像这样的随机输出( ཀའ㌱䔀8䵁ཀའ㈱䔀1䵁 ),它不是以可读的形式。请为我提供使用 fwrite() 函数在文件中写入结构成员的最佳方法。

【问题讨论】:

  • 因为你写的是指针的值而不是它们包含的值。除非您将其更改为静态大小的分配(数组),否则您将无法以这种方式 fwrite 该结构。
  • scanf("%s",&amp;stu-&gt;clas); scanf("%s",&amp;stu-&gt;age); 你正在将字符串写入 int 容器,可能会损坏内存
  • @DavidHoelzer ,是否可以写出包含什么指针?我知道如果 *name 更改为 name[20] 并且 *addr 更改为 addr[20],我的代码将起作用。
  • 在 C 中,当调用任何内存分配函数:(malloc, calloc, realloc) 时,返回值的类型为void*,因此可以分配给任何指针。转换返回值只会使代码混乱,使其更难以理解、调试和维护。建议删除返回值的强制转换。注意:表达式:sizeof(char) 在标准中被定义为 1,任何东西乘以 1 都没有效果,只会使代码混乱。建议删除表达式。
  • 调用fopen()时,打开追加,如果文件不存在,则创建文件,如果失败,打开写入+读取不会解决问题。

标签: c data-structures fwrite file-handling


【解决方案1】:

对于ints,您需要使用%d 而不是%s

printf("Class: ");
scanf("%s",&stu->clas);

printf("Age: ");
scanf("%s",&stu->age);

应该是

printf("Class: ");
scanf("%d",&stu->clas);

printf("Age: ");
scanf("%d",&stu->age);

正如@David Hoelzer 在 cmets 中指出的那样:您正在编写指针的值而不是它们包含的内容,请更改

struct student{
    char *name;
    char *addr;

struct student{
    char name[20];
    char addr[20];

并删除这些行:

stu->name=(char *) malloc(sizeof(char)*20);
stu->addr=(char*) malloc(sizeof(char)*20);

【讨论】:

  • 我更正了,但输出也是随机字符。因为我正在使用指针,所以使用 fwrite 在文件中写入结构成员的最佳方法是什么。
  • fwrite 写入原始字节,如果你想要格式化文本使用 fprintf
  • @EttoreBarattelli 感谢您提供信息。那么我们如何才能以可读格式读取这些原始字节,有什么功能吗?
  • 不确定“读取那些原始字节”是什么意思,如果你想将可读数据输出到文件中,你可以这样做fprintf(fp, "%s %s %d %d\n", stu-&gt;name, std-&gt;addr, stu-&gt;age, stu-&gt;clas);
  • @EttoreBarattelli 你说 fwrite 写入原始字节所以我说的是,是否有任何函数可以读取 fwrite() 写入的 c 中的原始字节。
猜你喜欢
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 2019-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多