0

Hi everyone, i got a question about the fscanf.
I input a file and scan the data in the file.
It works good that if there are integers in line1,2,3,4 in the file.
However, it does not work good with empty file or the file only has dat in line 1 and line 2.
Please help me. Thanks

        printf("\nThe results from file %s are : \n", filename);
        printf("____________________________________________________________________\n");
        printf("| # of data | sum |  range   | mean | variance | standard deviation|\n");
        printf("____________________________________________________________________\n");
        fscanf(inputFile, "%d", &number_of_data); 
        for(i =0; i <number_of_data; i++)
        {
            fscanf(inputFile, "%d", &all_number);
            sum += all_number;  
        }  
        printf("|         %d|   %d| \n", number_of_data, sum);
        printf("____________________________________________________________________\n");
        fscanf(inputFile, "%d", &number_of_data_1);
        for(i =0; i <number_of_data_1; i++)
        {
            fscanf(inputFile, "%d", &all_number_1);
            sum_1 += all_number_1;
        }  
        printf("|         %d|   %d| \n", number_of_data + number_of_data_1, sum_1+sum);
2
Contributors
2
Replies
4
Views
4 Years
Discussion Span
Last Post by leesin
0

The first thing you need to know, is what format the data in the file, will be in. The more variety this is to the format, the more logic you will need to have, since every possible format that is found in the data file, must be handled appropriately.

So, what is the format, and what do you want done with it (put it into an array, a linked list, a queue, a stack, etc.)?

Edited by Adak

0

Thanks for replying

The format of two lines data is

3
-3 6 7

the format of four lines data is

3
-3 6 7
2
1 2

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.