大神请默默飘过。。。

以下是第一次制作时的源码:

// 商品信息管理.cpp : 定义控制台应用程序的入口点。
//

// 小型商品信息管理系统.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<string>
#include<conio.h>
#include<iostream>
#include<sstream>
using namespace std;

//定义结构体
typedef struct goods{
    string name;//商品名
    string number;//商品编号
    string area_of_producting;//出产地
    string the_date_of_purchase;//进货日期
    string sales_man;//进货商
    int price;//商品单价
    int stocks;//库存量
    struct goods *next;
    int i;//记录商品原始节点位置
    goods() {//构造函数
        name="\0";
        number="\0";
        area_of_producting="\0";
        the_date_of_purchase="\0";
        sales_man="\0";
        price=0;
        i=0;
        next=NULL;
    }
   
}Goods;

//函数声明
Goods* Search_the_massage_of_Goods(Goods* );
Goods* Delete_link_node(Goods *);
Goods* Insert_link_node(Goods*);
Goods* Create_link_node(Goods* );
Goods* Create_head();
void Input_the_massage_of_Goods(Goods *);
Goods* Sort_link_node(Goods *);
void Write_the_massage_of_one_Good_to_file(Goods *);
Goods* Read_the_massage_of_Goods_from_system();

//主函数
int main(){
    
       while(1){
        char ch='0';
        Goods *p,*pr;
        system("color A9");
        cout<<"_____________________________________________\n";
        cout<<(char)2<<"输入相应数字执行相应操作."<<(char)2<<"\n";
        cout<<"1.录入商品信息"<<(char)2<<"\n";
        cout<<"2.删除商品信息"<<(char)2<<"\n";
        cout<<"3.查询商品信息"<<(char)2<<"\n";
        cout<<"4.修改商品信息"<<(char)2<<"\n";
        cout<<"5.插入商品信息"<<(char)2<<"\n";
        cout<<"6.退出"<<(char)2<<"\n";
        cout<<"_____________________________________________\n";

Goods *head=Read_the_massage_of_Goods_from_system();;
    if(head==NULL){
        head=Create_head();
    }

for(;ch>'6'|| ch<'1';ch=_getch());
        system("cls");
        switch(ch){
            case '1':
            p=Create_link_node(head);
            cout<<"是否立即写入商品信息 Y/N\n";
            char ch;
            for(;ch!='Y'&&ch!='y'&&ch!='N'&&ch!='n';ch=_getch());
            if(ch=='y'||ch=='Y') Input_the_massage_of_Goods(p);
            Write_the_massage_of_one_Good_to_file(p);//将信息写入文件
            break;
        case '2':
            Delete_link_node(head);
            break;
        case '3':
            Search_the_massage_of_Goods(head);
            break;
        case '4':
            cout<<"查询要修改的商品\n";
            pr=Search_the_massage_of_Goods(head);
            Input_the_massage_of_Goods(pr);
            break;
        case '5':
            Insert_link_node(head);
            break;
        case '6':
            exit(0);
            break;
        }
    }
    return 0;
}

Goods* Delete_link_node(Goods *head){
    Goods *p,*pr;
    cout<<"查询要删除的节点\n";
    p=Search_the_massage_of_Goods(head);
    if(p==head){
        p=head;
        head=p->next;
        free(p);
    }
    else if(p->next=NULL){
        for(pr=head;pr->next!=p;pr=pr->next);
        free(pr);
        pr=NULL;
        p=NULL;
    }
    else{
        for(pr=head;pr->next!=p;pr=pr->next);
        pr->next=p->next;
        free(p);
        p=NULL;
    }
    return head;
}

//创建头节点,返回头节点的指针

Goods* Create_head(){
    Goods *head;
    head=new Goods();
    head->i=1;
    head->next=NULL;
    return head;
}

//新建节点,传入头指针,如果头指针为空,将新建节作为头节点,否则将新建节
//插入至链表末尾,返回头指针

Goods* Create_link_node(Goods* head){
    Goods *p,*pr;
    p=new Goods();
    (p->i)++;
    if(head==NULL){
        head=p;
    }
    else{
        for(pr=head;pr->next!=NULL;pr=pr->next);
        pr->next=p;
    }
    return head;
}

//新建节点,传入头指针,调用查询函数,插入节点至查询到的函数的前一节点

Goods* Insert_link_node(Goods*head){
    Goods *pr,*p,*temp;
    p=new Goods();
    if(p==NULL)
    {
        cout<<"No enough mrmory!\n";
        exit(0);
    }
    p->next=NULL;
    if(head==NULL)
        head=p;
    else
    {
        cout<<"插入节点至查询到的函数的前一节点\n";
        pr=Search_the_massage_of_Goods(head);
        if(pr==head)
        {
            p->next=head;
            head=p;
        }
        else
        {
            for(temp=head;temp->next==pr;temp=temp->next);
            p=temp->next;
            pr=p->next;
        }
    }
    return head;
}

