Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C语言解析大型文件_C_Linux - Fatal编程技术网

用C语言解析大型文件

用C语言解析大型文件,c,linux,C,Linux,对于一个类,我被赋予了使用pthreads、openmp和MPI并行编写基数排序的任务。在这种情况下,我选择的语言是C——我不太了解C++。 无论如何,我读取文本文件的方式会导致文件大小约为500MB的分段错误。这些文件是以行分隔的32位数字: 12351 1235234 12 53421 1234 我知道C,但我不太懂;我使用我知道的东西,在这种情况下,我知道的东西效率非常低。我读取文本文件的代码如下: #include <stdlib.h> #include <stdio

对于一个类,我被赋予了使用pthreads、openmp和MPI并行编写基数排序的任务。在这种情况下,我选择的语言是C——我不太了解C++。 无论如何,我读取文本文件的方式会导致文件大小约为500MB的分段错误。这些文件是以行分隔的32位数字:

12351
1235234
12
53421
1234
我知道C,但我不太懂;我使用我知道的东西,在这种情况下,我知道的东西效率非常低。我读取文本文件的代码如下:

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>

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

 if(argc != 4) {
   printf("rs_pthreads requires three arguments to run\n");
   return -1;
 }

 char *fileName=argv[1];
 uint32_t radixBits=atoi(argv[2]);
 uint32_t numThreads=atoi(argv[3]);

 if(radixBits > 32){
   printf("radixBitx cannot be greater than 32\n");
   return -1;
 }

 FILE *fileForReading = fopen( fileName, "r" );
 if(fileForReading == NULL){
   perror("Failed to open the file\n");
   return -1;
 }
 char* charBuff = malloc(1024);

 if(charBuff == NULL){
   perror("Error with malloc for charBuff");
   return -1;
 }

 uint32_t numNumbers = 0;
 while(fgetc(fileForReading) != EOF){
   numNumbers++;
   fgets(charBuff, 1024, fileForReading);
 }

 uint32_t numbersToSort[numNumbers];

 rewind(fileForReading);
 int location;
 for(location = 0; location < numNumbers; location++){
   fgets(charBuff, 1024, fileForReading);
   numbersToSort[location] = atoi(charBuff);
     } 
#包括
#包括
#包括
#包括
#包括
int main(int argc,字符**argv){
如果(argc!=4){
printf(“rs_pthreads需要三个参数才能运行\n”);
返回-1;
}
char*fileName=argv[1];
uint32_t radixBits=atoi(argv[2]);
uint32_t numThreads=atoi(argv[3]);
如果(半径大于32){
printf(“radixBitx不能大于32\n”);
返回-1;
}
FILE*fileForReading=fopen(文件名为“r”);
if(fileForReading==NULL){
perror(“打开文件失败\n”);
返回-1;
}
char*charBuff=malloc(1024);
if(charBuff==NULL){
perror(“charBuff的malloc错误”);
返回-1;
}
uint32_t numNumbers=0;
while(fgetc(fileForReading)!=EOF){
numNumbers++;
fgets(charBuff,1024,fileForReading);
}
uint32_t numbersToSort[个数];
倒带(文件读取);
int定位;
对于(位置=0;位置
在一个有5000万个数字(约500MB)的文件中,我在倒带所有位置时都遇到了分段错误。我对文件流如何工作的知识几乎不存在。我猜它试图在没有足够内存或其他东西的情况下进行malloc,但我不知道

所以,我这里有两个问题:倒带分段是如何出错的?我只是在倒带之前做得很差,没有检查一些我应该做的系统调用吗

还有,从文本文件中读取任意数量数字的更有效方法是什么


非常感谢您的帮助。

我认为最有可能的原因是(讽刺的是)堆栈溢出。您的
numbersToSort
数组分配在堆栈上,并且堆栈具有固定的大小(因编译器和操作系统而异,但1 MB是一个典型的数字)。您应该使用
malloc()
,在堆(有更多可用空间)上动态分配
numbersToSort

别忘了以后取消分配:

free(numbersToSort);

我还想指出,如果有空行,第一次循环(用于计算行数)将失败。这是因为在空行上,第一个字符是
'\n'
,而
fgetc()
将消耗它;下一次调用
fgets()将读取下面的行,您将跳过计数中的空白行。

< P>问题在这行

uint32_t numbersToSort[numNumbers];
您正试图在堆栈中分配一个巨大的数组,堆栈大小以几KB为单位(而且旧的C标准不允许这样做),所以您可以试试这个

uint32_t *numbersToSort; /* Declare it with other declarations */


/* Remove uint32_t numbersToSort[numNumbers]; */
/* Add the code below */
numbersToSort = malloc(sizeof(uint32_t) * numNumbers);
if (!numbersToSort) {
     /* No memory; do cleanup and bail out */
     return 1;
}

rewind()
没有任何功能,它只是将文件指针放回起始位置……seg故障发生时的确切信息是什么?@Mike He可能在
rewind()之前的一行中损坏了堆栈(更准确地说是损坏了堆栈附近的东西)
通过分配一个巨大的基于堆栈的数组。你真的需要提供一个SCCE来演示这个问题。代码片段没有帮助。
numbersToSort
很可能是个问题。但不确定。它足够大吗?当你malloc
的时候,它是不是乘以了
sizeof(int)`或者你的代码有其他问题,覆盖了堆栈上读取的
文件。这就是导致
倒带
死亡的原因。为什么不直接使用
scanf
而不是读取字符串?@Gene他没有
malloc
它,他在堆栈上分配了它。读取代码(然后读取我的答案)@ dvnrras啊,谢谢。我正在寻找函数顶部的声明。在中间没有看到它。在代码错误之前,代码在部分数组中运行是不正常的。通常故障发生在函数被调用的那一点。对我来说,这好像是意外地使用了VLA和用户的情况之一。被它咬了。嘿。谢谢你的建议。当我再次访问数据文件时,我会尝试一下。这是有意义的,因为100万整数的文件没有引起这个问题,这将是1MB以下。我知道一个空行会毁了我的策略。幸运的是,我可以假设没有空行。
uint32_t *numbersToSort; /* Declare it with other declarations */


/* Remove uint32_t numbersToSort[numNumbers]; */
/* Add the code below */
numbersToSort = malloc(sizeof(uint32_t) * numNumbers);
if (!numbersToSort) {
     /* No memory; do cleanup and bail out */
     return 1;
}