编辑:事实证明 strtok() 成功解决了我的问题。谢谢 Barmar。
我对 C 编程还是个新手,现在我已经花了几个小时试图完成一项大学作业。我们应该做的是从包含食品可用性的文本文件中获取输入,然后根据它们的可用性输出这些项目。本质上,该程序应该从用户输入中获取文件名,读取文件,将信息存储到四个预定义的字符串数组中,然后以以下格式输出可用的食物:
名称(类别)——描述。
文本文件的每一行都按以下顺序排列:类别、名称、描述和可用性。类别、名称、描述和可用性由 ' ' 字符分隔,我们应该省略它。
我一直遇到的问题是我似乎无法弄清楚如何让输出正确打印。当我运行该程序时,输出结果如下:
b'Ham sandwich\x04 (Sandwiches\xec\x9dO\x7f) -- Classic ham sandwich\nCaesar salad\x10 (Salads\xff\xff) -- Chunks of romaine heart lettuce dressed with lemon juice\nWater\x96\x19 (Beverages) -- 16oz bottled water\nBeef tacos\x0f\x9eO\x7f (Mexican food) -- Ground beef in freshly made tortillas\n'
我相信混乱是预定义的字符串之一,当我打印它时无法终止,几乎可以肯定是因为它缺少 ' ' 字符。我还认为,这个问题源于我获取每一行文本并将每个类别、名称、描述和可用性放入各自数组的方式中的一些问题。真正的问题是什么,以及如何解决它,是我一直无法弄清楚的事情。我已经通过电子邮件向我的教授寻求帮助,他们说他们会尽快回复我,但我还没有得到他们的回复。
综上所述,这是我当前的作业代码:
#include <stdio.h>
#include <string.h>
int main(void) {
const int MAX_LINES = 25; //Maximum number of lines in the input text file
const int MAX_STRING_LENGTH = 100; //Maximum number of characters in each column of the input text file
const int MAX_LINE_LENGTH = 200; //Maximum number of characters in each line of the input text file
// Declare 4 string arrays to store the 4 columns from the input text file
char column1[MAX_LINES][MAX_STRING_LENGTH];
char column2[MAX_LINES][MAX_STRING_LENGTH];
char column3[MAX_LINES][MAX_STRING_LENGTH];
char column4[MAX_LINES][MAX_STRING_LENGTH];
//get name of file, then open it
FILE* inputFile;
char fileName[MAX_STRING_LENGTH];
scanf("%s", fileName);
inputFile = fopen(fileName, "r");
//string variable to take in each line
//int variable to keep track of / count each line for the column arrays
char fileLine[MAX_LINE_LENGTH];
int countLine = 0;
//when skipping the '\t' character, remember that there is also a null character
//being skipped over in the array being copied too.
while(!feof(inputFile)){
fgets(fileLine, MAX_LINE_LENGTH, inputFile);
if(feof(inputFile)){ //I have found that if I don't check for end-of-file again,
break; //it would duplicate the last line.
}
int i = 0;
int k = 0;
int tabCount = 0; //tabCount to account for skipping over \t characters
//while loop to take file input and put it in respective columns
while( i < strlen(fileLine) ){
if( fileLine[i] == '\t' ){
k = 0;
i++;
tabCount++;
}
///////////////////////////////////////////////
if( tabCount == 0 ){
column1[countLine][k-tabCount] = fileLine[i];
} else if( tabCount == 1 ){
if( !(k >= 1) ){
k = 1;
}
column2[countLine][k-tabCount] = fileLine[i];
} else if( tabCount == 2 ){
if( !(k >= 2) ){
k = 2;
}
column3[countLine][k-tabCount] = fileLine[i];
} else if( tabCount == 3 ){
if( !(k >= 3) ){
k = 3;
}
column4[countLine][k-tabCount] = fileLine[i];
}
///////////////////////////////////////////////
i++;
k++;
}
countLine++;
}
/* Attempt at adding NULL character to strings */
/////////////////////////////////////////////////
/*for(int i = 0; i < countLine; i++){
if( ( column1[i][strlen(column1[i])]) != '\0'){
column1[i][strlen(column1[i])] = '\0';
}
if( ( column2[i][strlen(column2[i])]) != '\0'){
column2[i][strlen(column2[i])] = '\0';
}
if( ( column3[i][strlen(column3[i])]) != '\0'){
column3[i][strlen(column3[i])] = '\0';
}
}*/
/////////////////////////////////////////////////
/* Second attempt at adding NULL character to strings */
/////////////////////////////////////////////////
/*for(int i = 0; i < countLine; i++){
for(int k = 0; k < strlen(column1[i]); k++){
if(column1[i][k] == '\0'){
column1[i][k] = ' ';
}
}
for(int k = 0; k < strlen(column2[i]); k++){
if(column2[i][k] == '\0'){
column2[i][k] = ' ';
}
}
for(int k = 0; k < strlen(column3[i]); k++){
if(column3[i][k] == '\0'){
column3[i][k] = ' ';
}
}
}*/
/////////////////////////////////////////////////
//output
int b = 0;
while( b < countLine ){
if( strcmp(column4[b], "Available\n") == 0 ){
printf("%s (%s) -- %s\n", column2[b], column1[b], column3[b]);
}
b++;
}
fclose(inputFile);
return 0;
}
这是初始的txt文件:
Sandwiches Ham sandwich Classic ham sandwich Available
Sandwiches Chicken salad sandwich Chicken salad sandwich Not available
Sandwiches Cheeseburger Classic cheeseburger Not available
Salads Caesar salad Chunks of romaine heart lettuce dressed with lemon juice Available
Salads Asian salad Mixed greens with ginger dressing, sprinkled with sesame Not available
Beverages Water 16oz bottled water Available
Beverages Coca-Cola 16oz Coca-Cola Not available
Mexican food Chicken tacos Grilled chicken breast in freshly made tortillas Not available
Mexican food Beef tacos Ground beef in freshly made tortillas Available
Vegetarian Avocado sandwich Sliced avocado with fruity spread Not available
我将不胜感激任何帮助。