2012-09-19 74 views
0

我在读取和写入文件时遇到问题。我从input.txt读取数据,计算(Bytelandian问题)并将结果写入文件。分割错误(核心转储)

如果我删除代码的最后一节,

/* 
    for (input_counter=0; input_counter<no_of_cases; input_counter++) 
    { 
     sprintf(buffer, "%d", max[input_counter]); 
    fputs(buffer, fw);     
    } 
    fclose(fw); 
    */ 

一切正常(除了我不能写入文件)。但是我能够编译和运行代码。

但是,在包含该代码时,出现错误“分段错误(核心转储)”。任何想法可能发生什么?

#include<stdio.h> 
    #include <stdlib.h> 

    int Calculate_max (int number){ 
     int counter; 
     int store[number+1]; 
     store[0] = 0; 
     if (number>=1){ 
      store[1] = 1; 
     } 
     if (number>=2){ 
      store[2] = 2; 
     } 

     for (counter=3; counter<=number; counter++){ 
      store[counter] = store [counter/2] +store[counter/3]+store[counter/4]; 
      if (store[counter]<counter){ 
       store[counter]=counter; 
      } 
     } 
     return store[number]; 
    } 

    int main(void){ 
     int no_of_cases=0; 
     int number[10]; 
     int max[10]; 
     int input_counter=0; 
     char line [ 128 ]; 
     char buffer [ 16 ]; 
     FILE *fr= fopen ("input.txt", "rt");   /* declare the file pointer */ 
     FILE *fw= fopen ("output.txt", "W+");   /* declare the file pointer */ 
     if (fr != NULL) 
     { 
      if (fgets (line, sizeof line, fr) != NULL) /* read a line */ 
     { 
      no_of_cases = atoi (line); 
      //printf("no %d \n", no_of_cases); 
     }  
     if (no_of_cases==0) 
     { 
      printf("No Cases!!"); 
     } 
     else 
     { 
      for (input_counter=0; input_counter<no_of_cases; input_counter++) 
      { 
       if (fgets (line, sizeof line, fr) != NULL) 
      { 
       number[input_counter] = atoi (line); 
       //scanf (line, number[input_counter], "%d"); 
       //printf(" %s \n " , line); 
       //fputs (line, stdout); 
      }    
      max[input_counter]= Calculate_max(number[input_counter]); 
      //fwrite(max[input_counter],sizeof(int),1,fp);   
      //fprintf(fw, "%d \n", max[input_counter]);     
      printf("%d \n", max[input_counter]);     
      }    
      } 
     fclose(fr); 
     } 
     /* 
     for (input_counter=0; input_counter<no_of_cases; input_counter++) 
     { 
      sprintf(buffer, "%d", max[input_counter]); 
     fputs(buffer, fw);     
     } 
     fclose(fw); 
     */ 
    return 0; 
    } 

新代码:

#include<stdio.h> 
    #include <cstdlib> 
    long long Calculate_max (long long number) 
    { 
    long long counter; 
    long long store[number+1]; 
    store[0] = 0; 
    if (number>=1){ 
     store[1] = 1; 
    } 
    if (number>=2){ 
     store[2] = 2; 
    } 
    for (counter=3; counter<=number; counter++){ 
     store[counter] = store [counter/2] +store[counter/3]+store[counter/4]; 
     if (store[counter]<counter){ 
      store[counter]=counter; 
     } 
    } 
    return store[number]; 
    }   
    int main(void) 
    { 
    int no_of_cases=10; 
    long long number; 
    long long max[10]; 
int input_counter=0; 
char line [128]; 
char buffer [ 64 ]; 
FILE *fr= fopen ("input.txt", "rt");   /* declare the file pointer */ 
FILE *fw= fopen ("output.txt", "w+");   /* declare the file pointer */ 
if (fr != NULL) 
{ 
    while (fgets (line, sizeof line, fr) != NULL) 
     { 
      //number= atoll (line); 
      number=1000000000;    
      max[input_counter]= Calculate_max(number); 
      input_counter++; 
      printf("test \n"); 
     }    
    fclose(fr); 
} 
printf("writing \n"); 
no_of_cases=input_counter; 
for (input_counter=0; input_counter<no_of_cases; input_counter++) 
{ 
    sprintf(buffer, "%lld", max[input_counter]);   
    fputs(buffer, fw);     
    fputs("\n", fw);     
} 
fclose(fw); 
return 0; 
    } 
+0

从我绘制的错误消息你在unix。尝试使用'gdb'调试器来逐步跟踪程序,同时检查正在使用的变量的值。你也可以喂入被转储到'gdb'中的核心,它会告诉你分割违规发生的地方。 – alk

+0

使用调试符号进行编译,通常使用-g标志并在调试器中运行它。这应该是非常明显的。 – dmp

+0

现在我的答案解释了新的代码错误。 – Chimera

回答

3

你真的应该在这里使用调试器,它会告诉你到底是哪行代码崩溃。

我怀疑你的问题是输出文件fw无法打开,所以致电fputs(buffer, NULL)崩溃。您应该检查以确保文件已成功打开,如果不成功,请相应地进行保护。由于您传递的是无效模式字符串"W+",因此可能无法打开。由于您只需要写入权限,因此您不需要读写权限(如果需要,请使用"w+"代替)。

+0

是啊!工作! 非常感谢! –

+0

欢迎来到StackOverflow!如果这解决了你的问题,你应该[接受它作为答案](http://meta.stackexchange。COM /问题/ 5234 /如何-不接受-的回答工作)。 –

2

编辑:你的新代码在store[0] = 0Calculate_max()功能失败,因为你number包含太大创建的long longs堆栈上大小的数组的值。

正如Adam在他的回答中所建议的那样,您应该使用调试器来帮助您确定代码导致问题的位置。我已经为你做了这件事,所以你可以看到它是如何完成的。希望你会发现这有用。

这是一个使用GDB的调试会话。你会看到分段故障在第69行造成的:

fclose(fw); 

的解决方法是使用下面这行来打开文件:

FILE *fw= fopen ("output.txt", "w+"); 

注意,w是现在小写。

[[email protected] ~]$ gcc -ggdb boom.c -o boom 
[[email protected] ~]$ gdb boom 
Reading symbols from /home/jrn/boom...done. 
(gdb) run 
Starting program: /home/jrn/boom 

Program received signal SIGSEGV, Segmentation fault. 
0x00c0660d in [email protected]@GLIBC_2.1() from /lib/libc.so.6 
(gdb) bt 
#0 0x00c0660d in [email protected]@GLIBC_2.1() from /lib/libc.so.6 
#1 0x08048759 in main() at boom.c:69 
+0

我更新了代码以包含long long。 现在我在Calculate_max出现错误。 这个错误(分段错误)是什么意思? –

+0

分段错误意味着您写入了超出界限的内存位置(您错误地指向了无效地址)。见http://www.cprogramming.com/debugging/segfaults.html – Chimera

+0

如果你发现我的答案有帮助,请给予好评和/或接受的答案。这就是你如何奖励人们在这里帮助你。 – Chimera