程序在 malloc 处崩溃
我今天正在寻求您的帮助,以获取我编写的一个程序,该程序用于读取文件中的问题及其答案。文件的组织方式如下:
This is question 1 ?;This is the answer of question 1.
This is question 2 ?;This is the answer of question 2.
我为完成此任务而编写的函数:
int LectureFichier(question *tab,const char *NomFichier)
{
FILE *fichier = NULL;
char c;
int count = 0;
int n = 0;
int m = 0;
char **ligne = (char**)malloc(sizeof(char*));
fichier = fopen(NomFichier, "r");
ligne[0] = (char*)malloc(sizeof(char));
while ((c = fgetc(fichier)) != EOF)
{
if (c == '\n')
{
printf("\nTest 35 : n = %d",n);
strcpy(tab[n].reponse,ligne[m]);
n++;
m++;
tab = realloc(tab,n);
printf("\nTest 36");
ligne = realloc(ligne,m);
printf("\nTest 37");
ligne[m] = (char*)malloc(sizeof(char));
printf("\nTest 38");
count = 0;
continue;
}
if (c == ';')
{
strcpy(tab[n].quest,ligne[m]);
m++;
printf("\n%s",tab[n].quest);
ligne = realloc(ligne,m);
ligne[m] = (char*)malloc(sizeof(char));
count = 0;
continue;
}
ligne[m][count] = c;
count ++;
ligne[m] = realloc(ligne[m],count);
}
free(ligne);
fclose(fichier);
return 1;
}
我在主函数中调用的函数:
int main()
{
int retval;
char *NomFichier;
question *QuestionsTab;
QuestionsTab = (question*)malloc(sizeof(question));
strcpy(NomFichier,"Test.txt");
retval = LectureFichier(QuestionsTab,NomFichier);
printf("Test 4");
主函数中有更多行,但没有必要在此处给出,因为程序在“测试 4”之前崩溃。
在终端中,显示的内容是:
Ceci est la question 1 ?[
Test 35 : n = 0
Test 36
Test 37
Process returned -1073740940 (0xC0000374) execution time : 1.162 s Press any key to continue.
它似乎在指令 ligne[m] = (char*)malloc(sizeof(char));
处中断,并且“[”打印为好吧,这不是我从代码中等待的。
我使用的是 Windows 10。
提前感谢您的阅读和帮助。
编辑:这是我正在使用的结构:
typedef struct Question
{
char *quest;
char *reponse;
int NumeroQuestion;
}question;
I am seeking your help today for a program I wrote to read a question and its answer on a file. The file is organised like this :
This is question 1 ?;This is the answer of question 1.
This is question 2 ?;This is the answer of question 2.
and the function I wrote to accomplish this task :
int LectureFichier(question *tab,const char *NomFichier)
{
FILE *fichier = NULL;
char c;
int count = 0;
int n = 0;
int m = 0;
char **ligne = (char**)malloc(sizeof(char*));
fichier = fopen(NomFichier, "r");
ligne[0] = (char*)malloc(sizeof(char));
while ((c = fgetc(fichier)) != EOF)
{
if (c == '\n')
{
printf("\nTest 35 : n = %d",n);
strcpy(tab[n].reponse,ligne[m]);
n++;
m++;
tab = realloc(tab,n);
printf("\nTest 36");
ligne = realloc(ligne,m);
printf("\nTest 37");
ligne[m] = (char*)malloc(sizeof(char));
printf("\nTest 38");
count = 0;
continue;
}
if (c == ';')
{
strcpy(tab[n].quest,ligne[m]);
m++;
printf("\n%s",tab[n].quest);
ligne = realloc(ligne,m);
ligne[m] = (char*)malloc(sizeof(char));
count = 0;
continue;
}
ligne[m][count] = c;
count ++;
ligne[m] = realloc(ligne[m],count);
}
free(ligne);
fclose(fichier);
return 1;
}
Which I call in my main function :
int main()
{
int retval;
char *NomFichier;
question *QuestionsTab;
QuestionsTab = (question*)malloc(sizeof(question));
strcpy(NomFichier,"Test.txt");
retval = LectureFichier(QuestionsTab,NomFichier);
printf("Test 4");
There is more line in the main function but it is not necessary to give here as the program crash before "Test 4".
In the terminal, there is what is displayed :
Ceci est la question 1 ?[
Test 35 : n = 0
Test 36
Test 37
Process returned -1073740940 (0xC0000374) execution time : 1.162 s Press any key to continue.
It seems to interrupt at the instruction ligne[m] = (char*)malloc(sizeof(char));
, and a "[" is printed as well and it is not what I wait from the code.
I am using Windows 10.
Thanks in advance for reading and for your help.
Edit : here is the structure I am using :
typedef struct Question
{
char *quest;
char *reponse;
int NumeroQuestion;
}question;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论