| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 624 人关注过本帖
标题:菜b有个课程设计。我前面一小部分就出问题了。写入文件怎么出错儿了?
只看楼主 加入收藏
saber930490
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2012-12-20
结帖率:66.67%
收藏
已结贴  问题点数:18 回复次数:6 
菜b有个课程设计。我前面一小部分就出问题了。写入文件怎么出错儿了?
#include<stdio.h>
struct student
{
    char name[10];
    char sex;
    int phone;
    char adress[20];
    int QQ;
};

int main()
{
    int i;
    struct student stu[10]={{"Lily",'W',111123,"apple road",56443256},
    {"Bob",'M',656411,"banaba road",83254467},
    {"Tom",'M',897523,"tower road",564432123},
    {"Kity",'W',495821,"rain road",56458369},
    {"Tony",'M',367498,"gold road",564454781},
    {"Sed",'M',232599,"air road",294658715}};
FILE*fp;
    fp=fopen("D:\\"stu.dat","wb");
    for(i=0;i<10;i++)
        fread(&student[10],sizeof(struct student),1,fp);
    fclose(fp);
for (i=0;i<10;i++)

printf("%s\t%c\t%5d\t%s\t%5d\n",stu[i].name,stu[i].sex,stu[i].phone,stu[i].adress,stu[i].QQ);
return 0;
}





í¨Ñ¶Â¼.cpp
C:\Users\Administrator\Desktop\Debug\D½¨Îļt¼D (10)\í¨Ñ¶Â¼.cpp(22) : error C2146: syntax error : missing ')' before identifier 'stu'
C:\Users\Administrator\Desktop\Debug\D½¨Îļt¼D (10)\í¨Ñ¶Â¼.cpp(22) : error C2660: 'fopen' : function does not take 1 parameters
C:\Users\Administrator\Desktop\Debug\D½¨Îļt¼D (10)\í¨Ñ¶Â¼.cpp(22) : error C2001: newline in constant
C:\Users\Administrator\Desktop\Debug\D½¨Îļt¼D (10)\í¨Ñ¶Â¼.cpp(23) : warning C4552: '<' : operator has no effect; expected operator with side-effect
C:\Users\Administrator\Desktop\Debug\D½¨Îļt¼D (10)\í¨Ñ¶Â¼.cpp(23) : error C2143: syntax error : missing ';' before ')'
Ö′DD cl.exe ê±3ö′í.

í¨Ñ¶Â¼.exe - 1 error(s), 0 warning(s)
搜索更多相关主题的帖子: apple phone include 
2013-01-01 22:27
yaobao
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:4
帖 子:1854
专家分:4121
注 册:2012-10-25
收藏
得分:0 
这样试试
程序代码:
#include<stdio.h>
struct student
{
    char name[10];
    char sex;
    int phone;
    char adress[20];
    int QQ;
};

int main()
{FILE *fp;
    int i;
    struct student stu[10]={{"Lily",'W',111123,"apple road",56443256},
    {"Bob",'M',656411,"banaba road",83254467},
    {"Tom",'M',897523,"tower road",564432123},
    {"Kity",'W',495821,"rain road",56458369},
    {"Tony",'M',367498,"gold road",564454781},
    {"Sed",'M',232599,"air road",294658715}};

    fp=fopen("D:\\stu.txt","wb");
    for(i=0;i<10;i++)
        fwrite(&stu[0],sizeof(struct student),1,fp);
    fclose(fp);
for (i=0;i<10;i++)

printf("%s\t%c\t%5d\t%s\t%5d\n",stu[i].name,stu[i].sex,stu[i].phone,stu[i].adress,stu[i].QQ);
return 0;
}


认认真真的学习,踏踏实实的走路:戒骄戒躁!!!
2013-01-01 23:08
yaobao
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:4
帖 子:1854
专家分:4121
注 册:2012-10-25
收藏
得分:0 
马小虎楼主

认认真真的学习,踏踏实实的走路:戒骄戒躁!!!
2013-01-01 23:09
古丁高手
Rank: 1
来 自:地狱
等 级:新手上路
帖 子:56
专家分:8
注 册:2012-8-8
收藏
得分:0 
声明必须放在函数开头,貌似C99之后是变了吧?我看书说的

不要管我是谁,我也不管你是谁!
just kiss myass!
2013-01-02 02:46
saber930490
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2012-12-20
收藏
得分:0 
回复 2楼 yaobao
哦哦。其实这些问题我都不太明白 - -。又好好看了看书。请问为何我程序运行之后在D盘建立的txt文件中是乱码?
2013-01-02 09:30
lupeifeng123
Rank: 2
等 级:论坛游民
帖 子:74
专家分:47
注 册:2012-12-13
收藏
得分:0 
刚学到这点上,学习了!!!!

一步一个脚印,贵在坚持。
2013-01-02 09:32
yaobao
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:4
帖 子:1854
专家分:4121
注 册:2012-10-25
收藏
得分:18 
程序代码:
#include<stdio.h>
struct student
{
    char name[10];
    char sex;
    long phone;
    char adress[20];
   long QQ;
};

int main()
{FILE *fp;
    int i;
    struct student stu[10]={{"Lily",'W',111123,"apple road",56443256},
    {"Bob",'M',656411,"banaba road",83254467},
    {"Tom",'M',897523,"tower road",564432123},
    {"Kity",'W',495821,"rain road",56458369},
    {"Tony",'M',367498,"gold road",564454781},
    {"Sed",'M',232599,"air road",294658715}};

    fp=fopen("D:\\stu.txt","wb");
    for(i=0;i<10;i++)
        fprintf(fp,"%s  %c  %ld  %s  %ld",stu[i].name,stu[i].sex,stu[i].phone,stu[i].adress,stu[i].QQ);
    fclose(fp);
for (i=0;i<10;i++)

printf("%s\t%c\t%5d\t%s\t%5d\n",stu[i].name,stu[i].sex,stu[i].phone,stu[i].adress,stu[i].QQ);
return 0;
}
数据类型不匹配,int只有5位,fwrite不能格式写入

认认真真的学习,踏踏实实的走路:戒骄戒躁!!!
2013-01-02 09:43
快速回复:菜b有个课程设计。我前面一小部分就出问题了。写入文件怎么出错儿了?
数据加载中...
 
   



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

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