| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1729 人关注过本帖, 1 人收藏
标题:关于文件的读写 ,有兴趣就进来看看!!!!
只看楼主 加入收藏
dsjdcy
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-12-2
收藏(1)
 问题点数:0 回复次数:11 
关于文件的读写 ,有兴趣就进来看看!!!!
void openfile(FILE *nf)   /*这是一个显示文件内容的函数*/
{
        int d=0;
        rewind(nf);
        rd=(struct pcb *)malloc(sizeof(struct pcb));
        if(!rd)exit(0);
        while(!feof(nf))
        {       fread(rd,sizeof(struct pcb),1,nf);
                printf("\nNO.%d=====================================\n",++d);
                printf("%s\n%s\n%s\n%s\n%s\n",rd->a,rd->kehu,rd->gongyi,rd->kebian,rd->beizhu);
                printf("\n=====================================\n");
        }
        if(d==0)printf("file is null!\n");
}


no2=head;  /*这段代码是将数据写入文件的函数,数据是一条头指针为head(节点类型是struct pcb)的链表*/
while(no2)
{
   if(fwrite(no2,sizeof(struct pcb),1,nf)!=1)puts("error");
   /*printf("%s\n%s\n%s\n%s\n%s\n",no2->a,no2->kehu,no2->gongyi,no2->kebian,no2->beizhu);*/
   no2=no2->next;
}
  链表确定没问题了,我已经测试过!!!但文件内容的显示有问题,只能显示第一个被写入文件的数据.其它的就显示为乱码,不知道是写入文件时出了问题还在读取显示内容时出了问题,帮此想听听各位的意见!!!!!
  附件为全部的原代码!!!

pcb.rar (1.61 KB)
搜索更多相关主题的帖子: 兴趣 文件 pcb struct 
2007-12-15 07:05
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
/* 修改了第一个函数 ,暂时就改到这里 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE * create_file(char * fname);    /* 打开 fname 文件 */

int main(void)
{
    FILE * pf;
    char fname[125] = {'\0'};
   
    while(fname[0] == '\0')
    {
        printf("Please enter the file name: ");
        gets(fname);
    }
   
    pf = create_file(fname);
   
    // show_file(pf);          /* 打开并显示文件内容              */
    // edit_file(pf);          /* 对文件内容进行编辑\查找等操作   */

    fclose(pf);             /* 关闭文件                        */
   
    puts("The End!");
    system("Pause");
    return 0;
}


/*----------------------------------------------------------*/

FILE * create_file(char * fname)
{
    FILE * pf = NULL;
    int ch;

    if((pf = fopen(fname, "r+")) == NULL)   /* 文件不存在则创建 */
    {
        printf("The %s does not exist!\n", fname);

        printf("Whether the creation of the document? (Q to quit)\n");
        printf("[Y / N]... ");
        while((ch = getchar()) != 'y'&& ch != 'Y'
                       && ch != 'n' && ch != 'N'  )
        {
            while(getchar() != '\n')
                ;
            printf("[Y / N]... ");
        }

        switch(ch)
        {
            case 'y':
            case 'Y':
                if((pf = fopen(fname, "a+")) == NULL)
                {
                    puts("Failure to create documents!");
                    system("Pause");
                    exit(1);
                }
                break;

            case 'n':
            case 'N':
                puts("Goodbye!");
                system("Pause");
                exit(1);
                break;
            default :
                puts("Goodbye!");
                system("Pause");
                exit(1);
                break;
        }
    }
    else
        printf("%s to open!\n", fname);

    return pf;
}

—>〉Sun〈<—
2007-12-15 11:59
dsjdcy
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-12-2
收藏
得分:0 
回复2#
2楼所做的更改确实很不错,而且我也打算改一下,看来不用我动手了,呵呵!!!!!先谢了!!

      但现在遇到的关键问题是不能正确的写入或是读取(还搞不清楚是读取还是写入时出了问题)显示出来,就是我贴出来的两段代码!!!!!!!想了好多办法,但还是显示出乱码!!!!!

有兴趣共同学习的加QQ287624635 !或E-main至dsjdcy@
2007-12-15 12:26
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
/*   v 1.1  不支持删除操作,并且文件中的项目允许相同 */
                       /* 现在就写到这 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct pcb
{
    int  num;                   /* 编号   */
    char kehu[40];              /* 客户   */
    char gongyi[30];            /* 工艺   */
    char kebian[50];            /* 客编   */
    char beizhu[200];           /* 备注   */
};

int write_pcb(FILE * pf);            /* 修改 (edit_file()函数访问此函数)  */
int show_file(FILE * pf);           /* 显示文件内容        */
int edit_file(FILE * pf);            /* 修改、查找、显示    */
FILE * create_file(char * fname);    /* 打开 fname 文件     */

