2016-12-02 110 views
-1

我有一個.txt文件,其值以這種格式寫入:LetterNumber,LetterNumber,LetterNumber等(例如:A1,C8,R43,A298,B4)。我想將字母和數字讀入兩個單獨的數組中(例如:array1將是A C R A B; array2將是1 8 43 298 4)。我怎樣才能做到這一點?C - 從文件中讀取整數和字符到數組

此刻我唯一想出如何閱讀所有的值,數字和字母和逗號和一切,爲字符的一個數組:

FILE *myfile; 
myfile = fopen("input1.txt", "r"); 
char input[677]; //I know there are 676 characters in my .txt file 
int i; 
if (myfile == NULL) { 
    printf("Error Reading File\n"); 
    exit (0); 
}  
for (i=0; i<677; i++) { 
    fscanf(myfile, "%c", &input[i]); 
} 
fclose(myfile); 

但最好我想兩個數組:包含一個只有字母和一個只包含數字。它甚至有可能嗎?

我會很感激任何形式的幫助,甚至只是一個提示。謝謝!

+3

是的,這是可能的。 –

+0

爲什麼不使用'getc()',在這種情況下使用'fscanf'可能會很困難。 – RoadRunner

+0

@RoadRunner使用scanf是最好的選擇,至少當它總是單個字母時。格式將簡單地爲「%c%d」。然後讀出逗號。 T.can的回答幾乎涵蓋了它。 –

回答

1

定義另一個數組對整數,

int inputD[677]; 

然後,在for循環讀取一個字符,一個整數,每次一個空間炭。

fscanf(myfile, " %c%d %*[,] ", &input[i], &inputD[i]); 
+0

您也可以簡單地爲逗號使用字符串格式.Waitepace在%s之前被丟棄。由於整個格式以空格開始,所以在讀取char之前,空白也被丟棄。 –

0

你基本上只需要創建一個char陣列和一個int陣列,然後使用fscanf從文件流中讀取值。

爲了簡單起見,使用在這種情況下while循環,使工作更容易,因爲你可以閱讀fscanf返回到EOF2值。

像這樣的事情是正確的觀念:

#include <stdio.h> 
#include <stdlib.h> 

// Wasn't really sure what the buffer size should be, it's up to you. 
#define MAXSIZE 677 

int 
main(void) { 
    FILE *myFile; 
    char letters[MAXSIZE]; 
    int numbers[MAXSIZE], count = 0, i; 

    myFile = fopen("input1.txt", "r"); 
    if (myFile == NULL) { 
     fprintf(stderr, "%s\n", "Error reading file\n"); 
     exit(EXIT_FAILURE); 
    } 

    while (fscanf(myFile, " %c%d ,", &letters[count], &numbers[count]) == 2) { 
     count++; 
    } 

    for (i = 0; i < count; i++) { 
     printf("%c%d ", letters[i], numbers[i]); 
    } 
    printf("\n"); 

    fclose(myFile); 

    return 0; 
} 
+0

你確定你的程序解析「A2,B3」嗎? –

+0

甚至「A2」,就此而言.. –

+0

是的,它解析了這一罰款。 – RoadRunner

1

我真的定義一個struct保持字母和數字一起;數據格式強烈表明它們有密切的關係。這是一個例證這個想法的程序。

scanf格式是有點很難得到正確的(儘可能簡單,但並不簡單)。例如RoadRunner在他的回答中忘記了跳過信件前面的空格。

它有助於我們(我假設)只有單個字母。記住除%c之外的所有標準格式都會跳過空格,這很有幫助。 (這句話的兩部分應該記住。)

#include<stdio.h> 

#define ARRLEN 10000 

// Keep pairs of data together in one struct. 
struct CharIntPair 
{ 
    char letter; 
    int number; 
}; 

// test data. various space configurations 
// char *data = " A1, B22 , C333,D4,E5 ,F6, Z12345"; 

void printParsedPairs(struct CharIntPair pairs[], int count) 
{ 
    printf("%d pairs:\n", count); 
    for(int i = 0; i<count; i++) 
    { 
     printf("Pair %6d. Letter: %-2c, number: %11d\n", i, pairs[i].letter, pairs[i].number); 
    } 
} 

int main() 
{ 
    setbuf(stdout, NULL); 
    setbuf(stdin, NULL); 

    // For the parsing results 
    struct CharIntPair pairs[ARRLEN]; 
    //char dummy [80]; 
    int parsedPairCount = 0; 
    for(parsedPairCount=0; parsedPairCount<ARRLEN; parsedPairCount++) 
    { 
     // The format explained> 
     // -- " ": skips any optional whitespace 
     // -- "%c": reads the next single character 
     // -- "%d": expects and reads a number after optional whitespace 
     // (the %d format, like all standard formats except %c, 
     // skips whitespace). 
     // -- " ": reads and discards optional whitespace 
     // -- ",": expects, reads and discards a comma. 
     // The position after this scanf returns with 2 will be 
     // before optional whitespace and the next letter-number pair. 
     int numRead 
      = scanf(" %c%d ,", 
        &pairs[parsedPairCount].letter, 
        &pairs[parsedPairCount].number); 

     //printf("scanf returned %d\n", numRead); 
     //printf("dummy was ->%s<-\n", dummy); 
     if(numRead < 0) // IO error or, more likely, EOF. Inspect errno to tell. 
     { 
      printf("scanf returned %d\n", numRead); 
      break; 
     } 
     else if(numRead == 0) 
     {   
      printf("scanf returned %d\n", numRead); 
      printf("Data format problem: No character? How weird is that...\n"); 
      break; 
     } 
     else if(numRead == 1) 
     { 
      printf("scanf returned %d\n", numRead); 
      printf("Data format problem: No number after first non-whitespace character ->%c<- (ASCII %d).\n", 
        pairs[parsedPairCount].letter, (int)pairs[parsedPairCount].letter); 
      break; 
     } 
     // It's 2; we have parsed a pair. 
     else 
     { 
      printf("Parsed pair %6d. Letter: %-2c, number: %11d\n", parsedPairCount, 
      pairs[parsedPairCount].letter, pairs[parsedPairCount].number); 
     } 
    } 
    printf("parsed pair count: %d\n", parsedPairCount); 
    printParsedPairs(pairs, parsedPairCount); 

} 

我掙扎了一下與bash和mintty在Windows 8%C我的cygwin環境下有時會遇到一個換行符(ASCII 10)應被前面的空白吃飯空間吃掉,破壞了解析。 (更嚴格的解析會在發生錯誤後嘗試讀取char字符,直到遇到下一個逗號,並嘗試從那裏恢復。)

發生這種情況時,我鍵入Ctr-D(或者,我認爲,也Ctr-Z在控制檯窗口中)試圖發信號EOF;以下輸入關鍵行程將導致換行符「到達」%c。當然,在Windows系統上的POSIX仿真中的文本I/O是棘手的;我必須假設在翻譯CR-NL序列之間來回轉換這個錯誤。在通過ssh/putty的linux系統上,它按預期工作。

+0

不錯,在這種情況下使用結構會更好。 – RoadRunner