| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2099 人关注过本帖
标题:新人求助~~~~关于 stream!=NULL的问题
只看楼主 加入收藏
yyrdldw
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-6-22
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
新人求助~~~~关于 stream!=NULL的问题
图片附件: 游客没有浏览图片的权限,请 登录注册
     求解决呀~~~~~~
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <io.h>
#define NULL 0
#define LEN sizeof(struct employeeInfo)
#define FILE_DATA_PATH "c:\employeeInfo.txt"
 
struct employeeInfo   
{
 long int num;
 char name[20];
 int age;
 char sex[4];
 char eduLevel[10];
 char address[30];
 long int tel[11];
 char salary[20];
 struct employeeInfo *next;
};

int TOTAL_NUM = 0;
struct employeeInfo *head = NULL;

void welcome();
void mainmenu();
void record();
void insert(struct employeeInfo *stu);
void display(struct employeeInfo *stu);
void displayAll();
void query();
void query_by_num();
void query_by_name();
void readData();
void writeData();
void freeAll();
void del();
void change();
void devise(struct employeeInfo *p);
void exit();

int main()
{
    char userName[9];
    char userPWD[7];
    int i;
    welcome();
    for(i = 0; i < 3; i++)
    {
        printf("\n管理员初始用户名和密码均为admin\n");
        printf("请输入您的用户名:");
        scanf("%s", userName);
        printf("\n请输入您的密码:");
        scanf("%s", userPWD);
        if ((strcmp(userName,"admin")==0) && (strcmp(userPWD,"admin")==0))
        {
            //用户名和密码正确,显示主菜单
            mainmenu();
   break;
        }
        else
        {
            if (i < 2)
            {
                //用户名或密码错误,提示用户重新输入
                printf("用户名或密码错误,请重新输入!");
            }
            else
            {
                //连续3次输错用户名或密码,退出系统。
                printf("您已连续3次将用户名或密码输错,系统将退出!");
            }
        }
    }
 return 0;
}
//显示欢迎信息
void welcome()
{
    printf("+---------------------------+\n");
    printf("|                           |\n");
    printf("|  欢迎使用职工信息管理系统 |\n");
    printf("|  制作人:** ** **  |");
    printf("|                           |\n");
    printf("+---------------------------+\n");
}

//系统主菜单
void mainmenu()
{
    int choice;
    choice = -1;
    readData();
 printf("\n温馨提示:为保证您的操作得到保存,请按正常顺序退出系统^_^\n");
    do
    {
        printf("\n\n\n");
        printf("\t\t\t--------------------------------\n");
  printf("\t\t\t+        职工信息管理系统      |\n");  
  printf("\t\t\t--------------------------------\n");               
  printf("\t\t\t+    [1]----录入职工信息       |\n");
  printf("\t\t\t+    [2]----浏览职工信息       |\n");
     printf("\t\t\t+    [3]----查询职工信息       |\n");
  printf("\t\t\t+    [4]----删除职工信息       |\n");
  printf("\t\t\t+    [5]----修改职工信息       |\n");
  printf("\t\t\t+    [0]----退出系统           |\n");
  printf("\t\t\t+*·*·*·*·*·*·*·*·*·*·|\n");
  printf("\t\t\t--------------------------------\n");
        printf("请输入您的选择:");
        scanf("%d", &choice);
        switch(choice)
        {
            case 0:
    writeData();
    freeAll();
                exit( );
        case 1:
                record();
                break;
        case 2:
                displayAll();
                break;
        case 3:
                query();
                break;  
     case 4:
                del();
                break;
     case 5:
                change();
                break;
            default:
                printf("\n无效选项!");
                break;
        }
    }
    while(choice != 0);
}
//录入职工信息
void record()
{
 struct employeeInfo *p0;   
 
 p0 = (struct employeeInfo *)malloc(LEN);
 printf("请输入职工的职工号:");
 scanf("%ld",&p0->num);   
 printf("请输入职工的姓名:");
 scanf("%s",p0->name);
 printf("请输入职工的年龄:");
 scanf("%d",&p0->age);
 printf("请输入职工的性别:");
 scanf("%s",p0->sex);
 printf("请输入职工的学历:");
 scanf("%s",p0->eduLevel);
 printf("请输入职工的地址:");
 scanf("%s",p0->address);
 printf("请输入职工的电话:");
 scanf("%ld",&p0->tel);
 printf("请输入职工的工资:");
 scanf("%s",p0->salary);
   
 insert(p0);
 printf("该职工的信息为:\n");
 printf("-------------------------------------------------------------------------------");
 printf("职工号\t姓名\t年龄\t性别\t学历\t\t地址\t电话\t工资\n");
 display(p0);
}

