简体   繁体   中英

Why the incorrect data are read from fscanf?

I trying to read the data by fscanf from file, then I printed the data. However, some of data are not correct. There are four column. Only third column data have a problem. The other column is no problem.

Small number is read from file correctly, but large data are saturated to 26843519. It looks data type limitation. but the number 26843519 is not the limitation number of specific data type.

I trying to change many data type. I still could not solve this issue.

Could you please help it?

Compling and running system : I7-4790, ubuntu 14.04.1 x86_64, gcc(g++) 4.8.4

Source:

while(!feof(trace_file))
{
double time;
unsigned long location;
unsigned int req_size, temp; 
char op_type[10];

//fscanf(trace_file, "%s\t%lf\t%d\n", op_type, &time, &location);
fscanf(trace_file, "%lf %s %lu %d %d \n", &time, op_type, &location, &req_size, &temp);

// for debug
printf("[Application] OP : %s     Location : %lu     Time : %lf    Size : %d\n", op_type, location, time, req_size);

Input data file:

(The third column is a problem.)

128166477394345573     Read     383496192     32768     113736
128166483087163644     Read     2822144     65536     71730 
128166620794097839     Read     3221266432     4096     121008
128166624207799335     Read     3354624     49152     147664
128166624207887065     Read     2961408     57344     59933
128166624210238731     Read     368979968     65536     52003
128166624211812801     Read     395730944     65536     40423
128166624211962708     Read     442093568     65536     46765
128166624211968354     Read     437964800     65536     41118
128166624212755738     Read     396734464     49152     34979
128166624212868365     Read     386232320     20480     78602
128166624212905977     Read     398438400     45056     40989
128166624213527427     Read     378523648     65536     44536
128166624213988219     Read     404738048     65536     52490
128166624213996455     Read     404475904     65536     44254
128166624232905615     Read     381739008     65536     41223
128166624242878893     Read     439144448     57344     67882
128166624242935246     Read     3608576     57344     11528
128166624243149609     Read     386646016     65536     109663
128166624243365012     Read     398176256     65536     50510
128166624274398038     Read     397676544     32768     111035
128166624295029088     Read     6320128     65536     104853

Outputted results (by printf):

(The third data will be outputted to "Location".)

[Application] OP : Read     Location : 26843519     Time : 128166477394345568.000000    Size : 32768
[Application] OP : Read     Location : 2822144     Time : 128166483087163648.000000    Size : 65536
[Application] OP : Read     Location : 26843519     Time : 128166620794097840.000000    Size : 4096
[Application] OP : Read     Location : 3354624     Time : 128166624207799328.000000    Size : 49152
[Application] OP : Read     Location : 2961408     Time : 128166624207887072.000000    Size : 57344
[Application] OP : Read     Location : 26843519     Time : 128166624210238736.000000    Size : 65536
[Application] OP : Read     Location : 26843519     Time : 128166624211812800.000000    Size : 65536
[Application] OP : Read     Location : 26843519     Time : 128166624211962704.000000    Size : 65536
[Application] OP : Read     Location : 26843519     Time : 128166624211968352.000000    Size : 65536
[Application] OP : Read     Location : 26843519     Time : 128166624212755744.000000    Size : 49152
[Application] OP : Read     Location : 26843519     Time : 128166624212868368.000000    Size : 20480
[Application] OP : Read     Location : 26843519     Time : 128166624212905984.000000    Size : 45056
[Application] OP : Read     Location : 26843519     Time : 128166624213527424.000000    Size : 65536
[Application] OP : Read     Location : 26843519     Time : 128166624213988224.000000    Size : 65536
[Application] OP : Read     Location : 26843519     Time : 128166624213996448.000000    Size : 65536
[Application] OP : Read     Location : 26843519     Time : 128166624232905616.000000    Size : 65536
[Application] OP : Read     Location : 26843519     Time : 128166624242878896.000000    Size : 57344
[Application] OP : Read     Location : 3608576     Time : 128166624242935248.000000    Size : 57344
[Application] OP : Read     Location : 26843519     Time : 128166624243149616.000000    Size : 65536
[Application] OP : Read     Location : 26843519     Time : 128166624243365008.000000    Size : 65536
[Application] OP : Read     Location : 26843519     Time : 128166624274398032.000000    Size : 32768
[Application] OP : Read     Location : 6320128     Time : 128166624295029088.000000    Size : 65536
[Application] OP : Read     Location : 26843519     Time : 128166624295087648.000000    Size : 49152

Your (updated) code works for me. In any case, it's not a problem with integer size unless you're using a very odd architecture. The first value to go wrong is 383,496,192, which can be represented in 28 bits (2^29 = 536,870,912).

If there is a question about architecture impacting integer size, the you can use the exact size integer representations provided by stdint.h and the scan and print macros in inttypes.h . For example, your code could be rewritten as:

#include <stdio.h>
#include <stdint.h>     /* for uintX_t       */
#include <inttypes.h>   /* for SCNtX & PRItX */

int main (int argc, char **argv) {

    double time;
    uint64_t location;          /* exact size variables */
    uint32_t req_size, temp; 
    char op_type[10];
    FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;

    if (!fp) {  /* validate file open for reading */
        fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
        return 1;
    }

    /* using SCN with fscanf and PRI with printf */
    while (fscanf (fp, "%lf %s %" SCNu64 " %" SCNu32 " %" SCNu32 "", 
                &time, op_type, &location, &req_size, &temp) == 5)
        printf ("[Application] OP : %s    Location : %10" PRIu64 "   "
                "  Time : %.0lf    Size : %5" PRIu32 "\n", op_type, 
                location, time, req_size);

    if (fp != stdin) fclose (fp);     /* close file if not stdin */

    return 0;
}

That will eliminate any storage size differences.

Example Use/Output

$ ./bin/readloc <dat/location.txt
[Application] OP : Read    Location :  383496192     Time : 128166477394345568    Size : 32768
[Application] OP : Read    Location :    2822144     Time : 128166483087163648    Size : 65536
[Application] OP : Read    Location : 3221266432     Time : 128166620794097840    Size :  4096
[Application] OP : Read    Location :    3354624     Time : 128166624207799328    Size : 49152
[Application] OP : Read    Location :    2961408     Time : 128166624207887072    Size : 57344
[Application] OP : Read    Location :  368979968     Time : 128166624210238736    Size : 65536
[Application] OP : Read    Location :  395730944     Time : 128166624211812800    Size : 65536
[Application] OP : Read    Location :  442093568     Time : 128166624211962704    Size : 65536
[Application] OP : Read    Location :  437964800     Time : 128166624211968352    Size : 65536
[Application] OP : Read    Location :  396734464     Time : 128166624212755744    Size : 49152
[Application] OP : Read    Location :  386232320     Time : 128166624212868368    Size : 20480
[Application] OP : Read    Location :  398438400     Time : 128166624212905984    Size : 45056
[Application] OP : Read    Location :  378523648     Time : 128166624213527424    Size : 65536
[Application] OP : Read    Location :  404738048     Time : 128166624213988224    Size : 65536
[Application] OP : Read    Location :  404475904     Time : 128166624213996448    Size : 65536
[Application] OP : Read    Location :  381739008     Time : 128166624232905616    Size : 65536
[Application] OP : Read    Location :  439144448     Time : 128166624242878896    Size : 57344
[Application] OP : Read    Location :    3608576     Time : 128166624242935248    Size : 57344
[Application] OP : Read    Location :  386646016     Time : 128166624243149616    Size : 65536
[Application] OP : Read    Location :  398176256     Time : 128166624243365008    Size : 65536
[Application] OP : Read    Location :  397676544     Time : 128166624274398032    Size : 32768
[Application] OP : Read    Location :    6320128     Time : 128166624295029088    Size : 65536

Look it over, give it a try, and let me know if you have any questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM