我在讀取和寫入文件時遇到問題。我從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;
}
從我繪製的錯誤消息你在unix。嘗試使用'gdb'調試器來逐步跟蹤程序,同時檢查正在使用的變量的值。你也可以喂入被轉儲到'gdb'中的核心,它會告訴你分割違規發生的地方。 – alk
使用調試符號進行編譯,通常使用-g標誌並在調試器中運行它。這應該是非常明顯的。 – dmp
現在我的答案解釋了新的代碼錯誤。 – Chimera