2014-10-08 43 views
2

我在每一行上都有一个带有数字的文本文件。我想在C中编写一个读取文件的函数,并在每次调用函数时返回文件中的下一个数字。每次调用函数时读取文件的下一行

例如,如果我有这些数字:

100 
200 
300 
400 

和函数调用get_number(),如果我叫get_number(),它会返回100,如果我再次调用它,它会返回200等

这就是我迄今为止所写的内容,但每次函数被调用时,都会返回到文本文件中的第一个数字。

int * get_number() 
{ 

    FILE* file = fopen("random_numbers.txt", "r"); 
    char line[256]; 

    if (file==NULL) 
    { 
     perror ("Error reading file"); 
    } 
    else 
    { 
     fgets(line, sizeof(line), file); 
     printf("%s", line); 
    } 

    return 0; 
} 
+0

你要使用'FTELL()'来获得在读取之后,您在文件中的当前位置,并且您将要使用'fseek()'来寻找t在随后的每一次阅读之前,该位置。 – Icemanind 2014-10-08 22:27:08

+0

您在这里泄漏文件描述符。至少可以说。 – wildplasser 2014-10-08 22:27:38

+1

每次调用函数时都重新打开文件。试着通过在方法外执行该操作,或者在打开文件之前检查该文件是否为NULL。 – dramzy 2014-10-08 22:28:58

回答

0

这里有一个版本,正是这么做的:

int * get_number(long* pos) 
{ 

    FILE* file = fopen("random_numbers.txt", "r"); 
    char line[256]; 

    if (file==NULL) 
    { 
     perror ("Error reading file"); 
    } 
    else 
    { 
     fseek(file , *pos , SEEK_CUR); 
     fgets(line, sizeof(line), file); 
     printf("%s", line); 
    } 
    *pos = ftell(file); 
    return 0; 
} 

,你从main这样称呼它

long pos = 0; 
get_number(&pos); 

或更好,但使用一个静态变量

int * get_number() 
{ 
    static long pos = 0; 
    FILE* file = fopen("random_numbers.txt", "r"); 
    char line[256]; 

    if (file==NULL) 
    { 
     perror ("Error reading file"); 
    } 
    else 
    { 
     fseek(file , pos , SEEK_CUR); 
     fgets(line, sizeof(line), file); 
     printf("%s", line); 
    } 
    pos = ftell(file); 

    return 0; 
} 
0

避免重复打开文件是个好主意。每次调用函数时,不要打开文件,而要打开一次,然后在每次调用函数时将文件指针传递给该函数。

int * get_number(FILE *file) 
{ 
    char line[256]; 

    fgets(line, sizeof(line), file); 
    printf("%s", line); 

    return 0; 
} 

int main() 
{ 
    FILE *file = fopen("random_numbers.txt", "r"); 

    if (file == NULL) 
    { 
     perror("Error opening file"); 
     return 1; 
    } 

    while (!feof(file)) 
    { 
     get_number(file); 
    } 

    fclose(file); 
} 
0
  1. 公开赛在调用函数的文件。

  2. FILE*传递给get_number

  3. get_number返回int而不是int*

这是修改的get_number

int get_number(FILE* file) 
{ 
    // This is the core functionality. 
    // You should add error handling code 

    int number; 
    int n = fscanf(file, "%d", &number); 
    if (n != 1) 
    { 
     // Add error handling code. 
    } 

    return number; 
} 
0

这个是正常的,因为每次调用get_number()时都会打开文件; (这是更糟糕,因为没有FCLOSE被称为 你想,也许是给在get_number()文件描述符什么;这样:

void get_number(FILE* file) 
{ 

    char line[256]; 

    if (file==NULL) 
     perror ("Bad descriptor given"); 
    else 
    { 
     if (fgets(line, sizeof(line), file) == NULL) 
      perror("Fgets failed"); 
     else 
      printf("%s", line); 
    } 
} 

,你想要什么,你的功能外,就是做如下:。

FILE * file = fopen("random_numbers.txt", "r"); 
get_number(file); // 100 
get_number(file); // 200 
fclose(file); 

我做你的函数void,因为这里的回报率是没有意义的,您可以更改和使用的atoi和返回fonction

+0

它应该检查'fgets'的返回值。用fgets的支票更新了 – BLUEPIXY 2014-10-08 22:39:34

+0

。 – Mekap 2014-10-08 22:43:24