int main(void)
{
    FILE * pf;
    char fname[125] = {'\0'};
   
    while(fname[0] == '\0')
    {
        printf("Please enter the file name: ");
        gets(fname);
    }
   
    pf = create_file(fname);
   
    while(edit_file(pf))
        continue;

    fclose(pf);             /* 关闭文件                        */
   
    puts("\nThe End!");
    system("Pause");
    return 0;
}


/* 打开文件 ****************************/
FILE * create_file(char * fname)
{
    FILE * pf = NULL;
    int ch;

    if((pf = fopen(fname, "r+b")) == NULL)
    {
        printf("The %s does not exist!\n", fname);

        printf("Whether the creation of the document? (Q to quit)\n");
        printf("[Y / N]... ");
        while((ch = getchar()) != 'y'&& ch != 'Y'
                       && ch != 'n' && ch != 'N'  )
        {
            while(getchar() != '\n')
                continue;
            printf("[Y / N]... ");
        }
        while(getchar() != '\n')
            continue;

        switch(ch)
        {
            case 'y':
            case 'Y':
                if((pf = fopen(fname, "w+b")) == NULL)
                {
                    puts("Failure to create documents!");
                    system("Pause");
                    exit(1);
                }
                break;

            case 'n':
            case 'N':
                puts("Goodbye!");
                system("Pause");
                exit(1);
                break;

            default :
                exit(1);
                break;
        }
    }
    else
        printf("%s to open!\n", fname);

    return pf;
}


/* 显示文件内容 ******************/
int show_file(FILE * pf)
{
    struct pcb temp;
    int count = 0;

    if(pf == NULL)
    {
        fprintf(stderr, "Error! [enter]...");
        getchar();
        exit(1);
    }

    rewind(pf);   /* 回到文件开始处 */
   
    system("CLS");
    puts("============== File ==================");
   
    while(fread(&temp, sizeof(struct pcb), 1, pf) == 1)
    {
        ++count;
        printf("\nId: %d\n  kehu: %s\n  gongyi%s\n  kebian%s\n  beizhu%s\n"
            ,temp.num, temp.kehu, temp.gongyi, temp.kebian, temp.beizhu);

        if(count % 3 == 0)
        {
            system("Pause");
            system("CLS");
        }
    }

    if(count == 0)
        printf("\nfile is null!\n\n");
    else
        printf("\n                     Count: %d\n\n", count);
        
    rewind(pf);   /* 回到文件开始处 */
   
    return count;
}


/* 项目写入操作 ****************************/
int write_pcb(FILE * pf)
{
    struct pcb temp;

    printf("Id: ");
    while(scanf("%d", &temp.num) != 1)
    {
        while(getchar() != '\n')
            continue;
        printf("Id: ");
    }

    while(getchar() != '\n')
        continue;

    printf("kehu: ");
    gets(temp.kehu);
    printf("gongyi: ");
    gets(temp.gongyi);
    printf("kebian: ");
    gets(temp.kebian);
    printf("beizhu: ");
    gets(temp.beizhu);

    if(fwrite(&temp, sizeof(struct pcb), 1, pf) != 1)
    {
        fprintf(stderr, "Error! [enter]...\n");
        getchar();
        exit(1);
    }
    return 1;
}

/* 编辑文件 **********************************/
int edit_file(FILE * pf)
{
    int ch;
    int num;
    int i;
    struct pcb temp;

    if(pf == NULL)
    {
        fprintf(stderr, "Error! [enter]...");
        getchar();
        exit(1);
    }

    rewind(pf);   /* 回到文件开始处 */

    puts("================== Menu ==================\n");
    ch = '\0';
    while(ch != 'e' && ch != 'f' && ch != 'a' && ch != 'q' && ch != 's')
    {
        printf("\na)add    e)edit\n"
               "f)find   s)show\n"
               "q)quit\n\n"          );
        ch = getchar();
        if(ch != '\n')
            while(getchar() != '\n')
                continue;
    }

    system("CLS");
   
    switch(ch)
    {
        case 'a':
            puts("Add ---------------------------\n");
            fseek(pf, 0l, SEEK_END);    /* 到文件结尾 */
            write_pcb(pf);
            break;
            
        case 'e':
            puts("Edit ---------------------------\n");
            printf("Edit Id: ");
            while(scanf("%d", &num) != 1)
            {
                while(getchar() != '\n')
                    continue;
                printf("Edit Id: ");
            }
            while(getchar() != '\n')
                continue;

            rewind(pf);  /* 回到文件开始处 */

            while(fread(&temp, sizeof(struct pcb), 1, pf) == 1)
            {
                if(num == temp.num)
                {
                    i = ftell(pf) - sizeof(struct pcb);
                    fseek(pf, (long)i, SEEK_SET);  /* 定位要修改的项目 */
                    write_pcb(pf);
                    break;
                }
            }
            if(num != temp.num)
            {
                printf("不存在!\n");
                system("Pause");
            }
            break;
            
        case 'f':
            puts("Find ---------------------------\n");
            printf("Find Id: ");
            while(scanf("%d", &num) != 1)
            {
                while(getchar() != '\n')
                    continue;
                printf("Find Id: ");
            }
            while(getchar() != '\n')
                continue;

            rewind(pf);  /* 回到文件开始处 */
            while(fread(&temp, sizeof(struct pcb), 1, pf) == 1)
            {
                if(num == temp.num)
                {
                    printf("记录存在!\n");
                    break;
                }
            }
            if(temp.num != num)
                printf("不存在!\n");
            system("Pause");
            break;
            
        case 's':
            show_file(pf);
            break;
            
        default :
            return 0;
            break;
    }

    rewind(pf);   /* 回到文件开始处 */
   
    return 1;
}


