2015-11-21 79 views
0

我一直在尋找一些關於這個問題的答案,但我只看到的東西,如:讀空間的東西,並顯示在屏幕上。如何閱讀和保存在C的可變部分的TXT

我的問題是類似的,但增加了一個變化。

假設你有一個文件:

邁克爾;人; 25,34,12,34

我的問題是,你怎麼能走這條線,並保存號碼,只有一些數組中的數字?

我一直在試圖讀取的行,並顯示它做一些測試的典型的事情,但我無法找到如何保持這部分在某些陣列:

const int NUMBEROFLINES = 4; 
FILE *fp 
int i; 
char leer[100] = ""; 

    fp = fopen("name.txt", "r"); 
    if (fp == NULL) 
    { 
     perror("ERROR"); 
    } 
    for (i=0;i<NUMBEROFLINES;i++) 
    { 
    fgets(read, 100, fp); 
    puts(read); 
    read[strlen(leer)] = '\0'; 
} 
close(fp); 
+0

請更具體一些;你是否想將這些數字中的每一個都保存在「int」數組中,還是你想要一個包含第二個分號後面的子串的數組? – szczurcio

+0

請不要發佈無法編譯的,代碼不正確的代碼。見[mcve] –

回答

0

我假設你想將每個字符串都是一個數字轉換成一個數字並將這個數字存儲到一個數組中。
我還假設你的文件包含多行,因爲你有一個常量說有4行。
這似乎是你沒有太多的經驗與c,或編程,或stackoverflow或谷歌,所以我會張貼一個示例程序,做我想你想要的。

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

int main() { 
    // assuming we know how many lines we have and there is a constant number of numbers per such line 
    const int NUMBEROFLINES = 4; 
    const int NUMBERS_PER_LINE = 4; 

    FILE * fp; 
    char * line = NULL; 
    size_t len = 0; 
    ssize_t read; 

    // we count the numbers we found 
    int numbers = 0; 
    // we create an array big enaugh to store the amount of numbers we think we will find 
    size_t arraySize = NUMBEROFLINES * NUMBERS_PER_LINE; 
    long* array = malloc(sizeof(long) * arraySize); 
    // if malloc failed (there is not enough memory) we stop 
    if(array == NULL){ 
     printf("malloc failed\n"); 
     return -1; 
    } 

    fp = fopen("PATH_TO_YOUR_FILE/input.txt", "r"); 
    if (fp == NULL) 
     return -2; 

    // we read each line into a pointer line and store in len how long that line was 
    while ((read = getline(&line, &len, fp)) != -1) { 
     // we have a pointer that will contain the single parts separated by , or ; 
     char* numberS = line; 
     for(int i = 0; i < len; i++){ 
      // when we find , or ; we replace it by '\0' so that the word in numberS ends there 
      if(line[i] == ';' || line[i] == ','){ 
       line[i] = '\0'; 
       char *endptr; 
       // we try to convert our string in numberS to a number 
       long number = strtol(numberS, &endptr, 10); 
       // if endptr and numberS are the same, there was no number in numberS 
       if(endptr != numberS){ 
        // we try to put our new number to the array 
        if(numbers < arraySize){ 
         array[numbers++] = number; 
        } else { 
         // if the array is full we alloc more memory (twice as much) 
         array = realloc(array, 2 * arraySize); 
         // if realloc fails we quit 
         if(array == NULL){ 
          printf("realloc failed"); 
          return -3; 
         } else { 
          arraySize = 2 * arraySize; 
          array[numbers++] = number; 
         } 
        } 
       } 
       // our new word starts after the , or ; we just found 
       numberS = line + i + 1; 
      } 
     } 
    } 

    printf("Your numbers are:\n"); 
    for(int i = 0; i < numbers; i++){ 
     printf("%ld\n", array[i]); 
    } 

    fclose(fp); 
    if (line) 
     free(line); 
    return 0; 
} 
+0

哇,非常感謝!這就是我一直在尋找的! – D4rKNiGhT

1

您可以使用strtok()來獲取由;分隔的令牌,然後您可以使用strtol()或編寫自己的函數來遍歷每個令牌字符串中的字符,並檢查每個字符是否是數字數字(ASCII代碼比較)。如果字符串都是數字字符,請將其保存到數組中,否則不要。