64,662
社区成员
发帖
与我相关
我的任务
分享
float b;
fstream fp;
fp.open("data.txt");
while(!fp.eof())
fp>>b;
cout<<b<<endl;
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
void main()
{
float b;
fstream fp;
fp.open("data.txt");
if (!fp.good())
{
cout << "文件打开失败" << endl;
return;
}
while(!fp.eof())
{
fp >> b;
cout << b << endl;
}
}
while(!fp.eof())
{
fp>>b;
cout<<b<<endl
}
while(!fp.eof())
fp>>b;
// End while
cout<<b<<endl;
你只有一次输出怎么可能输出那么多数据?!
你是不是代码修改后没有编译?!#include <stdio.h>
#include <string.h>
char s[80];
double d;
char c;
int main() {
while (1) {
printf("Input a double:");fflush(stdout);
rewind(stdin);
fgets(s,80,stdin);
if (strlen(s)>20) {
printf("Input too long(>20)!\n");
continue;
}
if (2==sscanf(s,"%lf%c",&d,&c)) {
if ('\n'==c) {
printf("%lg is a double.\n",d);
break;
}
}
if ('\n'==s[strlen(s)-1]) s[strlen(s)-1]=0;
printf("\"%s\" is NOT a double!\n",s);
}
return 0;
}