[[italic] 本帖最后由 cosdos 于 2007-12-15 20:50 编辑 [/italic]]

—>〉Sun〈<—
2007-12-15 19:47
dsjdcy
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-12-2
收藏
得分:0 
回复 4# 的帖子
啊!!!!!!!今天遇到绝世高手了!!!!!   问题考虑得太具体了.我都给看呆了~!!老哥, 是专业的吧!!!!?????
I真是服了YOU!!!!!!  好象这段代码有一种情况没考虑到哦!!!
case 'e':
            puts("Edit ---------------------------\n");
            printf("Edit Id: ");
            while(scanf("%d", &num) != 1)
            {
                while(getchar() != '\n')
                    continue;
                printf("Edit Id: ");
            }
            while(getchar() != '\n')
                continue;

            rewind(pf);  /* 回到文件开始处 */

            while(fread(&temp, sizeof(struct pcb), 1, pf) == 1)      /*如果读取文件失败,temp.num就不存在*/
            {
                if(num == temp.num)
                {
                    i = ftell(pf) - sizeof(struct pcb);
                    fseek(pf, (long)i, SEEK_SET);  /* 定位要修改的项目 */
                    write_pcb(pf);
                    break;
                }
            }
            if(num != temp.num)          /*temp.num不存在,若输入的num值为0,则该if语句不会被执行*/
            {
                printf("Don't exist!\n");
                system("Pause");
            }
            break;

要是不嫌本人太菜,交个朋友吧!!!!QQ287624635E-mail:dsjdcy@

[[italic] 本帖最后由 dsjdcy 于 2007-12-16 10:24 编辑 [/italic]]

有兴趣共同学习的加QQ287624635 !或E-main至dsjdcy@
2007-12-16 09:09
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
确实没考虑到,谢谢提醒!

[[italic] 本帖最后由 cosdos 于 2007-12-16 15:35 编辑 [/italic]]

—>〉Sun〈<—
2007-12-16 15:15
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
            
            k = 0;     /* 加了个变量 k 处理这个问题 */
            while(fread(&temp, sizeof(struct pcb), 1, pf) == 1)
            {
                if(num == temp.num)
                {
                    k = 1;
                    i = ftell(pf) - sizeof(struct pcb);
                    fseek(pf, (long)i, SEEK_SET);
                    write_pcb(pf);
                    break;
                }
            }
            if(k < 1)   /* 小于 1 就说明没有读取到匹配的项目 */
            {
                printf("不存在!\n");
                system("Pause");
            }

-

[[italic] 本帖最后由 cosdos 于 2007-12-16 15:34 编辑 [/italic]]

—>〉Sun〈<—
2007-12-16 15:32
dsjdcy
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-12-2
收藏
得分:0 
回复 7# 的帖子
你这个程序确实写得不错!!!!比起我的来不知道要好到哪里去了,唉!!!
     你这个程序将数据存入文件时是输完一个结构类型就立刻存进去了,说实话,我当时没想到这个方法,加上我在学链表的使用,所以就想先把数据存入链表,然后一次性写入文件.到现在我还不知道为什么会出现乱码,怎么也想不通!!!   不过经测试,问题不是出在链表上,这点我还是感到蛮欣慰!!!!!!!

有兴趣共同学习的加QQ287624635 !或E-main至dsjdcy@
2007-12-19 09:04
xianshizhe111
Rank: 1
等 级:新手上路
帖 子:1451
专家分:0
注 册:2007-12-8
收藏
得分:0 
2#写的好啊,支持
2007-12-19 11:07
yeqishi
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2007-9-19
收藏
得分:0 
学习了

2007-12-19 12:03
快速回复:关于文件的读写 ,有兴趣就进来看看!!!!
数据加载中...
 
   



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

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