| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 838 人关注过本帖
标题:怎么新建和使用旧文件?还要可以在整个程序中使用它
只看楼主 加入收藏
gdyflxw
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-7-9
收藏
 问题点数:0 回复次数:6 
怎么新建和使用旧文件?还要可以在整个程序中使用它
我的程序是这样的:
#include "stdio.h"
#include "conio.h"
#include "string.h"
#include "stdlib.h"

#define N 100
struct people
{
    char num[20];
    char name[20];
    float P1;
    float P2;
    float all;
}people[N];

void show_version()
{
    printf("\n\n\n\n\n");
    printf("\t\t\t  The version is V1.00");
    printf("\n\n");
    printf("\t\t    Please press any key to continue...");
    getch();
}

printf_face()
{
    printf("\n       num      name        P1        P2       all      \n");
}

printf_one(int i)
{
    printf("%10s%10s%10.2f%10.2f%10.2f\n",people[i].num,people[i].name,people[i].P1,people[i].P2,people[i].all);
}

printf_back()
{
    int display();
    void menu();
    int w;
    printf("\n\n\tSuccessful.^_^\n\n");
    printf("What do you want to do?\n\n 1)Display all now\t2).Back:[ ]\b\b");
    scanf("%d",&w);
    if(w==1)
        display();
    else menu();
}

save(int n)
{
    FILE *fp;
    int i;
    if((fp=fopen("Pro.txt","wb"))==NULL)
    {printf("\nCannot open file\n");
    return NULL;
    }
    for(i=0;i<n;i++)
        if(people[i].num!=NULL)
            if(fwrite(&people[i],sizeof(struct people),1,fp)!=1)
                printf("file write error\n");
            fclose(fp);
}

no_input(int i,int n)
{
    int j,k,w1;
    do
    {
        w1=0;
        for(j=0;people[i].num[j]!='\0';j++)
            if(people[i].num[j]>'9')
            {
                puts("Input error! Only be made up of(0-9).Please reinput!\n");
                w1=1;break;
            }
            if(w1!=1)
                for(k=0;k<n;k++)
                    if(k!=i&&strcmp(people[k].num,people[i].num)==0)
                    {
                        puts("This record is exist.Please reinput!\n");
                    }
    }
    while(w1==1);
}


enter()
{
    int input(int i);
    int i,n;
    printf("How many people(0-%d)?:",N-1);
    scanf("%d",&n);
    printf("\n Enter data now\n\n");
    for(i=0;i<n;i++)
    {
        printf("\n Input %dth people record.\n",i+1);
        input(i);
    }
    if(i!=0)
        save(n);
    printf_back();
}

load()
{
    FILE *fp;
    int i;
    if((fp=fopen("Pro.txt","rb"))==NULL)
    {printf("\nCannot open file\n");
    getch();
    return menu();
    }
    for(i=0;!feof(fp);i++)
        fread(&people[i],sizeof(struct people),1,fp);
    fclose(fp);
    return(i-1);
}



input(int i)
{
    no_input(i,i);
    printf("num:");
    scanf("%s",&people[i].num);
    printf("name:");
    scanf("%s",&people[i].name);
    printf("P1:");
    scanf("%f",&people[i].P1);
    printf("P2:");
    scanf("%f",&people[i].P2);
    people[i].all=people[i].P1+people[i].P2;
    return NULL;
}

modify_data(int i)
{
    int c,w1;
    do
    {
        puts("\nmodify by=>\n\n 1).num 2).name 3).P1 4).P2 ");
        printf("Which you needed?:[ ]\b\b");
        scanf("%d",&c);
        if(c>4||c<1)
        {
            puts("\nChoice error! Please again!");
            getch();
        }
    }while(c>8||c<1);
    do
    {
        switch(c)
        {
        case 1:printf("num:");scanf("%s",&people[i].num);break;
        case 2:printf("name:");scanf("%s",&people[i].name);break;
        case 3:printf("P1:");scanf("%f",&people[i].P1);break;
        case 4:printf("P2:");scanf("%f",&people[i].P2);break;
        }
        puts("\nNow:\n");
        printf_face();
        printf_one(i);
        printf("\nAre you sure?\n\n\t 1).Sure 2)No and remodify 3).Back without save in this time [ ]\b\b");
        scanf("%d",&w1);
    }while(w1==2);
    return(w1);
}

display()
{
    int i,n;
    n=load();
    printf_face();
    for(i=0;i<n;i++)
    {
        if((i!=0)&&(i%10==0))
        {
            printf("\nPass any key to continue...");
            getch();
            puts("\n");
        }
        printf_one(i);
    }
    printf("\tThere are %d record.\n",n);
    printf("\nPass any key to back...");
    getch();
    return menu();
}


add()
{
    int i,n,m,k;
    FILE *fp;
    n=load();
    printf("How many people are you want to add(0-%d)?:",N-1-n);
    scanf("%d",&m);
    k=m+n;
    for(i=n;i<k;i++)
    {
        printf("\nInput %dth people record.\n",i-n+1);
        input(i);        
    }
    if((fp=fopen("Pro.txt","ab"))==NULL)
    {
        printf("cannot open file\n");
    }
    for(i=n;i<k;i++)
        if(fwrite(&people[i],sizeof(struct people),1,fp)!=1)
            printf("file write error\n");
        fclose(fp);
        printf_back();
}

