将文件读入结构体

尘埃092 2012-08-31 03:19:55

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <conio.h>
#include <iostream>
#include <conio.h>
#include <windows.h>
typedef struct student
{
char name[30];
char major[30];
char sex[20];
int age;
struct student * next;
}*STU, STUS;
void load( STU & head );
int main()
{

STU p, head;
head = NULL;
load( head );
p = head;
while( p != NULL )
{
printf( "%s", p->name );
p = p->next;
}
getchar();
return 0;
}
void load( STU & head )
{
STU p, r;
FILE * f;
p = head;
f = fopen( "1.txt", "rb" );
while( !feof(f) )
{
r = (STU)malloc(sizeof(STUS));
fscanf( f, "%s", r->name );
r->next = NULL;
p = r;
p = p->next;
}
fclose(f);
}

请问有什么问题吗?
...全文
157 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
gfzeng 2012-08-31
  • 打赏
  • 举报
回复
C++ 这种引用形参方式有点让我头痛, ^_^。
gfzeng 2012-08-31
  • 打赏
  • 举报
回复
这个程序有问题, head一直是NULL
最终失控
尘埃092 2012-08-31
  • 打赏
  • 举报
回复

//函数主菜单采用递归调用所以在选择次数上存在缺陷
//如果使用菜单次数过多可能会造成栈溢出,如需解决此问题只需用数组模拟函数入栈即可
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <conio.h>
#include <graphics.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
typedef struct student
{
char name[30];
char major[30];
char sex[20];
int age;
struct student * next;
}*STU, STUS;
void menu( STU &head );
void create(STU &head);
void del( STU &head );
void view( STU head );
void find( STU head );
void calculate();
//--------------------------
void game(); //小游戏函数
void welcome();
void goodbye( int score );
//---------------------------
void load( STU &head );
int save( STU &head );
using namespace std;
int main()
{
STU head = NULL;
load( head );
system( "color 2e" );
menu( head );
return 0;
}
void menu( STU &head )
{
int choice;
printf( "欢迎使用多功能校友录!!\n" );
printf( "\t\t━━━━━━━━━━━━━━━━━━━━━━━━━\n" );
printf( "\t\t┃1.添加好友 2.删除好友 ┃\n");
printf( "\t\t┃3.查看好友列表 4.按姓名索引 ┃\n");
printf( "\t\t┃5.计算器 6.小游戏 ┃\n");
printf( "\t\t┃7.退出并保存 ┃\n");
printf( "\t\t┃ 联系我们:www.zzulion.com┃\n");
printf( "\t\t━━━━━━━━━━━━━━━━━━━━━━━━━\n");
printf( "\t\t请选择一个选项(1-8)\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf( "\t\t\t\t关于我们:郑州大学南校区11软工3班409寝室01小组\n" );
printf( "\t\t\t\t\t\t\t王诗阳,邓博文,闫世华\n" );
choice = getch();
switch( choice )
{
case 49:
fflush(stdin);
system( "cls" );
create( head );
system( "cls" );
menu( head );
break;
case 50:
fflush(stdin);
system( "cls" );
del( head );
system( "cls" );
menu( head );
break;
case 51:
fflush(stdin);
system( "cls" );
view( head );
system( "cls" );
menu( head );
break;
case 52:
fflush(stdin);
system( "cls" );
find( head );
system( "cls" );
menu( head );
break;
case 53:
fflush(stdin);
system( "cls" );
calculate();
menu( head );
break;
case 54:
fflush(stdin);
system( "cls" );
game();
menu( head );
break;
case 55:
if( save( head ) )
printf( "文件保存成功,按任意键退出" );
else
printf( "文件保存失败,按任意键退出" );
getch();
exit( 0 );
break;
default:
fflush(stdin);
menu( head );
break;
}
}
void del( STU &head )
{
STU p, q;
p = q = head;
char name[30];
printf( "请输入要删除的姓名:" );
fgets(name, 30, stdin);
for( int i = 0; i < strlen(name); i++ )
if( name[i] == '\n' )
name[i] = '\0';
while( p != NULL )
{
if( !strcmp(p->name, name) )
{
if( p == head ) //如果删除的是表头
{
head = p->next;
free(p);
}
else
{
q->next = p->next;
free(p);
}
system( "cls" );
printf( "删除成功" );
Sleep( 2000 );
return;
}
q = p;
p = p->next;
}
system( "cls" );
printf( "无此人,删除失败\n" );
Sleep( 2000 );
return;
}
void create( STU &head )
{
char name[30];
STU r, p, q;
p = q = head;
printf( "请输入姓名:" );
fgets(name, 30, stdin);
for( int i = 0; i < strlen(name); i++ )
if( name[i] == '\n' )
name[i] = '\0';
while( p != NULL )
{
if( !strcmp(p->name, name) )
{
system( "cls" );
printf( "此人已存在!\n" );
Sleep(2000);
return;
}
else
{ q = p; p = p->next;} //走链
}
r = (STU)malloc( sizeof(STUS) );
r->next = NULL;
if( r == NULL )
{
system( "cls" );
printf( "分配失败!\n" );
Sleep(1000);
return;
}
strcpy( r->name, name );
printf( "请输入性别:" );
scanf( "%s", r->sex );
printf( "请输入年龄:" );
while( !scanf( "%d", &r->age ) )
{
r->age = 0;
printf( "非法输入,请重试\n");
printf( "请输入年龄:" );
}
getchar();
printf( "请输入专业:" );
fgets(r->major, 10, stdin);
for( i = 0; i < strlen(r->major); i++ )
if( *(r->major+i) == '\n' )
*(r->major+i) = '\0';
if( q == NULL )
head = r;
else
q->next = r;
system( "cls" );
printf( "创建成功!\n" );
Sleep( 1000 );
return ;
}
void find( STU head )
{
char name[30];
STU p;
p = head;
printf( "请输入要查找的姓名:" );
fgets(name, 30, stdin);
for( int i = 0; i < strlen(name); i++ )
if( name[i] == '\n' )
name[i] = '\0';
while( p!= NULL )
{
if( !strcmp(p->name, name) )
{
printf( "姓名\t性别\t年龄\t\t专业\t\t\n" );
while( p != NULL )
{
printf("%-10s%-10s%-10d%-10s\n", p->name, p->sex, p->age, p->major );
p = p->next;
}
Sleep( 2000 );
return ;
}
p = p->next;
}
system( "cls" );
printf( "没有这个人!!\n" );
Sleep( 2000 );
return ;
}
void view( STU head )
{
STU p;
p = head;
system( "cls" );
printf( "姓名\t性别\t年龄\t专业\t\t\n" );
while( p != NULL )
{
printf("%-10s%-10s%-10d%-10s\n", p->name, p->sex, p->age, p->major );
p = p->next;
}
getch();
return ;
}
void calculate()
{
system( "cls" );
printf( "sorry! we haven't open it\n" );
Sleep( 2000 );
return ;
}
int save( STU &head )
{
char a = '\n';
STU p = head;
FILE *f;
if( f = fopen( "1.txt", "a+" ) )
{
while( p != NULL )
{
fprintf( f, "%-10s%-10s%-10d%-10s\n", p->name, p->sex, p->age, p->major );
p = p->next;
}
fclose(f);
return 1;
}
else
{
printf( "创建文件失败!!" );
return 0;
}
}
void load( STU &head )
{
STU p, r;
p = head;
FILE *f;
char c;
if( (f = fopen( "1.txt", "rb" ) ) != NULL )
{
while( !feof(f) )
{
r = (STU)malloc( sizeof(STUS) );
fscanf( f, "%s", r->name );
fscanf( f, "%s", r->sex );
fscanf( f, "%d", &r->age );
fscanf( f, "%s", r->major);
r->next = NULL;
if( p != NULL )
p->next = r;
if( head == NULL )
head = r;
p = r;
}
fclose(f);
}
}
void welcome()
{
// 输出屏幕提示
cleardevice();
setcolor(YELLOW);
setfont( 64, 0, "黑体");
outtextxy(160, 50, "打字母游戏");
setcolor(WHITE);
setfont(16, 0, "宋体");
outtextxy(100, 200, "玩法:随机掉字母然后,按相应键就消失的游戏即得分");

// 实现闪烁的“按任意键继续”
int c=255;
while(!kbhit())
{
setcolor(RGB(c, 0, 0));
outtextxy(280, 400, "按任意键继续");
c -= 8;
if (c < 0) c = 255;
Sleep(20);
}
getch();
cleardevice();
}

// 退出界面
void goodbye( int score )
{
char s[20];
cleardevice();
setcolor(YELLOW);
setfont(20, 0, "黑体");
itoa( score, s, 10 );
outtextxy(0, 0, "您的分数\n" );
outtextxy(20, 20, s );
outtextxy(104, 180, "郑州大学南校区409寝室1小组");
outtextxy(104, 200, "王诗阳,邓博文,闫世华");
getch();
closegraph();
}

// 主函数
void game()
{
initgraph(640, 480); // 初始化屏幕为 640x480

welcome(); // 显示欢迎界面

srand(time(NULL)); // 设置随机种子
setfont(30, 0, "Arial"); // 设置字母的字体和大小
setfillstyle(BLACK); // 设置清除字母的填充区域颜色

char target; // 目标字母
char key; // 用户的按键
int x, y; // 字母的位置
int score = 0;
// 主循环
while(true)
{
target = 65 + rand() % 26; // 产生任意大写字母
x = rand() % 620; // 产生任意下落位置
for (y=0; y<460; y++ ) //边界为460
{
setcolor(WHITE); // 设置字母的颜色
outtextxy(x, y, target); // 显示字母

if(kbhit())
{
key = getch(); // 获取用户按键

if((key == target) || (key == target + 32))
{
bar(x, y, x + 30, y + 30); // 按键正确,击落字母
score += 10;
break;
}
else if (key == 27)
{
goto EXIT; //ESC退出
}
}
Sleep( 10 );
bar(x, y, x + 30, y + 30); //清除字母
}
}

EXIT:
goodbye( score );
}


这是我写好的东西,就最后文件存取和读入的函数有问题,求解答
Gonefar 2012-08-31
  • 打赏
  • 举报
回复
fscanf遇到空格和换行时结束,注意空格时也结束。这与fgets有区别,fgets遇到空格不结束。
不知道lz有没有注意到这个。
Gonefar 2012-08-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
1. 你没有为 head 赋值, 它一直是 NULL.
2. 你的 next 指针没有串起来.

C/C++ code

while( !feof(f) )
{
r = (STU)malloc(sizeof(STUS));
fscanf( f, "%s", r->name );
r->next = NULL;

……
[/Quote]
没有什么问题。LZ都考虑了。LZ把具体问题贴出来吧。。
www_adintr_com 2012-08-31
  • 打赏
  • 举报
回复
1. 你没有为 head 赋值, 它一直是 NULL.
2. 你的 next 指针没有串起来.

while( !feof(f) )
{
r = (STU)malloc(sizeof(STUS));
fscanf( f, "%s", r->name );
r->next = NULL;

if(p != NULL)
p->next = r;

if(head == NULL)
head = r;

p = r;
}
Gonefar 2012-08-31
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

fscanf遇到空格和换行时结束,注意空格时也结束。这与fgets有区别,fgets遇到空格不结束。
不知道lz有没有注意到这个。
[/Quote]
读取文件时注意到了吗!!!!
proorck6 2012-08-31
  • 打赏
  • 举报
回复
自己动手,丰衣足食。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