调试出错在strcpy的地方
第一次循环的时候没有出错
我在内层循环的结尾为buf重新申请了地址
不知道为什么会出错 心态崩了 求大神
char *strtok_r(char *s, const char *delim, char **save_ptr) {
char *token;
if (s == NULL) s = *save_ptr;
/* Scan leading delimiters. */
s += strspn(s, delim);
if (*s == '\0')
return NULL;
/* Find the end of the token. */
token = s;
s = strpbrk(token, delim);
if (s == NULL)
/* This token finishes the string. */
*save_ptr = strchr(token, '\0');
else {
/* Terminate the token and make *SAVE_PTR point past it. */
*s = '\0';
*save_ptr = s + 1;
}
return token;
}
char *strtok(char *s, const char *delim)
{
static char *last;
return strtok_r(s, delim, &last);
}
int main()
{
ifstream file("F://datastructure//studentimformation1.csv",ios::in);
Student *studentrecord=(Student *)malloc(sizeof(Student));
Student *pStudent=studentrecord;
Student *head=studentrecord;
studentrecord->next=NULL;
char *buf=new char[36];
char *p[20];
string s;
int i=0;
if (!file)
{
cout<<"open error!!"<<endl;
}
else
{
while(!file.eof())
{
while(getline(file,s)!=NULL)
{strcpy(buf,s.c_str());
int j=0;
while((p[j]=strtok(buf,","))!=NULL)
{
/*switch(i%6)
{
case 0: printf("number");break;
case 1: printf("name");break;
case 2:printf("sex");break;
case 3:printf("age");break;
case 4:printf("birthplace");break;
case 5:printf("GPA");break;
}
i++;*/
j++;
buf=NULL;
}
/*cout<<head->number<<endl;*/
strcpy(head->number,p[0]);
strcpy(head->name,p[1]);
strcpy(head->sex,p[2]);
strcpy(head->age,p[3]);
strcpy(head->birthplace,p[4]);
strcpy(head->GPA,p[5]);
head->next=new Student;
head=head->next;
head->next=NULL;
delete []buf;
char *buf=new char[36];
}
}
}
file.close();
delete []buf;
return 0;
}
好好学习一下局部变量作用域的知识。
http://blog.csdn.net/chosen0ne/article/details/8084388
你结构体里面的number什么的应该不是字符串类型的吧?那这些都不对
strcpy(head->number,p[0]);
strcpy(head->name,p[1]);
strcpy(head->sex,p[2]);
strcpy(head->age,p[3]);
strcpy(head->birthplace,p[4]);
strcpy(head->GPA,p[5]);
strcpy要求里面的两个参数都是字符指针类型的
strcpy里的两个东西要同一个类型