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);

Recommended Answers

All 2 Replies

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.)?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.