简单的问题,找不出原因
交谈中请勿轻信汇款、中奖消息,勿轻易拨打陌生电话。
魔鬼终结者(365108014) 23:07:30
#include "stdio.h "
typedef struct student
{
char num[6];
int score;
char sex;
}stu;
stu Input();
main()
{
FILE *fp;
stu name,*p;
if((fp=fopen( "G:\\TC201E\\Project\\students.txt ", "w ")==NULL))
{
printf( "can 't open this file\n ");
exit() ;
}
name=Input();
p=&name;
if(fwrite(&name,sizeof(stu),1,fp)!=1)
printf( "write error !\n ");
fclose(fp);
getch();
}
stu Input()
{
stu name1;
printf( "\ninput the student NO.: ");
scanf( "%s ",name1.num);
printf( "\ninput the student score: ");
scanf( "%d ",name1.score);
printf( "\ninput the student sex: ");
scanf( "\n%c ",name1.sex);
return name1;
}
魔鬼终结者(365108014) 23:07:53
这个文件怎么不能正常输入呢?
总是提示: "write error! "
[解决办法]
scanf( "%d ",&name1.score);
[解决办法]
程序有不少问题,帮你改了,自己对照看看
#include "stdio.h "
typedef struct student
{
char num[6];
int score;
char sex;
}stu;
stu Input();
void main()
{
FILE *fp;
stu name,*p;
if((fp = fopen( "students.txt ", "w ")) == NULL)
{
printf( "can 't open this file\n ");
exit() ;
}
name=Input();
p=&name;
if(fwrite(&name,sizeof(stu),1,fp)!=1)
printf( "write error !\n ");
fclose(fp);
getch();
}
stu Input()
{
stu name1;
printf( "\ninput the student NO.: ");
scanf( "%s ",name1.num);
printf( "\ninput the student score: ");
scanf( "%d ",&(name1.score));
printf( "\ninput the student sex: ");
scanf( "\n%c ",&(name1.sex));
return name1;
}
// 我把你的文件名改了
G:\\TC201E\\Project\\students.txt
改成了
students.txt
这样的话就几乎不会失败
如果是
G:\\TC201E\\Project\\students.txt
那你要确定你有
G:\\TC201E\\Project\\ 这个路径
[解决办法]
scanf的用法需要好好看看
printf( "\ninput the student NO.: ");
scanf( "%s ",name1.num);
printf( "\ninput the student score: ");
scanf( "%d ",name1.score);
printf( "\ninput the student sex: ");
scanf( "\n%c ",name1.sex);
在name前加&
[解决办法]
//Devcpp下没问题
#include "stdio.h "
typedef struct student
{
char num[6];
int score;
char sex;
}stu;
stu Input();
int main()
{
FILE *fp;
stu name,*p;
if( ( fp = fopen( "G:\\TC201E\\Project\\students.txt ", "w ") )==NULL )
{
printf( "can 't open this file\n ");
return 0;
}
name=Input();
p=&name;
if(fwrite(&name,sizeof(stu),1,fp)!=1)
printf( "write error !\n ");
fclose(fp);
getchar();
}
stu Input()
{
stu name1;
printf( "\ninput the student NO.: ");
scanf( "%s ",name1.num);
printf( "\ninput the student score: ");
scanf( "%d ",&name1.score);
printf( "\ninput the student sex: ");
scanf( "\n%c ",&name1.sex);
return name1;
}
[解决办法]
Chiyer(星羽) ( ) 信誉:100
个人认为用相对路径当然不会导致文件创建的问题,但是会有其他更严重的问题,因为相对路径会导致文件的创建的位置无法确定,虽然程序不报错,但是不是自己想要的结论
我想楼主是要找到问题的原因,而不是隐藏、掩盖问题
[解决办法]
一个右括号引发的血案……
调试了一下,发现创建文件后,fp=NULL,写文件的时候当然error了.
看这句:if((fp=fopen( "G:\\TC201E\\Project\\students.txt ", "w ")==NULL))
==比=优先级高,先计算fopen( "G:\\TC201E\\Project\\students.txt ", "w ")==NULL,结果是0(false),然后再赋给fp……
if((fp=fopen( "G:\\TC201E\\Project\\students.txt ", "w ")==NULL))
改为
if((fp=fopen( "G:\\TC201E\\Project\\students.txt ", "w "))==NULL)
scanf的问题就不说了
[解决办法]
上面那个程序是按照你原来的改的,提几点建议:
1,返回一个结构体既浪费时间又浪费空间;
2,结构体定义的不合理(需要占16个字节),若改成下面这样则只需要12个字节.
typedef struct student
{
char num[6];
char sex;
int score;
}stu;
3,不要轻易使用fwrite,容易出错,特别是像在写结构体之类的结构数据类型时.
4,if((fp=fopen( "G:\\TC201E\\Project\\students.txt ", "w ")==NULL))这条语句最好分开写,一时读起来比较容易,二是有效地控制了出错的机率.
5,自动变量最好定义时初始化(其实全局和静态变量也最好是定义时显示初始化).