#include<fstream>
using namespace std;
struct node
{
char name[20];
char num[20];
node *pnext;
}
node * create(string filename);
void main()
{
create("abc");
}
node * create(string filename)
{
node *head=NULL,*p1,*p2;
ifstream fin(filename);
if(fin.is_open())
{
cout<<"读文件失败!\n";
return NULL;
}
char num[20],name[20];
while(fin>>num>>name)
{
node *s=new node;
s.num[0]=num;//这里怎么赋值啊
s.name[0]=name//这里怎么赋值啊
s->pnext=NULL;
if(head==NULL)
{
head=s;
p1=s;
}
else
{
p1->pnext=s;
p1=s;
}
}
return head;
}
12 个解决方案
#1
strcpy啊
#2
#include<iostream>
#include<fstream>
using namespace std;
struct node
{
char name[20];
char num[20];
node *pnext;
}
node * create(string filename);
void main()
{
create("abc");
}
node * create(string filename)
{
node *head=NULL,*p1,*p2;
ifstream fin(filename);
if(fin.is_open())
{
cout<<"读文件失败!\n";
return NULL;
}
char num[20],name[20];
while(fin>>num>>name)
{
node *s=new node;
strcpy(s.num,num);
strcpy(s.name,name);
s->pnext=NULL;
if(head==NULL)
{
head=s;
p1=s;
}
else
{
p1->pnext=s;
p1=s;
}
}
return head;
}
为什么我改成这样,老提示我miss ; before }呢
#3
结构体定义后面的大括号要加个分号
struct node
{
char name[20];
char num[20];
node *pnext;
} ;
struct node
{
char name[20];
char num[20];
node *pnext;
} ;
#4
#include<iostream>
#include<fstream>
using namespace std;
struct node
{
char name[20];
char num[20];
node *pnext;
};
node * create(string filename);
void main()
{
create("abc");
}
node * create(string filename)
{
node *head=NULL,*p1,*p2;
ifstream fin(filename);
if(!fin.is_open())
{
cout<<"读文件失败!\n";
return NULL;
}
char num[20],name[20];
while(fin>>num>>name)
{
node *s=new node;
strcpy(s->num,num);
strcpy(s->name,name);
s->pnext=NULL;
if(head==NULL)
{
head=s;
p1=s;
}
else
{
p1->pnext=s;
p1=s;
}
}
return head;
}
这是我改进后的 仍有问题啊
#5
++
别动不动自杀 对生命的漠视
鬼门关走一遭 你就知道多幼稚了
#6
我是个新手,希望大家对我有点耐心
#7
ifstream fin(filename.c_str());
#8
我就为了让你们多关注我一下
#9
我先给你默哀一分钟啊,楼主明天别爽约。
#10
#include<fstream>
using namespace std;
struct node
{
char name[20];
char num[20];
node *pnext;
};
node * create(string filename);
void main()
{
create("abc");
}
node * create(string filename)
{
node *head=NULL,*p1,*p2;
ifstream fin(filename.c_str());
if(fin.is_open())
{
cout<<"读文件失败!\n";
return NULL;
}
char num[20],name[20];
while(fin>>num>>name)
{
node *s=new node;
strcpy(s->num,num); //
strcpy(s->name,name);//strcpy(s.name,name);
s->pnext=NULL;
if(head==NULL)
{
head=s;
p1=s;
}
else
{
p1->pnext=s;
p1=s;
}
}
return head;
}
#11
node *s=new node;
这条语句放while循环里干什么?每次循环都new一次?
你定义的*P2倒没见你用过
这条语句放while循环里干什么?每次循环都new一次?
你定义的*P2倒没见你用过
#12
#include<fstream>
#include <iostream>
#include <cstring>
using namespace std;
struct node
{
char name[20];
char num[20];
node *pnext;
};
node * create(string filename);
int main()
{
create("abc");
}
node * create(string filename)
{
node *head=NULL,*p1,*p2;
ifstream fin(filename.c_str());
if(!fin.is_open()) // 这里条件写反了
{
cout<<"读文件失败!\n";
return NULL;
}
char num[20],name[20];
while(fin>>num>>name)
{
node *s=new node;
strncpy(s->num,num, sizeof(s->num)); // strncpy更安全
strncpy(s->name,name, sizeof(s->name));
s->pnext=NULL;
if(head==NULL)
{
head=s;
p1=s;
}
else
{
p1->pnext=s;
p1=s;
}
}
return head;
}
既然你用c++了,其实可以用string类代替,更简单。稍微改进你的代码:
#include<fstream>
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
struct node
{
string name;
string num;
node *pnext;
};
node * create(string filename);
int main()
{
create("abc");
}
node * create(string filename)
{
node *head=NULL,*p1,*p2;
ifstream fin(filename.c_str());
if(!fin.is_open()) // 这里条件写反了
{
cout<<"读文件失败!\n";
return NULL;
}
string num, name;
while(fin>>num>>name)
{
node *s=new node;
s->num = num;
s->name = name;
s->pnext=NULL;
if(head==NULL)
{
head=s;
p1=s;
}
else
{
p1->pnext=s;
p1=s;
}
}
return head;
}
#1
strcpy啊
#2
#include<iostream>
#include<fstream>
using namespace std;
struct node
{
char name[20];
char num[20];
node *pnext;
}
node * create(string filename);
void main()
{
create("abc");
}
node * create(string filename)
{
node *head=NULL,*p1,*p2;
ifstream fin(filename);
if(fin.is_open())
{
cout<<"读文件失败!\n";
return NULL;
}
char num[20],name[20];
while(fin>>num>>name)
{
node *s=new node;
strcpy(s.num,num);
strcpy(s.name,name);
s->pnext=NULL;
if(head==NULL)
{
head=s;
p1=s;
}
else
{
p1->pnext=s;
p1=s;
}
}
return head;
}
为什么我改成这样,老提示我miss ; before }呢
#3
结构体定义后面的大括号要加个分号
struct node
{
char name[20];
char num[20];
node *pnext;
} ;
struct node
{
char name[20];
char num[20];
node *pnext;
} ;
#4
#include<iostream>
#include<fstream>
using namespace std;
struct node
{
char name[20];
char num[20];
node *pnext;
};
node * create(string filename);
void main()
{
create("abc");
}
node * create(string filename)
{
node *head=NULL,*p1,*p2;
ifstream fin(filename);
if(!fin.is_open())
{
cout<<"读文件失败!\n";
return NULL;
}
char num[20],name[20];
while(fin>>num>>name)
{
node *s=new node;
strcpy(s->num,num);
strcpy(s->name,name);
s->pnext=NULL;
if(head==NULL)
{
head=s;
p1=s;
}
else
{
p1->pnext=s;
p1=s;
}
}
return head;
}
这是我改进后的 仍有问题啊
#5
++
别动不动自杀 对生命的漠视
鬼门关走一遭 你就知道多幼稚了
#6
我是个新手,希望大家对我有点耐心
#7
ifstream fin(filename.c_str());
#8
我就为了让你们多关注我一下
#9
我先给你默哀一分钟啊,楼主明天别爽约。
#10
#include<fstream>
using namespace std;
struct node
{
char name[20];
char num[20];
node *pnext;
};
node * create(string filename);
void main()
{
create("abc");
}
node * create(string filename)
{
node *head=NULL,*p1,*p2;
ifstream fin(filename.c_str());
if(fin.is_open())
{
cout<<"读文件失败!\n";
return NULL;
}
char num[20],name[20];
while(fin>>num>>name)
{
node *s=new node;
strcpy(s->num,num); //
strcpy(s->name,name);//strcpy(s.name,name);
s->pnext=NULL;
if(head==NULL)
{
head=s;
p1=s;
}
else
{
p1->pnext=s;
p1=s;
}
}
return head;
}
#11
node *s=new node;
这条语句放while循环里干什么?每次循环都new一次?
你定义的*P2倒没见你用过
这条语句放while循环里干什么?每次循环都new一次?
你定义的*P2倒没见你用过
#12
#include<fstream>
#include <iostream>
#include <cstring>
using namespace std;
struct node
{
char name[20];
char num[20];
node *pnext;
};
node * create(string filename);
int main()
{
create("abc");
}
node * create(string filename)
{
node *head=NULL,*p1,*p2;
ifstream fin(filename.c_str());
if(!fin.is_open()) // 这里条件写反了
{
cout<<"读文件失败!\n";
return NULL;
}
char num[20],name[20];
while(fin>>num>>name)
{
node *s=new node;
strncpy(s->num,num, sizeof(s->num)); // strncpy更安全
strncpy(s->name,name, sizeof(s->name));
s->pnext=NULL;
if(head==NULL)
{
head=s;
p1=s;
}
else
{
p1->pnext=s;
p1=s;
}
}
return head;
}
既然你用c++了,其实可以用string类代替,更简单。稍微改进你的代码:
#include<fstream>
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
struct node
{
string name;
string num;
node *pnext;
};
node * create(string filename);
int main()
{
create("abc");
}
node * create(string filename)
{
node *head=NULL,*p1,*p2;
ifstream fin(filename.c_str());
if(!fin.is_open()) // 这里条件写反了
{
cout<<"读文件失败!\n";
return NULL;
}
string num, name;
while(fin>>num>>name)
{
node *s=new node;
s->num = num;
s->name = name;
s->pnext=NULL;
if(head==NULL)
{
head=s;
p1=s;
}
else
{
p1->pnext=s;
p1=s;
}
}
return head;
}