| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1286 人关注过本帖
标题:求问?帮看下程序。为什么存入结构体至文件中其文件格式不是TXT而是“文件” ...
只看楼主 加入收藏
世界你好
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2013-12-12
结帖率:0
收藏
已结贴  问题点数:10 回复次数:32 
求问?帮看下程序。为什么存入结构体至文件中其文件格式不是TXT而是“文件”格式?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char serial_num[20];
struct node *next;
};
int n1=0,n2=0;
void SaveList(struct node * head)
{
    printf("input the file name:\n");
    char name[20]={0};
    FILE * fp;
    scanf("%s",name);
    if((fp=fopen(name,"w"))==NULL)
    {printf("NO\n");exit(0);}
    struct node * p1,* p2;
    p2=head;
    p1=head->next;
    for(;(p1!=NULL)&&(p1!=head);)
    {
        if(fwrite(p2,sizeof(struct node),1,fp)!=1)
        {printf("Wrong\n");fclose(fp);}
        p2=p1;
        p1=p1->next;
    }
    fclose(fp);
}
struct node * LoadList(int n)
{
    FILE * fp;
    char name[20];
    printf("please input the file to open:\n");
    scanf("%s",name);
    if((fp=fopen(name,"r"))==NULL)
    {printf("NO\n");exit(0);}
    struct node * p1,* p2,* head;
    p1=p2=(struct node *)malloc(sizeof(struct node));
    head=p1;
    for(;;)
    {
        fread(p1,sizeof(struct node),1,fp);
        if(p1->next==NULL||p1->next==head){p1->next=NULL;break;}
        n++;
        p1=(struct node *)malloc(sizeof(struct node));
        p2->next=p1;
        p2=p1;
    }
    fclose(fp);
    return(head);
}
void PrintList(struct node * head,int n)
{
    struct node * p=head;
    int i;
    for(i=0;i<n;i++,p=p->next)
        printf("%s ",p->serial_num);
    printf("\n");
}
void ChangeList(struct node * head,int n)
{
    struct node * p1,* p2;
    int i,j;
    char t[20]={0};
    for(i=0;i<n-1;i++)
    {
        p2=head;
        p1=head->next;
        for(j=0;j<n-1-i;j++)
        {
            if(strcmp(p1->serial_num,p2->serial_num)<0)
            {strcpy(t,p1->serial_num);strcpy(p1->serial_num,p2->serial_num);strcpy(p2->serial_num,t);}
            p2=p1;p1=p1->next;
        }
    }
}
void main()
{
    struct node * p1,* p2,* h1,* head,* rear;
    printf("please input the list1:\n");
    p1=(struct node *)malloc(sizeof(struct node));
    h1=p1;
    for(;;)
    {
        scanf("%s",&p1->serial_num);
        if(strcmp(p1->serial_num,"0")==0)
        {
            p2->next=h1;
            rear=p2;
            break;
        }
        n1++;
        ChangeList(h1,n1);
        PrintList(h1,n1);
        p2=p1;
        p1=(struct node *)malloc(sizeof(struct node));
        p2->next=p1;
    }
    printf("please input the list2:\n");
    p1=(struct node *)malloc(sizeof(struct node));
    head=p1;
    for(;;)
    {
        scanf("%s",&p1->serial_num);
        if(strcmp(p1->serial_num,"0")==0)
        {
            p2->next=NULL;
            break;
        }
        n2++;
        ChangeList(head,n2);
        PrintList(head,n2);
        p2=p1;
        p1=(struct node *)malloc(sizeof(struct node));
        p2->next=p1;
    }
    SaveList(h1);
    SaveList(head);
    n1=0;n2=0;
    h1=LoadList(n1);
    PrintList(h1,n1);
    head=LoadList(n2);
    PrintList(head,n2);
    for(p1=h1;p1->next!=NULL;p1=p1->next);
    p1->next=head;
    PrintList(h1,n1+n2);
    SaveList(h1);
}

[ 本帖最后由 世界你好 于 2014-1-2 10:57 编辑 ]
搜索更多相关主题的帖子: include 结构体 file 
2014-01-02 10:27
世界你好
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2013-12-12
收藏
得分:0 
求助啊。刚刚发现不管格式,改了读取程序继续做,读不出来。
2014-01-02 10:41
世界你好
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2013-12-12
收藏
得分:0 
而且话说"w"不是打开的自写的文本文档吗?怎么会出现打不开的“文件”格式。。。
2014-01-02 10:44
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:2 
不是TXT而是“文件”格式?

真心看不懂啊

DO IT YOURSELF !
2014-01-02 10:44
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
纯文本文件 都是可以通过 纯文本编辑器来打开的  

可能有些扩展名系统没有绑定  但是这并不影响其本质的一些东西

DO IT YOURSELF !
2014-01-02 10:45
世界你好
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2013-12-12
收藏
得分:0 
回复 4楼 wp231957
。。。。不会吧。就是一道文件的题啊。其他不用看,就是问下为什么“w”新建的不是文本文档,而是一个“文件”格式的东西。
2014-01-02 10:46
世界你好
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2013-12-12
收藏
得分:0 
回复 5楼 wp231957
我这次前面有道题就用的“w”,就没问题,是新建的TXT格式。
2014-01-02 10:47
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
如果你想双击就打开它  请给他安一个扩展名  比如 *.txt

DO IT YOURSELF !
2014-01-02 10:47
世界你好
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2013-12-12
收藏
得分:0 
回复 8楼 wp231957
打开了。。。是乱码。。。很多屯
2014-01-02 10:54
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
我试着运行了一下你的程序   好像有bug

DO IT YOURSELF !
2014-01-02 10:58
快速回复:求问?帮看下程序。为什么存入结构体至文件中其文件格式不是TXT而是“ ...
数据加载中...
 
   



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

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