我试图通过文件中的fscanf读取数据,然后打印数据。 但是,有些数据不正确。有四列。只有第三列数据有问题。另一栏没问题。
从文件正确读取小数字,但是大数据饱和到26843519.它看起来是数据类型限制。但数字26843519不是特定数据类型的限制数。
我试图更改许多数据类型。我仍然无法解决这个问题。
你能帮忙吗?
Compling and running系统: I7-4790,ubuntu 14.04.1 x86_64,gcc(g ++)4.8.4
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);
(第三栏是个问题。)
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
(第三个数据将输出到"位置"。)
[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
答案 0 :(得分:1)
您的(更新的)代码适合我。在任何情况下,除非你使用非常奇怪的架构,否则它不是整数大小的问题。出错的第一个值是383,496,192,可以用28位表示(2 ^ 29 = 536,870,912)。
答案 1 :(得分:1)
如果有关于体系结构影响整数大小的问题,您可以使用stdint.h
提供的精确大小整数表示以及inttypes.h
中的扫描和打印宏。例如,您的代码可以重写为:
#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;
}
这将消除任何存储大小差异。
示例使用/输出
$ ./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
仔细看看,试一试,如果您有任何问题,请告诉我。