//传入头指针,调用查询函数,查询相应要删除的节点,返回头指针

Goods* Search_the_massage_of_Goods(Goods* head){
    Goods* p;
    while(1){
        cout<<"__________________________________________\n";
        cout<<"输入相应数字执行相应查询操作              \n";
        cout<<"1.按商品名查询\n";
        cout<<"2.按商品编号查询\n";
        cout<<"按其余任意键退出\n";
        cout<<"__________________________________________\n";
        string s;
        char ch;
        for(;ch!='1'&&ch!='2'&&ch!='*';ch=_getch());
        switch(ch){
        case '1':
            cout<<"请输入要查询的商品名";
            cin>>s;
            for(p=head;s!=p->name;p=p->next);
            if(p==NULL) {
                cout<<"未查找到\n";
                break;
            }
            else
                cout<<"商品名"<<p->name<<"商品编号"<<p->number<<"出产地"<<p->area_of_producting<<"进货商"<<p->sales_man<<"库存量"<<"进货日期"<<p->stocks<<p->the_date_of_purchase;
            break;
            case '2':
            cout<<"请输入要查询的商品编号";
            cin>>s;
            for(p=head;s!=p->number;p=p->next);
            if(p==NULL) {
                cout<<"未查找到\n";
                break;
            }
            else
                cout<<"商品名"<<p->name<<"商品编号"<<p->number<<"出产地"<<p->area_of_producting<<"进货商"<<p->sales_man<<"库存量"<<"进货日期"<<p->stocks<<p->the_date_of_purchase;
            break;
        default:
            goto a;
            break;
        }
    }
a:;
    return p;
}
void Input_the_massage_of_Goods(Goods *p){
    cout<<"请输入商品编号";
    getline(cin,p->number);
    cout<<"请输入商品名称";
    getline(cin,p->name);
    cout<<"请输入商品出产地";
    getline(cin,p->area_of_producting);
    cout<<"请输入商品进货日期,中间用‘-’隔开";
    getline(cin,p->the_date_of_purchase);
    cout<<"请输入商品进货商";
    getline(cin,p->sales_man);
    cout<<"请输入商品库存量";
    cin>>p->stocks;
    cout<<"请输入商品单价";
    cin>>p->price;
    system("cls");
}

//排序函数,传入节点头指针,该函数要实现按商品单价排序和按商品库存量排序返回头指针
Goods* Sort_link_node(Goods *head){
    return head;
}

//输入函数,将商品信息存储到文件中,便于下次文件的初始化读写,传入新建节点的指针,将其内容写入文件末尾
void Write_the_massage_of_one_Good_to_file(Goods *p){
    ofstream file("the_massage_of_Goods.txt",ios::out|ios::ate);
    //)用 ate 方式打开一个已存在的文件,文件指针自动移到文件末尾,数据可以写入到其中。
    //用 in 方式打开文件只能用于输入数据,而且该文件必须已经存在
    if(!file){//如果文件不存在
    cout<< "不可以打开文件"<<endl;
    exit(1);
    }
    //写文件
    file<<p->i<<p->name<<p->number<<p->sales_man<<p->stocks<<p->area_of_producting<<p->price<<p->stocks<<'\n';
    //关闭文件
    file.close();
}
       
   
//在程序打开时将信息从文件读入,初始化信息
Goods* Read_the_massage_of_Goods_from_system(){
    Goods *p,*head=Create_head();
    ifstream rfile("the_massage_of_Goods.txt",ios::in);  //in 方式打开文件只能用于输入数据,而且该文件必须已经存在
       if(!rfile){        
        cout<< "不可以打开文件"<<endl;      
        exit(1);   
    }
    string str;
    for(p=head;getline(rfile,str);p=p->next){
        istringstream sin(str);
        p=new Goods();
        sin>>p->i>>p->name>>p->number>>p->sales_man>>p->stocks>>p->area_of_producting>>p->price>>p->stocks;
    }
    //rfile.close();
    return head;
}

这看上去好像是没什莫问题,可是在VS2008运行时,控制台一闪而过,,这是为甚末呢??小编也是苦思良久,终于得出答案,用 in 方式打开文件只能用于输入数据,而且该文件必须已经存在,所以我就在源文件出建立了the_massage_of_Goods.txt这个文件,可问题并没有解决,最后,小编在Goods* Read_the_massage_of_Goods_from_system()这个函数里加上了_getch();

