2

何十億もの数字を含むテキスト ファイルを読みたい、各 10 桁をまとめて保存し、次の 10 桁をまとめてカウントしたいなど... 例: 私のファイルには 123456789123456789123456789 が含まれます 私の最初の 10 桁の数字は次のようになります: 1234567891 私の 2 桁目の番号は次のようになります: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
#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 に答える
1

データのチャンクはすべて 10 バイトであるため、必ず一度に 10 バイトを読み取ることから始めてください。
データは >= power(2,32) になる可能性があるため、その後の数値処理には type unsigned long longorを使用します。uint64_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

使用する:

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

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

ただし、これをすばやく行う必要がある場合は、mmap() でファイルを開き、mmap バッファーに対してクイック カスタム atou() を使用することをお勧めします。

于 2013-10-21T18:48:37.400 に答える
1

それらを整数として保存したくないと仮定すると、同じアプローチを使用して、10桁bufをC文字列(nullで終了)として埋めることができます

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