2

我想读取一个包含数十亿个数字的文本文件,我想将每个 10 位数字存储在一起,然后一起计算下一个 10 位数字等等......例如:我的文件将包含 123456789123456789123456789 我的第一个 10 位数字将是:1234567891 我的第二个数字将是:2345678912 依此类推我知道下面的代码可以从文件中读取整数

#include<stdio.h>

    int main()
    {
            FILE *ptr_file;
            char buf[1000];

            ptr_file =fopen("num.txt","r");
            if (!ptr_file)
                return 1;

            while (fgets(buf,1000, ptr_file)!=NULL)
                printf("%s",buf);

        fclose(ptr_file);
            return 0;
    }

但是如何每次读取 10 位数字?

4

5 回答 5

1

采用:

ptr_file =fopen("num.txt","rb");

while(fread(buf, 1, 10, ptr_file) != 10) {
}

但是,如果您需要快速执行此操作 - 我建议通过 mmap() 打开文件,并在 mmaped 缓冲区上使用快速自定义 atou()。

于 2013-10-21T18:48:37.400 回答
1

假设您不想将它们存储为整数,因此可以采用相同的方法将 10 位数字填充buf为 C 字符串(以空字符结尾)

while ( fgets(buf, 11, ptr_file) !=NULL )
   printf("%s\n",buf);
于 2013-10-21T18:49:05.827 回答
1

由于所有数据块都是 10 个字节,因此请务必从一次读取 10 个字节开始。
由于数据可能 >= power(2,32),请使用 typeunsigned long longuint64_t用于后续数值处理。

inf = fopen("num.txt", "rb");  // Open in binary
#define ChunkSize (10)
char buf[ChunkSize + 1];         // Extra for \0
buf[ChunkSize] = '\0';
int result;
while((result = fread(buf, ChunkSize, 1, inf)) == 1) {
  unsigned long long x;
  char *endptr;
  x = strtoull(buf, 10, &endptr);
  if (endptr != &buf[ChunkSize]) {
    break;  // Syntax error
  }
  // Do something with x or buf;
}
if (result == 0) {
  ; // handle I/O error
}
于 2013-10-21T19:17:48.430 回答
1
#include<stdio.h>

int main()
{
        FILE *ptr_file;
        char buf[1000];
        char tendigits[11];

        ptr_file =fopen("num.txt","r");
        if (!ptr_file)
            return 1;

        while (fgets(buf,1000, ptr_file)!=NULL)
        {
            int counter = 0;
            do
            {
                 for(int loop=0;loop<10;loop++) tendigits[loop]=buf[counter+loop];
                 tendigits[10]='\0';
                 /*Process the tendigits string here*/
                 counter+=10;
            }while(counter<1000);
        }

    fclose(ptr_file);
    return 0;
}

当然,这只是一个例子。您必须考虑到该文件可能不是 1000 的倍数并进行相应调整。

编辑:如果您存储的数字包含“\ 0”,那么缓冲区的大小必须是 11 的倍数,并将我给出的示例调整为 11 位,删除添加的“\ 0”。

于 2013-10-23T02:31:29.343 回答
1
       #include <stdio.h>
       #include<sys/types.h>
       #include<sys/unistd.h>
       #include <math.h>
       enum  //these are modes to open the file
       { 
           reading=0; 
           writing=1;
           readwrite=2;
       }
       int main()
       {
           unsigned long int a[10];
           int file_1,i=0,lim,pid,j,k=0,res;
           if((pid=fork())==0) //child process
           {
               file_1=open("Program_name",mode); //program name has to be the path
               if(file_1<0)
               {
                   exit(1);
               }
               else
               {
                   while(1)
                   {
                       i=0;
                       j=9;
                       res=0;
                       while(i<10)
                       {
                           lim=read(file_1,&ch,sizeof(char));
                           if(lim<=0)
                           {
                               exit(2);
                           }
                           dig=convert(&ch);
                           res=res+dig*pow(10,j); //code to make a 10 digit number
                           j--;
                       }
                       a[k]=res;
                       k++;
                   }  
               }
               close(file_1);
           } //child process ends here
           else //parent process begins here
           {
               /*code that use the digits above */
           }
           return 0;
       }
       int converter(char *p) //this function coverts char to integer
       { 
           int num;
           *p=*p-'0';
           num=(int)*p;
           return num;
       }    
于 2014-01-16T08:48:43.200 回答