Goods* Read_the_massage_of_Goods_from_system(){
    Goods *p,*head=Create_head();
    ifstream rfile("the_massage_of_Goods.txt",ios::in);  //in 方式打开文件只能用于输入数据,而且该文件必须已经存在
    _getch();
    if(!rfile){        
        cout<< "不可以打开文件"<<endl;      
        exit(1);   
    }
    string str;
    for(p=head;getline(rfile,str);p=p->next){
        istringstream sin(str);
        p=new Goods();
        sin>>p->i>>p->name>>p->number>>p->sales_man>>p->stocks>>p->area_of_producting>>p->price>>p->stocks;
    }
    //rfile.close();
    return head;
}

这时控制台终于停了下来,显示一个我设计的一个菜单界面
可只要按一下,控制台又会消失,到底是哪出了问题呢??经过一再的检查代码,终于发现是这里:

只能说是文件没有成功打开,接下来我们可以试试,我对Goods* Read_the_massage_of_Goods_from_system()函数做以下修改,如果文件打开失败,那末就会打出一连串#字符号并显示不可打开文件:

Goods* Read_the_massage_of_Goods_from_system(){
    Goods *p,*head=Create_head();
    ifstream rfile("the_massage_of_Goods.txt",ios::in);  //in 方式打开文件只能用于输入数据,而且该文件必须已经存在
   
    if(!rfile){        
        cout<< "不可以打开文件"<<endl;
        cout<<"#################################################";
        _getch();
        exit(1);   
    }
    string str;
    for(p=head;getline(rfile,str);p=p->next){
        istringstream sin(str);
        p=new Goods();
        sin>>p->i>>p->name>>p->number>>p->sales_man>>p->stocks>>p->area_of_producting>>p->price>>p->stocks;
    }
    //rfile.close();
    return head;
}

运行后发现:

很明显,文件打开失败了,这又是怎末回事呢??

一般操作系统都会隐藏文件后缀,如果设置了隐藏常见文件类型,就会有两个后缀,所以就只能检测文件是否存在了,如果不存在,则重新创建一个,于是小编对Goods* Read_the_massage_of_Goods_from_system()作了如下修改:

//在程序打开时将信息从文件读入,初始化信息
Goods* Read_the_massage_of_Goods_from_system(){
    if(exist("the_massage_of_Goods.txt")==0) {
        ifstream rfile("the_massage_of_Goods.txt",ios::out);
        rfile.close();
    }      //如果文件不存在,则重新创建一个文件

Goods *p,*head=Create_head();
    ifstream rfile("the_massage_of_Goods.txt",ios::in);  //in 方式打开文件只能用于输入数据,而且该文件必须已经存在
   
    if(!rfile){        
        cout<< "不可以打开文件"<<endl;
        exit(1);   
    }
    string str;
    for(p=head;getline(rfile,str);p=p->next){
        istringstream sin(str);
        p=new Goods();
        sin>>p->i>>p->name>>p->number>>p->sales_man>>p->stocks>>p->area_of_producting>>p->price>>p->stocks;
    }
    rfile.close();
    return head;
}
   
int exist(char *file)    //传入想要判断的路径字符串指针
{
    FILE *fp;

fp=fopen(file,"r");   //fopen是一个C库函数,用于打开文件,"r"是只读模式,在这种模式下,如果文件存在,则能成功以只读模式打开,fopen返回一个非0的文件描述符,如果文件不存在,则fopen返回NULL(NULL意思是空)。正好可以利用这一点来判断文件是否存在

if(fp=NULL)

return 0;   //不存在返回0

else

{

fclose(fp);  //存在的话,要先把之前打开的文件关掉

return 1;    //然后返回1

}

}

最终调试成功,谢谢诸位。。。

