[英]Reading multiple strings from file seperated with space, using fscanf
[英]How to restrict fscanf properly on a string from a txt file? (need to read strings and integers seperated with whitespace AND “-”)
我真的希望有人能夠提供幫助,或者將我指向可以的資源,因為我已經為此苦苦掙扎了幾個小時。
我在 C 或一般編碼方面沒有很多經驗,我有這個任務,我需要將帶有足球/足球比賽結果的 txt 文件讀入數組,但我無法獲得“fscanf”部分工作。
我創建了一個結構,其中包含我需要保存和稍后使用的詳細信息。 (現在日期只是保存為字符串,或者我可以將該信息保存為 2 個整數 DD MM)。
我從main(int function,因為我需要知道稍后為qsort讀取多少行)調用我的“file_read”,並將我的“struct type”數組和.txt文件作為參數傳遞給該function。
In the "file_read" function I have an int variable for counting, and opening the file specified in main, checking for NULL, and then "getting to it" by calling the function "read_match" and passing my file pointer as parameter.
這是我遇到麻煩的地方(根據更精通代碼的情況可能更早)!
我不知道如何區分文本文件的各個段(?),使用 %s 和諸如此類的東西,以便能夠正確地將其保存在各種結構類型中。
txt 文件中的數據如下所示:
(day) (date)(time) (teamA)(teamB) (goalsA)(goalsB) (spectators)
Son 28/07 18.00 BIF - OB 3 - 2 13689
Man 29/07 19.00 AaB - SIF 3 - 1 5885
Fre 02/08 19.00 SIF - HOB 2 - 3 3468
(在上面的數據中添加了結構名稱)
到目前為止,我的整個代碼如下所示:(滾動到底部)
(I tried various approaches like %[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ] %[0123456789] and what you see below, but it always seems to go bad after the first "-" in the txt file. There's a print function added for troubleshooting)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STR_SHORT 10
#define STR_LONG 70
#define MATCHES 300
typedef struct {
char day[STR_SHORT];
char date[STR_SHORT];
char time[STR_SHORT];
char team_A[STR_SHORT];
char team_B[STR_SHORT];
int goals_A;
int goals_B;
int spectators;
} Match;
int file_read(char *file_input, Match arr_matches[]);
Match read_match(FILE *file_ptr);
/*MAIN*/
int main(void) {
Match arr_matches[MATCHES];
int n;
n = file_read("data.txt", arr_matches);
printf(" %d", n);
return 0;
}
/*FUNCTIONS*/
int file_read(char *file_input, Match arr_matches[]) {
int r = 0;
FILE *file_ptr = fopen(file_input, "r");
if (file_ptr == NULL) {
printf("Error! Can't open file!");
return 1;
}
while(!(feof(file_ptr))){
arr_matches[r] = read_match(file_ptr);
++r;
}
fclose(file_ptr);
return r;
}
Match read_match(FILE *file_ptr) {
Match input;
fscanf(file_ptr, " %s %s %s"
" %s %7s"
" %2d %*[-] %2d"
" %d",
input.day, input.date, input.time,
input.team_A, input.team_B,
&input.goals_A, &input.goals_B,
&input.spectators);
printf("(day) %s (date) %s (time) %s (teamA) %s (teamB) %s (scoreA) %d (scoreB) %d (spect) %d\n",
input.day, input.date, input.time, input.team_A, input.team_B, input.goals_A, input.goals_B, input.spectators);
return input;
}
非常感謝有用的反饋,如果我需要詳細說明,請告訴我。
謝謝!
編輯:一直在嘗試STRTOK,我也許可以以某種方式結合它......
#include <string.h>
#include <stdio.h>
int main () {
char str[80] = "Fre 12/07 19.00 FCM - EFB 1 - 0 7310 ";
const char s[] = "-' '";
char *token;
/* get the first token */
token = strtok(str, s);
/* walk through other tokens */
while( token != NULL ) {
printf( " %s\n", token );
token = strtok(NULL, s);
}
return(0);
}
如何在 txt 文件中的字符串上正確限制
fscanf
? (需要讀取用空格和“-”分隔的字符串和整數)?
OP 格式中缺少的關鍵部分是第一個'-'
和未能測試返回值。
// Son 28/07 18.00 BIF - OB 3 - 2 13689
// fscanf(file_ptr, " %s %s %s" " %s %7s" " %2d %*[-] %2d" " %d", ...
int cnt = fscanf(file_ptr, "%s%s%s" "%s -%7s" "%2d -%2d" "%d",
// ^
if (cnt == 8) Success();
另請查看為什么“while (?feof (file))”總是錯誤的?
提示:不要使用fscanf()
讀取數據行,而是使用fgets()
。
char buf[100];
if fgets(buf, sizeof buf, file_ptr) == NULL) {
然后使用sscanf()
、 strtol()
等解析輸入行。
最好將寬度限制與"%s"
一起使用
int cnt = fscanf(file_ptr, "%9s%9s%9s" "%9s -%9s" "%2d -%2d" "%d",
決定如何 1) 不再處理輸入,2) 錯誤輸入。
一些未經測試的代碼將這些想法結合在一起:
它還使用" %n"
在輸入行的大部分之后檢測垃圾。
Match* read_match(FILE *file_ptr, Match *input) {
char buf[100];
if (fgets(buf, sizeof buf, file_ptr) == NULL) {
return NULL;
}
int n = 0;
if (sscanf(buf,
"%9s%9s%9s" "%9s -%9s" "%2d -%2d" "%d" " %n", //
input->day, input->date, input->time, //
input->team_A, input->team_B, //
&input->goals_A, &input->goals_B, //
&input->spectators, //
&n) != 8 || buf[n]) {
fprintf(stderr, "Invalid input \"%s\"\n", buf);
return NULL;
}
return input;
}
int file_read(char *file_input, int n, Match *arr_matches) {
int r = 0;
FILE *file_ptr = fopen(file_input, "r");
if (file_ptr == NULL) {
printf("Error! Can't open file!");
return -1;
}
int i;
for (i = 0; i < n; i++) {
if (read_match(file_ptr, &arr_matches[i]) == NULL) {
break;
}
}
fclose(file_ptr);
return i;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.