void insert(struct employeeInfo *stu)
{
 struct employeeInfo *p0, *p1, *p2;
 
 p1 = head;
 p0 = stu;
 
 if(head == NULL)
 {
  head = p0;
  p0->next = NULL;
 }
 else
 {
  while((p0->num > p1->num)&&(p1->next != NULL))
  {
      p2 = p1;
   p1 = p1->next;
  }
  if(p0->num <= p1->num)
  {
   if(head == p1)
   head = p0;
   else
   p2->next = p0;
   p0->next = p1;
  }
  else
  {
   p1->next = p0;
   p0->next = NULL;
  }  
 }
 TOTAL_NUM++;
}

void display(struct employeeInfo *p)
{
 printf("%ld\t%s\t%d\t%s\t%s\t%s\t%ld\t%s\n", p->num, p->name, p->age, p->sex, p->eduLevel, p->address, p->tel, p->salary);
}
//浏览职工信息
void displayAll()
{
 struct employeeInfo *p;

 printf("职工总数:%d\n", TOTAL_NUM);
 p = head;
 if(head != NULL)
 {
  printf("\n职工号\t姓名\t年龄\t性别\t学历\t地址\t电话\t工资\n");
  printf("-------------------------------------------------------------------------------");
  do
  {
   display(p);
   p = p->next;
  }
  while(p != NULL);
 }
 printf("\n");
}
//查询职工信息
void query()
{
 int choice;
    choice = -1;
 do
    {
        printf("\n");
        printf("+--------------------+\n");
        printf("| 按职工号查询  请按 1 |\n");
        printf("| 按姓名查询  请按 2 |\n");        
  printf("| 取消        请按 0 |\n");
        printf("+--------------------+\n");

        printf("请输入您的选择:");
        scanf("%d", &choice);
      
        switch(choice)
        {
            case 0:
                return;
            case 1:
                query_by_num();
                break;
            case 2:
                query_by_name();
                break;           
            default:
                printf("\n无效选项!");
                break;
        }
    }
    while(choice != 0);
}
//按职工号查询职工信息
void query_by_num()
{
 int num;
 struct employeeInfo *p1;

 printf("请输入职工的职工号:");
 scanf("%ld", &num);
 if(head==NULL)
 {
  printf("无职工记录!\n");
  return;
 }
 p1=head;
 while(num!=p1->num && p1->next!=NULL)
  p1=p1->next;
 if(num == p1->num)
 {
  printf("\n职工号\t姓名\t年龄\t性别\t学历\t地址\t电话\t工资\n");
  printf("-------------------------------------------------------------------------------");
  display(p1);
 }
 else
  printf("没有该职工记录,请核对!");
}
//按姓名查询职工信息
void query_by_name()
{
 char name[20];
 struct employeeInfo *p1;

 printf("请输入职工的姓名:");
 scanf("%s", name);
 if(head==NULL)
 {
  printf("无职工记录!\n");
  return;
 }   
 p1=head;
 while(strcmp(name, p1->name) && p1->next!=NULL)
  p1=p1->next;
 if(!strcmp(name, p1->name))
 {

  printf("\n职工号\t姓名\t年龄\t性别\t学历\t地址\t电话\t工资\n");
  printf("-------------------------------------------------------------------------------");
  display(p1);
 }
 else
  printf("没有该职工记录,请核对!");
}
//写入文件
void writeData()
{
 FILE*  fp;//文件指针
 struct employeeInfo *p;

    fp = fopen(FILE_DATA_PATH, "w");
    if (!fp)
    {
  printf("文件打开错误!");
      
    }

 fprintf(fp,"%d\n", TOTAL_NUM);
    for(p = head; p!= NULL; p= p->next)
    {
        fprintf(fp,"%ld\t%s\t%d\t%s\t%s\t%s\t%ld\t%s\n",
            p->num, p->name, p->age, p->sex, p->eduLevel, p->address, p->tel, p->salary);
    }
    fclose(fp);
}

