C++文件读写(有关字符指针)
写一个单链表的程序,读文件时int 型数据都能正确读入,但字符指针不知该怎么读入?结构体定义和读文件的函数如下:
struct StuInfo
{
int nId;
char* strName;
int nSocre;
};
struct Student
{
StuInfo data;
Student* next;
};
void ReadFile(Student* head) // head 为头指针
{
char file[256];
cout<<"请输入文件路径:"<<endl;
cin>>file;
FILE* fp=fopen(file,"r");
if (!fp)
{
cout<<"打开文件失败!"<<endl;
return;
}
Student* newNode=new Student;
memset(&newNode,0,sizeof(Student));
while (!feof(fp)&&fread(&newNode->data,sizeof(StuInfo),1,fp)==1)//如果未读到文件尾且读取成功,则将数据插入链表
{
newNode->next=head->next;
head->next=newNode;
}
delete newNode;
fclose(fp);
}
哪位高手能指出该怎样解决,即能对char* 的字符指针赋值。