del()
{
    int i,j,n,k,a,b='y';
    struct people p;
    do
    {
        n=load();
        system("cls");
        for(i=0;i<n;i++)
        printf_one(i);
        printf("\n\nEnter num that you want to del! num:");
        scanf("%s",&p.num);
        printf_face();
        for(i=0;i<n;i++)
        {
            if(strcmp(p.num,people[i].num)==0)
            {
                k=i;
                printf_one(k);break;
            }
        }
        printf("Are you sure?(y/n)");
        scanf("%c",&j);
        getch();
        if(j=='y'||'Y')
        {
            for(a=k;a<n;a++)
            {
                strcpy(people[a].num,people[a+1].num);
                strcpy(people[a].name,people[a+1].name);
                people[a].P1=people[a+1].P1;
                people[a].P2=people[a+1].P2;
                people[a].all=people[a+1].all;
            }
            save(n-1);
            printf("\nSuccessful!^_^");
            getch();
        }
        printf("\n\nDelete another?(y/n)");
        scanf("%c",&b);
    }while(b=='y');
    menu();
}

search()
{
int i,n,k,w1=1,w2,w3,w4,m,a;
struct people p;
do
{
    n=load();
    printf("\n\nWhich way do you want to choose? \n\t1).By name 2).By num [ ]\b\b");
    scanf("%d",&m);
    switch(m)
    {
    case 1:
        do
        { k=-1;
        printf("\n\nEnter xeuli that you want to search! xueli.");
        scanf("%s",&p.name);
        printf_face();
        for(i=0;i<n;i++)
            if(strcmp(p.name,people[i].name)==0)
            {
                k=i;
                printf_one(k);break;
            }
            if(k==-1)
            {
                printf("\n\nNO exist!please");
                printf("\n\nAre you again?\n\t1).again 2).NO and back [ ]\b\b");
                scanf("%d",&w1);
                if(w1==2)
                    printf_back();
            }
        }
        while(k==-1&&w1==1);break;
    case 2:
        do
        {
            k=-1;
            printf("\n\nEnter num that you want to search! num.");
            scanf("%s",&p.num);
            printf_face();
            for(i=0;i<n;i++)
                if(strcmp(p.num,people[i].num)==0)
                {
                    k=i;
                    printf_one(k);break;
                }
                if(k==-1)
                {
                    printf("\n\nNO exist!please");
                    printf("\n\nAre you again?\n\t1).again 2).NO and back [ ]\b\b");
                    scanf("%d",&w1);
                    if(w1==2)
                        printf_back();
                }
        }
        while(k==-1&&w1==1);break;
    }
    w4=0;w3=0;
    if(k!=-1)
    {
        printf("\n\nWhat do you want to do?\n\t 1).Search 2).Modify 3).Delete 4).Back menu [ ]\b\b");
        scanf("%d",&w2);
        switch(w2)
        {
        case 2:w3=modify_data(k);break;
        case 3:{printf("\nAre you sure?\n\t 1).Sure 2).No and back [ ]\b\b");
            scanf("%d",&w4);
            if(w4==1)
                for(a=k;a<n;a++)
                {
                    strcpy(people[a].num,people[a+1].num);
                    strcpy(people[a].name,people[a+1].name);
                    people[a].P1=people[a+1].P1;
                    people[a].P2=people[a+1].P2;
                    people[a].all=people[a+1].all;
                }
                save(n-1);
                break;
               }
        }
        if(w3==1)
        {
            save(n);
            printf("\n\nSuccessful.^_^.");
            printf("\n\nWhant do you want to do? \n\t 1).Search another 2).Back [ ]\b\b" );
            scanf("%d",&w2);
        }
    }
}
while(w2==1);
menu();
}

modify()
{
    struct people p;
    int i,n,k,w0=1,w1,w2=0;
    n=load();
    do
    {
        k=-1;
        printf_face();
        for(i=0;i<n;i++)
        {
            if((i!=0)&&(i%10==0))
            {
                printf("\n\nRemember the num which needed modify.\npass any key to contiune ...");
                getch();
                puts("\n\n");
            }
            printf_one(i);
        }
        do
        {
            printf("\n\nEnter the num that you want to modify! num:");
            scanf("%s",&p.num);
            for(i=0;i<n;i++)
                if(strcmp(p.num,people[i].num)==0)
                {
                    k=i;
                    p=people[i];
                }
                if(k==-1)printf("\n\nNO exist!please again");
        }
        while(k==-1);
        printf_face();
        printf_one(k);
        w1=modify_data(k);
        if(w1==1)
        {printf("\nSuccessful ^_^.\n\nAre you modify another ?\n\n\t 1).Yes 2).Back with save\t[ ]\b\b");
        scanf("%d",&w0);
        w2=1;
        }
        else
        {
            w0=0;
            if(w2==1)
                people[k]=p;
        }
        if(w0!=1&&w2==1)
            save(n);
    }
    while(w0==1);
    menu();
}