void freeAll()
{
 struct employeeInfo *p1, *p2;

 p1=p2=head;
 while(p1)
 {
  p2=p1->next;
  free(p1);
  p1=p2;
 }
}
//读取文件
void readData()
{
 FILE*  fp;//文件指针
 struct employeeInfo *p1, *p2;

    fp = fopen(FILE_DATA_PATH, "r");
    if (!fp)
    {
  printf("文件打开错误!");
      
    }
 
 fscanf(fp,"%d\n", &TOTAL_NUM);
 head = p1 = p2 = (struct employeeInfo *)malloc(LEN);
 fscanf(fp,"%ld\t%s\t%d\t%s\t%s\t%s\t%ld\t%s\n",
        &p1->num, p1->name, &p1->age, p1->sex, p1->eduLevel, p1->address, &p1->tel, p1->salary);
 while(!feof(fp))
 {
  p1 = (struct employeeInfo *)malloc(LEN);
  fscanf(fp,"%ld\t%s\t%d\t%s\t%s\t%s\t%ld\t%s\n",
           &p1->num, p1->name, &p1->age, p1->sex, p1->eduLevel, p1->address, &p1->tel, p1->salary);
  p2->next = p1;
  p2 = p1;
 }
 p2->next = NULL;
 fclose(fp);
}
//删除职工信息
void del()
{
 struct employeeInfo *p1, *p2;
 long int num;
 
 if(head == NULL)
 {
  printf("无职工记录!\n");
  return;
 }

 printf("请输入您要删除的职工的职工号:");
 scanf("%ld", &num);
 
 p1 = head;
 
 while(num != p1->num && p1->next != NULL)
 {
  p2 = p1;
  p1 = p1->next;  
 }
 if(num == p1->num)
 {
  if(p1 == head)
  head = p1->next;
  else p2->next = p1->next;
  free(p1);
  TOTAL_NUM--;
 }
 else
  printf("没有该职工记录,请核对!\n");
}
//修改职工信息
void change()
{
 struct employeeInfo *p1, *p2;
 long int num;
 
 if(head == NULL)
 {
  printf("无职工记录!\n");
  return;
 }

 printf("请输入您要修改的职工的职工号:");
 scanf("%ld", &num);
 
 p1 = head;
 
 while(num != p1->num && p1->next != NULL)
 {
  p2 = p1;
  p1 = p1->next;  
 }
 if(num == p1->num)
  devise(p1);
 else
  printf("没有该职工记录,请核对!\n");

}

void devise(struct employeeInfo *p)
{
 int choice;
    choice = -1;
    do
    {
        printf("请选择您要修改的职工的信息内容:\n");
        printf("+----------------------+\n");        
        printf("| 姓名          请按 1 |\n");
        printf("| 年龄          请按 2 |\n");
  printf("| 性别          请按 3 |\n");
  printf("| 学历      请按 4 |\n");
  printf("| 地址          请按 5 |\n");
  printf("| 电话          请按 6 |\n");
  printf("| 工资        请按 7 |\n");
  printf("| 取消          请按 0 |\n");
        printf("+----------------------+\n");

        printf("请输入您的选择:");
        scanf("%d", &choice);
      
        switch(choice)
        {
            case 0:
    return;
            case 1:
                printf("请输入新姓名:");
    scanf("%s", p->name);
                break;
            case 2:
                printf("请输入新年龄:");
    scanf("%d", &p->age);
                break;
   case 3:
                printf("请输入新性别:");
    scanf("%s", p->sex);
                break;
   case 4:
                printf("请输入新学历:");
    scanf("%s", p->eduLevel);
                break;
   case 5:
                printf("请输入新地址:");
    scanf("%s", p->address);
                break;
   case 6:
                printf("请输入新电话:");
    scanf("%ld", &p->tel);
                break;
   case 7:
                printf("请输入新工资:");
    scanf("%s", p->salary);
                break;
            default:
                printf("\n无效选项!");
                break;
        }
    }
    while(choice != 0);
}
void exit()
{
}

搜索更多相关主题的帖子: long head welcome address include 
2011-06-22 15:28
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:0 
#define FILE_DATA_PATH "c:/employeeInfo.txt"
#define FILE_DATA_PATH "c:\\employeeInfo.txt"

改成以上两种形式之一
2011-06-22 15:47
ppfly
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:297
专家分:1956
注 册:2009-5-17
收藏
得分:20 
void readData()函数里面的语句:printf("文件打开错误!");后面加上return;即:
程序代码:
if (!fp)
{
    printf("文件打开错误!");
    return;
}


********多贴代码,少说空话*******
2011-06-22 15:49
快速回复:新人求助~~~~关于 stream!=NULL的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021475 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved