【发布时间】:2014-05-02 03:54:31
【问题描述】:
我是 C 的初学者。 我想写和读一个二进制文件,到目前为止我已经做到了:
我可以将我的结构转换成二进制文件,并且可以读取。
问题一:我只能读取整数,不知何故字符串被打印为垃圾或随机字符。
问题二:如果我运行我的程序,我将一些条目添加到我的二进制文件中,然后我打印所有工作正常的条目(除了问题一),但是在我终止我的程序之后并再次运行,我在尝试读取文件时遇到了分段错误。
请帮帮我,我无法前进。
/* Our structure */
struct rec
{
int max,cost;
char *name;
};
struct rec addNewEntry()
{
//init
char name[256];
int max;
int cost;
//input
printf("Type name: \n");
scanf("%s" , &name) ;
printf("Type guests limit: \n");
scanf("%d", &max);
printf("Type price: \n");
scanf("%d", &cost);
//create record
struct rec record;
record.name = name;
record.max = max;
record.cost = cost;
return record;
}
int main()
{
FILE *ptr_myfile;
//////////////////////////MENU////////////////////////////////
int option=-1;
while(option!=3)
{
printf("\n=== MENU === \n");
printf("\n1. Print all entries");
printf("\n2. Add new entry");
printf("\n3. Exit");
printf("\n");
printf("\nType menu option:");
scanf("%d", &option);
if(option == 1)
{
printf("\n...Printing all entries\n");
int f=open("stadionok.db",O_RDONLY);
if (f<0){ perror("Error at opening the file\n");exit(1);}
struct rec my_record;
while (read(f,&my_record,sizeof(my_record))){ //use write for writing
printf("name: %s \n",my_record.name);
printf("max: %d \n",my_record.max);
printf("cost: %d \n",my_record.cost);
}
close(f);
}
else if(option ==2)
{
printf("\n...Type a new entry\n");
//OPEN AND CHECK
ptr_myfile=fopen("stadionok.db","a");
if (!ptr_myfile)
{
printf("Unable to open file!");
return 1;
}
//TYPE A NEW ENTRY
struct rec new_stad = addNewEntry();
//WRITE TO FILE
fwrite(&new_stad, sizeof(struct rec), 1, ptr_myfile);
//CLOSE
fclose(ptr_myfile);
printf("Done.\n");
}
}
return 0;
}
编辑:
我按照您的建议进行了修改,现在我得到了: 错误:分配中的类型不兼容
在:
char name[256];
//input
printf("Type name: \n");
scanf("%s" , &name) ;
struct rec record;
record.name = name; //HERE
【问题讨论】:
-
name 已经是一个地址,所以不需要在它前面加上 &。你不能像这样将一个数组分配给另一个数组。使用 memcpy 或 strncpy 复制数组的内容。
-
谢谢,效果很好。
标签: c