【发布时间】:2015-10-11 02:07:57
【问题描述】:
我正在尝试从文本文件中填充数组。该数组位于一个结构中:
struct ISBN
{
long value;
};
struct Author
{
char authorName[60];
};
struct Book
{
char *bookTitle;
struct Author bookAuthor;
struct ISBN bookID;
};
我尝试编写一个填充函数,该函数采用 Book 类型的文件和结构,如下所示:
void fillin (FILE * file, struct Book * bk)
{
bk->bookTitle =(char*) malloc(1000);
size_t n = 0;
int c;
file=fopen("book.txt","r");
while ((c = fgetc(file)) != '\n')
{
bk->bookTitle[n++] = (char) c;
}
bk->bookTitle[n] = '\0';
fscanf(file,"%s", &bk->bookAuthor.authorName);
fscanf(file,"%lld",&bk->bookID.value);
//fscanf(file,"%s", &bk->bookTitle);
}
文件 book.txt 有这个数据:
UNIX Network Programming
W. Richard Stevens
0131411551
问题是,它不能扫描数组,我想从文本文件中填充 bookTitle 和 autherName 数组。
【问题讨论】: