【发布时间】:2015-09-18 16:42:14
【问题描述】:
该程序的目的是读取一个文本文件,其中包含 55 位作者和书名的列表。列表格式为(作者姓名、书名)。我可以使用 malloc、strlen、strtok 和 strcopy。到目前为止,我让程序读出作者的姓名,但我一直坚持如何让程序读取书名。如何让程序从文本文件中读取书名?我知道这段代码有错误,所以请善待。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void loadBookName(char* filename, char* authorName[55], char* bookName[55]);
int main(int argc, char* argv[])
{
//Create two arrays each with length 55
char* authorName[55];
char* bookName[55];
//Ask the user for the name of the file
char fileName[30];
//Insert your code here
printf("Please enter the name of the file\n");
scanf("%s", fileName);
//Call the method loadBookName
loadBookName(fileName, authorName, bookName);
return 0;
//Print the two arrays to test if the two arrays were correctly loaded with the data
int i = 0;
printf("%-30s%-40s\n", "Author", "Book");
for (i = 0; i < 55; i++) {
printf("%-30s%-40s\n", authorName[i], bookName[i]);
}
}
/*
loadBookName method
This method is responsible for:
1. Take a file containing a book name and the author name as input
2. Open the file
3. Read the information in the file and store it in two arrays: authorName, bookName
4. Return the two arrays to the main method.
*/
void loadBookName(char* filename, char* authorName[55], char* bookName[55])
{
int i;
char string_array[80];
const char comma[2] = ",";
//Open the file
FILE *fp;
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Failed to open file\n");
exit(1);
}
for (i=0; i<55; i++)
{
fgets(string_array, 80, fp);
authorName[i] = strtok(string_array, comma);
printf("%s\n", *authorName);
}
//Close the file
fclose(fp);
}
当我在终端中运行程序时,它要求我输入文件名(books.txt)。然后当我输入文件名时,程序会打印出 55 个作者的列表。
【问题讨论】:
-
您能添加一个数据样本吗?
-
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建最小、完整和可验证的示例。
-
authorName[i] = strtok(string_array, comma);:string_array(局部变量)的一部分地址设置为authorName[i]。你需要strdup(分配和复制)。 -
您可能想检查一下,我认为这个确切的问题在过去几天已经得到了回答。见Memory allocation for char array
-
main() 函数,在正文中大约 10 行,具有语句:
return 0;,因此以下行将永远不会被执行。请始终缩进代码。 (切勿使用制表符进行缩进,因为每个文字处理器/编辑器的制表位/制表符宽度设置不同。)建议在每个左大括号“{”之后缩进 4 个空格,并在每个右大括号“}”之前不缩进。建议的 4 个空格即使在可变宽度字体上也是可见的,并且在代码嵌套良好时不会占用整个页面宽度
标签: c