免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2569 | 回复: 9
打印 上一主题 下一主题

文件内容不能正常输出到屏幕上????? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-13 12:00 |只看该作者 |倒序浏览
#include<stdio.h>
#include<string.h>
#define SIZE 10


struct student_type
{
        int num;
        char name[10];
        int age;
        char sex[5];
        float English;
        float PE;
        float Computer;
        float Maths;
        float Progrome;
}std1[SIZE],std2[SIZE];

void main()
{
        FILE *fp;
        int i;
        char infile[10];
   
    printf("Enter the infilename:\n");
    scanf("%s",infile);
        if ((fp = fopen(infile,"rb")) == NULL)
         {
                printf("cannot open infile\n");
                return;
         }
        for (i = 0; i < SIZE; i++)
           {
                   if(! feof(fp))
              fread(&std1[i],sizeof(struct student_type),1,fp);
            else
              printf("file write error\n");
           }
        fclose (fp);
        for (i = 0; i < SIZE; i++)
        printf("%d %s %d %s %f %f %f %f %f\n",
    std1[i].num,std1[i].name,std1[i].age,std1[i].sex,
    std1[i].English,std1[i].PE,std1[i].Computer,std1[i].Maths,std1[i].Progrome);
}

当运行时:
Enter the infilename:
C:\8.txt
结果为一大堆的乱码!
???
请问各位高手
为什么会怎样呢?????
其中8.txt的内容为:
1 huang 21 m 87 78 78 78 78
2 jiang 20 w 89 87 67 78 89
3 jianh 21 m 90 90 90 90 90
4 jiajy 22 m 89 89 89 89 89
5 huang 32 m 89 89 89 89 89
6 haijs 21 w 90 89 89 89 98
7 ajhdj 21 w 43 34 43 43 43
8 hiajh 12 m 21 21 21 21 21
9 jianh 12 m 21 21 12 21 21
0 jisns 21 m 12 21 21 21 21
为什么不能正常输出8.txt的内容???
    谢谢
                           大一嫩生

论坛徽章:
0
2 [报告]
发表于 2008-05-13 12:17 |只看该作者
结构会字节对齐么? 读出来都是字符串, 不是int, float什么的

[ 本帖最后由 204tian 于 2008-5-13 12:19 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2008-05-13 13:12 |只看该作者

回复 #2 204tian 的帖子

结构体怎样字节对齐么??
读出来都是字符串, 不是int, float什么的
那用字符串也不行啊?

论坛徽章:
0
4 [报告]
发表于 2008-05-13 13:18 |只看该作者
将定义改为:
struct student_type
{
        int num;
        char c1;
        char name[5];
        char c2;
        int age;
        char c3;
        char sex;
        char c4;
        float English;
        char c5;
        float PE;
        char c6;
        float Computer;
        char c7;
        float Maths;
        char c8;
        float Progrome;
        char c9;
}std1[SIZE],std2[SIZE];
再改为:
           fread(&std1,sizeof(struct student_type),1,fp);
            printf("%d %s %s %s %d %s %s %s %f %s %f %s %f %s %f %s %f %s\n",
            std1.num,std1.c1,std1.name,std1.c2,std1.age,std1.c3,std1.sex,std1.c4,
            std1.English,std1.c5,std1.PE,std1.c6,std1.Computer,std1.c7,std1.Maths,
            std1.c8,std1.Progrome,std1.c9);
也不行啊?

论坛徽章:
0
5 [报告]
发表于 2008-05-13 13:25 |只看该作者
急需高手帮助!

论坛徽章:
0
6 [报告]
发表于 2008-05-13 14:30 |只看该作者
用fscanf()读

[ 本帖最后由 204tian 于 2008-5-13 14:31 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2008-05-13 14:49 |只看该作者

回复 #6 204tian 的帖子

还是不行啊!
但改成:
#include<stdio.h>
#include<string.h>
#define SIZE 10

struct student_type
{
        int num;
        char name[5];
        int age;
        char sex;
        float English;
        float PE;
        float Computer;
        float Maths;
        float Progrome;
}std1[SIZE],std2[SIZE];

void main()
{
        FILE *fp;
        int i;
        char infile[10];
   
    printf("Enter the infilename:\n");
    scanf("%s",infile);
        if ((fp = fopen(infile,"rb")) == NULL)
         {
                printf("cannot open infile\n");
                return;
         }
        for (i = 0; i < SIZE; i++)
           {
                   if(! feof(fp))
              {
                      fscanf(fp,"%d %s %d %s %f %f %f %f %f",&std1[i].num,std1[i].name,&std1[i].age,std1[i].sex,
    &std1[i].English,&std1[i].PE,&std1[i].Computer,&std1[i].Maths,&std1[i].Progrome);
                printf("%d %s %d %s %f %f %f %f %f\n",
    std1[i].num,std1[i].name,std1[i].age,std1[i].sex,
    std1[i].English,std1[i].PE,std1[i].Computer,std1[i].Maths,std1[i].Progrome);
              }
            else
              printf("file write error\n");
           }
        fclose (fp);
}
一点都不能运行!

论坛徽章:
0
8 [报告]
发表于 2008-05-13 15:02 |只看该作者
    fscanf(fp,"%d %s %d %c %f %f %f %f %f",&(std1[i].num),std1[i].name,&(std1[i].age),&(std1[i].sex),
&nbsp;&nbsp;&nbsp;&nbsp;&(std1[i].English),&(std1[i].PE),&(std1[i].Computer),&(std1[i].Maths),&(std1[i].Progrome));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d %s %d %c %f %f %f %f %f\n",
&nbsp;&nbsp;&nbsp;&nbsp;std1[i].num,std1[i].name,std1[i].age,std1[i].sex,
&nbsp;&nbsp;&nbsp;&nbsp;std1[i].English,std1[i].PE,std1[i].Computer,std1[i].Maths,std1[i].Progrome);


[ 本帖最后由 204tian 于 2008-5-13 15:09 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2008-05-13 19:19 |只看该作者

回复 #8 204tian 的帖子

谢谢!!!

论坛徽章:
0
10 [报告]
发表于 2008-05-13 21:15 |只看该作者
如果文件内容人类可以自然识别,那文件就是完全的纯文本文件。
你不能从文件读入到结构中,必须先当成字符串读入,在分析字符串
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP