我收到了一個包含電影放映時間資訊的文本檔案。我必須以干凈的方式格式化資訊。現在我只是想把所有線路的資訊保存到字串中。但是,當獲得電影的評分時,陣列不會正確保存評分。
這是主要代碼。
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const int MAX_TITLE_CHARS = 44; // Maximum length of movie titles
const int LINE_LIMIT = 100; // Maximum length of each line in the text file
char line[LINE_LIMIT];
char inputFileName[25];
FILE *file;
file = fopen("D:\\movies.txt", "r");
char currentLine[LINE_LIMIT];
char movieTitle[MAX_TITLE_CHARS];
char movieTime[10];
char movieRating[10];
fgets(currentLine, LINE_LIMIT, file); // Get first file
while(!feof(file)){
sscanf(currentLine, "%[^,],D[^,],%s", movieTime, movieTitle, movieRating);
printf("%s\n", movieRating);
fgets(currentLine, LINE_LIMIT, file); // Get next file
}
return 0;
}
這是 CVS 檔案
16:40,Wonders of the World,G
20:00,Wonders of the World,G
19:00,Journey to Space ,PG-13
12:45,Buffalo Bill And The Indians or Sitting Bull's History Lesson,PG
15:00,Buffalo Bill And The Indians or Sitting Bull's History Lesson,PG
19:30,Buffalo Bill And The Indians or Sitting Bull's History Lesson,PG
10:00,Adventure of Lewis and Clark,PG-13
14:30,Adventure of Lewis and Clark,PG-13
19:00,Halloween,R
這列印出來
G
G
PG-13
PG-13
PG-13
PG-13
PG-13
PG-13
R
我需要它
G
G
PG-13
PG
PG
PG
PG-13
PG-13
R
我使用 Eclipse,在除錯器中,我看到當它遇到第PG-13一個R. 我在想也許因為PG并且PG-13有相同的兩個起始字符可能會混淆?我不知道。任何幫助表示贊賞。
uj5u.com熱心網友回復:
您的字串 'Buffalo Bill...' 超過 44 個字符。因此,sccanf 陳述句讀取到該限制,然后它會查找一個“,”,它在字串中不存在 44 個字符并退出。
因為您的新movieRating 沒有被設定,它只是列印以前的值。
提示:如果您正在尋找解決方法,您可以使用類似 strsep(); 的方法決議字串。您也可以只增加電影標題的大小。
uj5u.com熱心網友回復:
您正在使用以下行轉換該行:
sscanf(currentLine, "%[^,],D[^,],%s", movieTime, movieTitle, movieRating);
該函式會將一個字串讀入movietTime,直到輸入中出現',',然后它將讀取另一個字串,直到出現','或讀取44個字符。手冊中解釋了此行為sscanf:
...
An optional decimal integer which specifies the maximum field width.
Reading of characters stops either when this maximum is reached or when
a nonmatching character is found, whichever happens first...
具有 PG 等級的行的標題為 62 個字符。因此,它不會閱讀整個標題,也不會找到逗號。要解決此問題,您可以將 MAX_TITLE_CHARS 設定為更大的值或使用%m修飾符sscanf為您動態分配字串。
uj5u.com熱心網友回復:
OP 代碼具有未定義的行為(UB),因為它movieTitle[]僅足夠大 43 個字符 終止空字符和使用的 OP,"D[^,]"而不是 43 的正確寬度限制。
const int MAX_TITLE_CHARS = 44; // Maximum length of movie
...
char movieTitle[MAX_TITLE_CHARS];
這個 UB 之后還有其他問題。
占線的'\n'和 a'\0'組成一個字串。
永遠不要使用while(feof(...)).
測驗sscanf()結果。
精確限制列印的標題寬度。
const int LINE_LIMIT = 100; // Maximum length of each line in the text file
char line[LINE_LIMIT 2 /* room for \n and \0 */];
while (fgets(currentLine, sizeof currentLine, file)) {
// Either use a _width limit_ with `"%s"`, `"%[]"` or use a worse case size.
char movieTime[10 1]; // 10 characters \0
char movieTitle[sizeof currentLine];
char movieRating[sizeof currentLine];
// Examples:
// Use a 10 width limit for the 11 char movieTime
// Others: use a worst case size.
if (sscanf(currentLine, " [^,], %[^,], %[^\n]",
movieTime, movieTitle, movieRating) != 3) {
fprintf(stderr, "Failed to parse <%s>\n", currentLine);
break;
}
// Maximum length of movie titles _to print_
const int MAX_TITLE_CHARS = 44;
printf("Title: %-.*s\n", MAX_TITLE_CHARS, movieTitle);
printf("Rating: %s\n", movieRating);
}
請注意,如果長度包括結尾,則“每行的最大長度”不清楚'\n'。在 C 庫中,一行包含'\n'.
文本流是組成為行的有序字符序列,每行由零個或多個字符加上一個終止換行符組成。最后一行是否需要終止換行符是實作定義的。C17dr § 7.21.2 2
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/441579.html
