【发布时间】:2018-10-13 11:28:27
【问题描述】:
我需要读取一个文件并将文件中的数据存储到一个结构中。文件的第一行包含我必须动态分配的结构数组的大小。
4
12/04/2010
Interview went well I think, though was told to wear shoes.
18/04/2010
Doc advised me to concentrate on something... I forget.
03/05/2010
Was asked today if I was an art exhibit.
19/05/2010
Apparently mudcakes not made of mud, or angry wasps.
我要在 Windows 中完美运行我的代码,但是当我在 Unix 环境中运行时,它显示了分段错误(核心转储)。我确实使用valgrind 来检查内存泄漏,这就是结果
==4344== Invalid read of size 1
==4344== at 0x407F842: ____strtol_l_internal (strtol_l.c:298)
==4344== by 0x407F606: strtol (strtol.c:108)
==4344== by 0x407C87E: atoi (atoi.c:27)
==4344== by 0x8048837: main (in /home/admininistrator/ucp/p6/gg)
==4344== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==4344==
==4344==
==4344== Process terminating with default action of signal 11 (SIGSEGV)
==4344== Access not within mapped region at address 0x0
==4344== at 0x407F842: ____strtol_l_internal (strtol_l.c:298)
==4344== by 0x407F606: strtol (strtol.c:108)
==4344== by 0x407C87E: atoi (atoi.c:27)
==4344== by 0x8048837: main (in /home/admininistrator/ucp/p6/gg)
==4344== If you believe this happened as a result of a stack
==4344== overflow in your program's main thread (unlikely but
==4344== possible), you can try to increase the size of the
==4344== main thread stack using the --main-stacksize= flag.
==4344== The main thread stack size used in this run was 8388608.
==4344==
==4344== HEAP SUMMARY:
==4344== in use at exit: 1,396 bytes in 3 blocks
==4344== total heap usage: 3 allocs, 0 frees, 1,396 bytes allocated
==4344==
==4344== LEAK SUMMARY:
==4344== definitely lost: 0 bytes in 0 blocks
==4344== indirectly lost: 0 bytes in 0 blocks
==4344== possibly lost: 0 bytes in 0 blocks
==4344== still reachable: 1,396 bytes in 3 blocks
==4344== suppressed: 0 bytes in 0 blocks
==4344== Rerun with --leak-check=full to see details of leaked memory
==4344==
==4344== For counts of detected and suppressed errors, rerun with: -v
==4344== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
这是我的代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"struct.h"
int main(int argc, char* argv[])
{
if (argc < 2)
{
printf("You have enter less arguments.\n");
}
else if (argc > 2)
{
printf("You have enter too many arguments.");
}
else
{
FILE *file;
Diary *res;
Diary *res2;
char line[102];
int i, size, k, l, choice;
int day, month, year;
/* int d[10],m[10],y[10];*/
char as[102];
char* oken;
char* yoken;
char* coken;
oken = NULL;
yoken = NULL;
coken = NULL;
i = 0;
file = fopen("struct.txt", "r");
if (file == NULL)
{
perror("Error opening file\n.");
}
else
{
fscanf(file, "%d", &size);
res = (Diary*) malloc(size * sizeof(Diary));
res2 = (Diary*) calloc((5), sizeof(Diary));
while (fgets(line, sizeof(line), file) != NULL)
{
oken = strtok(line, "/");
if (oken != NULL)
{
res2[i].day= atoi(oken);
coken = strtok(NULL, "/");
if (oken != NULL)
{
res2[i].month = atoi(coken);
yoken = strtok(NULL, "\n ");
if (coken != NULL)
{
/*printf("%s",yoken);*/
res2[i].year = atoi(yoken);
fgets(as, 102, file);
strncpy(res2[i].entry, as, 102);
}
}
}
i++;
}
k = 1;
l = 0;
while (l < size)
{
res[l].day = res2[k].day;
res[l].month = res2[k].month;
res[l].year = res2[k].year;
strncpy(res[l].entry, res2[k].entry, 102);
k++;
l++;
}
choice = atoi(argv[1]);
printf("%d-%02d-%02d:%s",res[choice].year, res[choice].month,res[choice].day,res[choice].entry);
free(res2);
free(res);
}
fclose(file);
}
return 0;
}
我需要将文件中的所有数据读取到结构中,并在用户需要输入时将其打印出来。我尝试逐部分调试,我发现它是部分while( fgets( line, sizeof( line ), file) != NULL) 那个循环,给出了问题。但我不知道如何解决它。
我的struct.h 如下:
typedef struct journal{
int day;
int month;
int year;
char entry[1024];
} Diary;
【问题讨论】:
-
从构建调试信息开始(添加
-g标志)。然后在调试器中运行以在发生崩溃时捕捉崩溃。将函数调用堆栈向上转到您的代码并检查所有涉及的变量及其值。 -
据我所知,这是两次拼写错误。您正在检查 oken !=NULL 两次。相反,您想检查 coken,然后检查 yoken。如果您正确命名变量,就不会发生这种情况
-
一个提示:一致性!您调用
strtok几次,分配给不同的变量。然后使用 other 变量而不是您从strok调用中分配的变量。就像你有yoken = strtok(...)然后检查if (coken != NULL)。 -
您显然在这个问题上付出了一些努力,我对此表示赞赏。但是你的代码格式真的是一团糟……
-
这不符合minimal reproducible example 的标准。你试图提供足够的信息,这很好,但现在你应该学习一点如何缩小问题的范围。第一条评论已经对您有所帮助(使用
-g获得调试信息)。您可能还想阅读how to debug small programs,只是为了获得一些好主意。
标签: c unix segmentation-fault valgrind