新建一txt文件,输入数字,但是为什麼它无法保存我输入的内容呢?
c语言吧
全部回复
仅看楼主
level 4
fox19891989 楼主
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *myfile;
int i, n, a;
myfile = fopen("numbers.txt", "w");
if (myfile == NULL)
{
printf("Unable to open the file");
}
else
{puts("This program will save a list of numbers to a text file.");
printf("How many numbers do u wish to save? ");
scanf("%d",&n);puts("Now, enter the numbers.");
for ( i=0; i<n; i++ ){
printf("> ");
fscanf(stdin,"%d",&a);
printf("U have %d remaining numbers to enter.\n",n-i-1);}
printf("U have entered all the numbers!");
fclose (myfile);
}
return 0;
}
代码在这,如何改进
2011年04月03日 15点04分 1
level 7
没有任何一条语句是把数据写入文件的...
2011年04月03日 15点04分 2
level 4
fox19891989 楼主
回复:2楼
什麼意思?
该怎麼改?我第一次写这个
2011年04月03日 15点04分 3
level 7
把什么写进去?
2011年04月03日 15点04分 4
level 7
for ( i=0; i<n; i++ ){
printf("> ");
fscanf(stdin,"%d",&a);
printf("U have %d remaining numbers to enter.\n",n-i-1);}
printf("U have entered all the numbers!");
fclose (myfile);
}
是这一段不?
2011年04月03日 15点04分 5
level 4
fox19891989 楼主
e
回复:5楼
恩的,fscanf(stdin,"%d",&a);应该就是写入数字吧
2011年04月03日 16点04分 6
level 7
回复:6楼
把&去掉
2011年04月03日 16点04分 7
level 4
fox19891989 楼主
回复:7楼
变成a未初始化了,若把a初始化为0.也不对
2011年04月03日 16点04分 8
level 7
回复:8楼
原程序的数据读到n里去了...
2011年04月03日 16点04分 9
1