我正在撰写一份应该执行以下操作的程序
-open file
-read file
-tokenize file,getting name,course,grade tokens
-dynamiclly add these tokens to an array of structures
该文件具有以下格式
Khai,IE 3301,69
Ashley,MATH 1426,59
Alisaad,CSE 1325,31
August,CSE 1325,55
Ethan,CSE 1320,92
但我继续得到一个我不理解的分段错误,我看了索引,他们似乎没有超出界限,所以我不知道最新情况如何。以下是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct info {
char* student;
char* courseName;
int grade;
};
int main(void)
{
char buffer[100];
struct info **strarray = NULL; /*struct of Arrays*/
char* token;
char* studen = (char *) malloc(15);
char* coursename = (char *) malloc(10);
int grad = malloc(sizeof(int)),count = 0,index = 0;
char* del = ",";
FILE* fp = fopen("input-hw05a.csv","r");
while(fgets(buffer,sizeof(buffer),fp) != NULL)
{
token = strtok(buffer,del);
studen = token;
while(token != NULL)
{
if(count == 1)
coursename = token;
if(count ==2)
grad = atoi(token);
token = strtok(NULL,del);
count = count + 1;
}
/*add ONE element to the array*/
strarray = (struct info **)realloc(strarray,(index + 1) * sizeof(struct info *));
/*allocate memory for struct info*/
strarray[index] = (struct info *)malloc(sizeof(struct info));
/*copy data inso structure array*/
strcpy(strarray[index]->student,studen);
strcpy(strarray[index]->courseName,coursename);
strarray[index]->grade = grad;
index = index + 1;
}
int i = 0;
for(i = 0; i < index; i++)
{
printf("%s %s %d\n",strarray[i]->student,strarray[index]->courseName,strarray[index]->grade);
}
}
答案 0 :(得分:2)
嗯,您的代码中可能存在多个malloc错误。我在评论中提到过。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct info {
char* student;
char* courseName;
int grade;
};
int main()
{
char buffer[100];
struct info **strarray = NULL; /*struct of Arrays*/
char* token;
char* studen = (char *) malloc(15);
char* coursename = (char *) malloc(10);
int grad, count = 0,index = 0;
char* del = ",";
FILE* fp = fopen("input-hw05a.csv", "r");
while(fgets(buffer,sizeof(buffer),fp) != NULL)
{
token = strtok(buffer,del);
studen = token;
count = 0; /* You missed it. Otherwise, count will never be 1 and 2 again when control enters the loop second time */
while(token != NULL)
{
if(count == 1)
coursename = token;
if(count ==2)
grad = atoi(token);
token = strtok(NULL,del);
count = count + 1;
}
/*add ONE element to the array*/
strarray = (struct info **)realloc(strarray,(count + 1) * sizeof(struct info *));
/*allocate memory for struct info*/
strarray[index] = (struct info *)malloc(sizeof(struct info));
/*copy data inso structure array*/
strarray[index]->student = (char *)malloc(strlen(studen)*sizeof(char)); //you need to allocate memory for student, to do a string copy
strcpy(strarray[index]->student,studen);
strarray[index]->courseName = (char *)malloc(strlen(coursename)*sizeof(char)); // similarly, you need to allocate memory for coursename, to do a string copy
strcpy(strarray[index]->courseName,coursename);
strarray[index]->grade = grad;
index = index + 1;
}
int i = 0;
for(i = 0; i < index; i++)
{
printf("%s %s %d\n",strarray[i]->student,strarray[i]->courseName,strarray[i]->grade); //In your code it was strarray[index]->courseName & strarray[index]->grade
}
return 0;
}
答案 1 :(得分:0)
如注释中所述,您必须在结构中使用malloc memory for student和courseName。我还删除了不必要的mallocs
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct info {
char* student;
char* courseName;
int grade;
};
int main(void)
{
char buffer[100];
struct info **strarray = NULL; /*struct of Arrays*/
char* token;
char* studen;
char* coursename;
int grad,count = 0,index = 0;
char* del = ",";
FILE* fp = fopen("input-hw05a.csv","r");
while(fgets(buffer,sizeof(buffer),fp) != NULL)
{
token = strtok(buffer,del);
studen = token;
while(token != NULL)
{
if(count == 1)
coursename = token;
if(count ==2)
grad = atoi(token);
token = strtok(NULL,del);
count++;
}
/*add ONE element to the array*/
strarray = realloc(strarray,(index + 1) * sizeof(struct info *));
/*allocate memory for struct info*/
strarray[index] = malloc(sizeof(struct info));
/*copy data inso structure array*/
strarray[index]->student = malloc(strlen(studen)+1);
strcpy(strarray[index]->student,studen);
strarray[index]->courseName = malloc(strlen(coursename)+1);
strcpy(strarray[index]->courseName,coursename);
strarray[index]->grade = grad;
index++;
}
int i = 0;
for(i = 0; i < index; i++)
{
printf("%s %s %d\n",strarray[i]->student,strarray[index]->courseName,strarray[index]->grade);
}
//free all malloc memory
return 0;
}
答案 2 :(得分:0)
上述代码对我不起作用。
需要更改以下内容:
1。内部while循环后添加count = 0,这是为了重置计数,以便它能够正确读取和存储数据。 数据存储在代码中的方式如下:
开府
IE 3301
69
阿什利
Alisaad
八月
探
由于计数永远不会回到0,所以它不会再次变为1或2 ..所以只显示名称。
2。在显示数据时,它使用strarray [index] - &gt; courseName,strarray [index] - &gt; grade。由于索引总是在最后递增。它总是指向将在下一循环中分配的东西。因此,在最后一个循环中,它指向不存在的内存并最终出现在分段错误中。