C语言读取制表符分隔食品文件时字符串输出乱码问题(已用strtok解决)
C语言文件读取分割字符串导致乱码问题
编辑:使用strtok()成功解决了我的问题。感谢Barmar。
我是C语言新手,正在完成大学作业时遇到问题,已经花了好几个小时调试。作业要求从存储食品库存信息的文本文件读取输入,根据库存状态输出对应食品:程序要先获取用户输入的文件名,读取文件内容并存入四个预定义字符串数组,最终按照名称(类别)——描述的格式输出可用食品。
文本文件每行格式为:类别、名称、描述、库存状态,各字段以\t分隔,需要去除该分隔符。
我遇到的问题是输出格式异常,运行后输出如下乱码:
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'
我认为乱码是因为预定义字符串缺少\0终止符导致的,问题根源在于把每行内容分割存入对应数组的逻辑。我试过手动添加终止符但没成功,已经联系教授但还没收到回复。
以下是我的当前代码:
#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; }
原始文本文件内容如下:
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
内容的提问来源于stack exchange,提问作者Phoinexx
相关产品推荐
相关产品推荐