P1()
{
    struct people p;
    int i,j,n;
    n=load();
    for(j=0;j<n-1;j++)
        for(i=0;i<n-j-1;i++)
            if(people[i].P1<people[i+1].P1)
            {
                p=people[i];
                people[i]=people[i+1];
                people[i+1]=p;
            }
            save(n);
            menu();
            
}


P2()
{
    struct people p;
    int i,j,n;
    n=load();
    for(j=0;j<n-1;j++)
        for(i=0;i<n-j-1;i++)
            if(people[i].P2<people[i+1].P2)
            {
                p=people[i];
                people[i]=people[i+1];
                people[i+1]=p;
            }
            save(n);
            menu();
}

all()
{
    struct people p;
    int i,j,n;
    n=load();
    for(j=0;j<n-1;j++)
        for(i=0;i<n-j-1;i++)
            if(people[i].all<people[i+1].all)
            {
                p=people[i];
                people[i]=people[i+1];
                people[i+1]=p;
            }
            save(n);
            menu();
}


compositor()
{
    int t;
    printf("Which way do you want? BY=>1).P1 2).P2 3).all 4).No and back[ ]\b\b");
    scanf("%d",&t);
    switch(t)
    {
    case 1:P1();break;
    case 2:P2();break;
    case 3:all();break;
    case 4:printf_back();break;
    }
}


help()
{
    printf("NO");
    getch();
    menu();
}



void menu()
{
    int n,w1;
    system("cls");
    do
    {
        printf("\t\t**********************************************\n");
        printf("\t\t*** 1 display record              ************\n");
        printf("\t\t*** 2 add record                  ************\n");
        printf("\t\t*** 3 del record                  ************\n");
        printf("\t\t*** 4 search record               ************\n");
        printf("\t\t*** 5 modify record               ************\n");
        printf("\t\t*** 6 sort and compositor         ************\n");
        printf("\t\t*** 7 help                        ************\n");
        printf("\t\t*** 8 save as...                  ************\n");
        printf("\t\t*** 9 exit                        ************\n");
        printf("\t\t**********************************************\n");
        printf("\n");
        printf("Choose your number(1-9):[ ]\b\b");
        scanf("%d",&n);
        if(n<1||n>9)
            w1=1;
        else w1=0;
    }while(w1==1);
    switch(n)
    {
    case 1:display();break;                     
    case 2:add();break;                          
    case 3:del();break;                       
    case 4:search();break;                    
    case 5:modify();break;                  
    case 6:compositor();break;               
    case 7:help();break;                       
    case 8:save(n);break;                        
    case 9:exit(0);                           
    }
}

main()
{
    show_version();                           
    menu();                                 
}


现在我不要指定的Pro.txt文件,我要可以另外输入一个文件的路径来打开那个文件,如果没有的话还要可以新建一个文件来使用,不知道要搞什么.....
先谢谢各位了...
搜索更多相关主题的帖子: printf include 文件 num 
2008-07-09 13:49
k7785524
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-6-27
收藏
得分:0 
不懂说的是什么呀









" border="0" />
[url=http://www.]魔兽剑圣异界纵横[/url]
2008-07-09 18:21
Rand
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2008-7-2
收藏
得分:0 
大概猜下,LZ是不是想自己指定文件名~
那你不如用个filename【N】来保存
gets(filename);
fp=fopen("filename",...);
2008-07-09 20:01
gdyflxw
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-7-9
收藏
得分:0 
哦,不好意思,我语文基本上都是60分,不会表达....
我的意思是在用一个全局变量,把那个Pro.txt变成自己输入的文件路径和名称,3楼的朋友是可以,但是我想只输入一次就可以以后都不用输入了,我不知道怎么加,如果在那个load()函数里加上
2008-07-10 11:38
gdyflxw
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-7-9
收藏
得分:0 
晕...按错了,就发表了..
把那个load()函数改成:
load()
{
    FILE *fp;
    int i;
    char filename[20];
    printf("Please enter the file name:");
    scanf("%s",filename);
    if((fp=fopen(filename,"rb"))==NULL)
    {printf("\nCannot open file\n");
    getch();
    return menu();
    }
    for(i=0;!feof(fp);i++)
        fread(&people[i],sizeof(struct people),1,fp);
    fclose(fp);
    return(i-1);
}
这样的话是可以,但是每次读取都要输入一次文件名,太麻烦了.
2008-07-10 11:43
gdyflxw
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-7-9
收藏
得分:0 
还有就是不能在整个程序中运用,在以后那个save(int n)这个函数里面又不能用那个路径的文件,太麻烦了.
2008-07-10 11:45
独孤小梦
Rank: 1
等 级:新手上路
威 望:1
帖 子:338
专家分:0
注 册:2008-6-5
收藏
得分:0 
太多了!我慢慢看,不过建议LZ加下注释!方便看代码!

编程小菜
2008-07-10 12:01
快速回复:怎么新建和使用旧文件?还要可以在整个程序中使用它
数据加载中...
 
   



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

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