用C处理文件中的整数 [英] Processing integers from a file in C

查看:121
本文介绍了用C处理文件中的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题解决了! ..................................

Problem solved! .........................

推荐答案

我看到的是你第一次扫描后没有关闭文件.那会发生什么呢?

第一次循环浏览文件时,您将一直浏览到END OF FILE,并且FILE结构的内部读取指针指向该位置.然后,您尝试在文件末尾之后读取其他t_size号->那行不通.如果您使用调试器,则可能会看到第二个循环(带有计数器的循环)永远不会执行.

那你该怎么办?
要么
1)在第一次通读后关闭文件,重新打开并跳过第一个整数



2)读取第一个整数后,您立即分配数组并开始将数据倒入其中,在将读取的整数分配给它在数组中的位置之前进行检查.

希望能对您有所帮助,
Denis
The thing I see is that you''re not closing the file after having it scanned the first time. So what happens?

The first time you cycle through the file you go right until the END OF FILE, and the internal read pointer of the FILE structure points there. Then you try to read other t_size number AFTER the end of file -> that doesn''t work. If you step with a debugger you will probably see the second loop (the one with the counter) never being executed.

So what can you do?
Either
1) close the file after the first read-through, re-open it and skip the first integer

OR

2) After reading the first integer you immediately allocate the array and start pouring data in there, making the checks right before assigning the read integer to its place in the array.

Hope to be of help,
Denis


while ( fscanf ( fp, "%d", &value) == 1) // read one integer
                {
                o++;                // this loop will continue until EOF or non-integer input
                }



现在你在EOF



now you''re at EOF

        if ( o > t_size)    // If the second line of the file has more integers
                            // than the first number in the first line says...
        {
        printf ( "too many integers\n");    // ...then will "print" an error.
        getchar();          // wait the user press a key
        return 1;           // the code will return 1, exit the program
        }
        if ( o < t_size)    // If the second line of the file has less integers
                            // than the first number in the first line says...
        {
        printf ( "not enough integers\n");  // ...then will "print" an error.
        getchar();          // wait the user press a key
        return 1;           // the code will return 1, exit the program
        }


        //else if everything work..

printf("Create a size of %d array\n", t_size);

int* my_array = NULL;
my_array = malloc(t_size*sizeof(*my_array));

if (my_array==NULL) {
printf("Error allocating memory!\n");
return 1;
getchar();
}



这些是简单的静态检查,它们可以正常工作.

后续的fscanf将从何处拾取数据? EOF之后.不好



These are simple static checks, they work just fine.

The subsequent fscanf where does it pick the data from? AFTER EOF. Bad.

        int i =0;
        for ( i = 0; i < t_size; i++ )
        {
        fscanf(fp, "%d",&my_array[i]);
        }
}




如果您在上述循环之前添加后续行




If you add the sequent lines before the above cycle

fclose(fp);
fp = fopen ("xxx.txt", "r");
fscanf(fp, "%*d");  // ditch the next integer


它应该工作.那是第一种方法.

另外请注意,在循环中进行两次琐碎的整数检查并不比在文件中进行两次TWICE慢.


it should work. That is the first way.

Also note that making two trivial integer checks in a loop is not slower than going TWICE through the file.


这篇关于用C处理文件中的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