用C语言制作小型商品信息管理系统过程中的问题的更多相关文章

  1. PHP基础示例:商品信息管理系统v1.1[转]

      实现目标:使用php和mysql写一个商品信息管理系统,并带有购物车功能 一.创建数据库和表 1.创建数据库和表:demodb 2.创建表格:goods 字段:商品编号,商品名称,商品类型,商品图 ...

  2. PHP基础示例:商品信息管理系统v1.1

    实现目标:使用php和mysql写一个商品信息管理系统,并带有购物车功能 一.创建数据库和表 1.创建数据库和表:demodb 2.创建表格:goods 字段:商品编号,商品名称,商品类型,商品图片, ...

  3. 简易商品信息管理系统——首个Web项目

    正文之前 在学习了一段时间的Java Web的内容之后,当然需要有个项目来练练手,我相信大多数人的首选项目都是信息管理系统吧,所以我选择了商品信息管理系统 目前项目源码已全部上传至GitHub,欢迎大 ...

  4. 【Java Web】简易商品信息管理系统——首个Web项目

    正文之前 在学习了一段时间的Java Web的内容之后,当然需要有个项目来练练手,我相信大多数人的首选项目都是信息管理系统吧,所以我选择了商品信息管理系统 目前项目源码已全部上传至GitHub,欢迎大 ...

  5. Java Swing设计简单商品信息管理系统(java swing+mysql+eclipse)

    一.概述 为了管理好商店库存信息,提升店铺管理工作效率,结合实际工作需要,设计和开发本系统,主要用于商店商品信息维护出入库等.包含商品库存信息查看.商品信息修改,新增商品信息,删除信息等功能. 二.功 ...

  6. C语言大作业---学生信息管理系统

    xxxx信息管理系统 简介 因为大作业规定的踩分项就那么多,为了不浪费时间 + 得分,就写成这样.现在看看,命名不规范,书写风格糟糕,全塞在一个源代码中······ 不过,应付大作业是没问题的 实验报 ...

  7. 制作移动端手机网站过程中的SEO优化方法技巧

    据国内三大运营商数据来看,中国的手机用户数已达10亿,超过2/5的移动用户每个月都会从手机终端访问网页,如今的移动端手机网站比例肯定有提升,但是对于这些存在的移动版本网站来说,马海祥查看了很大一部分手 ...

  8. 嵌入式C语言自我修养 09:链接过程中的强符号和弱符号

    9.1 属性声明:weak GNU C 通过 __atttribute__ 声明weak属性,可以将一个强符号转换为弱符号. 使用方法如下. void __attribute__((weak)) fu ...

  9. S1 商品信息管理系统

    #include <iostream> #include <cstdio> #include <cstdlib> #include <iomanip> ...

随机推荐

  1. TCP/IP详解之:IGMP和DNS

    第13章 IGMP:Internet组管理协议 IGMP用于支持主机和路由器进行多播: IGMP是IP层的一部分,IGMP报文通过IP数据报进行传输: IGMP报文长度为固定8 Byte: 报文中,I ...

  2. shell ps1 提示设置

    PS1="\[\033[01;37m\]\u\[\033[00m\]@\[\033[01;31m\]localhost \t\[\033[00m\]:\[\033[01;35m\]\w\[\ ...

  3. DWZ 框架remote 验证字段唯一性方法提交后台,如果是中文会显示成乱码问题

    关于jquery  remote 验证字段唯一性方法提交后台,如果是中文会显示成乱码问题.可以直接修改tomcat 配置文件server.xml  设置 URIEncoding=utf-8属性,将ge ...

  4. Picasso 加载图片到RelativeLayout之解决方案

    Picasso 加载图片到ImageView 或者自己的自定义View都是可以直接调用对应API的,但是用into(0直接也加载到RelatieLayout就不好使了,可以这样来: Picasso.w ...

  5. 【原】YUI Test自动化测试实例详解

    测试在软件开发中至关重要,目前针对不同的开发语言,都有比较成熟的测试框架,如jUnit,cUnit,cppUnit,nUnit等,我们统称为xUnit,他们的都遵守统一的规则: 针对代码测试 断言 启 ...

  6. IPTV小窗口播放视频 页面焦点无法移动的解决方法

    在IPTV高清页面中,小窗口播放视频时,在某些机顶盒上(如高清中兴.高清大亚4904)会出现焦点无法移动现象,即按键无响应.被这个bug困扰了很久,虽然我知道解决方法,但只知其然,不知其所以然.今天做 ...

  7. C语言基础03

    1.随机数 :一个范围内随机数字的返回值. 格式为: arc4random() % ( num大值 -num小值 + 1 ) + num小值. int n,i= 0;           //控制随机 ...

  8. 当Evernote结合MindManager

    最近潜心研究Evernote(印象笔记)在数据存储和GTD规划方面的运用,感觉到的震撼和惊艳,一如当初开始接触MindManager的时候! 我非常喜欢这个SaaS的软件,以至于我将我国际版的Ever ...

  9. 非GUI-Qt程序运行后显示Console(简单好用)

    ----我的生活,我的点点滴滴!! 有很多时候,我们在程序中添加了好Debug信息,方便程序在运行期间打印出一些我们需要的信息或者,想用他来显示一些必要信息时, 那么console就太重要了,曾几何时 ...

  10. KO.js学习笔记(一)

    1.官方网站:knockoutjs.com 2.要dom树加载完毕才能绑定数据 3.ui能实时更新,使用了ko的一个自定义属性:data-bind 4.可以对viewmodel中的属性添加subsci ...