P10256: 科创项目代码提交
传统题
1.000s
时间限制
128MB
内存限制
59 提交
23 解决
【题目描述】
C++设计并开发一个应用软件,用来参加2024个中小学生信息素奍大赛和科技创新大赛!
【样例输入】复制
#include<bits/stdc++.h>
using namespace std;
struct GOODS{
int i;
double d;
char c;
string s;
};
struct GOODS goods[10005];
int g=0;
void initRead(){
//ifstream f1("try1.txt");
ifstream f1("try1.txt");
if (f1.is_open()) {
// 从文件中读取数据 end of file
while(!f1.eof()) { //eof 文件是否读结束
f1 >> goods[g].i;
f1 >> goods[g].d;
f1>>goods[g].c;
getline(f1,goods[g].s);
//f1.getline(cin,s);
// 输出读取的数据
//cout << "Number: " << goods[g].i << endl;
//cout << "Word: " << goods[g].d << endl;
//cout<<"char:"<<goods[g].c<<endl;
//cout<<goods[g].s<<endl;
g++;
}
f1.close();
} else {
// 文件打开失败,进行错误处理
cout << "无法打开文件进行读取操作。" << endl;
}
}
void write(){
ofstream f("try1.txt");
for(int i=0;i<g;i++){
f<<goods[i].i<<" "<<goods[i].d<<" "<<goods[i].c<<" "<<goods[i].s<<endl; //写入数据
}
f.close();
}
int main(){
initRead(); //初始化加载数据,读文件
for(int i=0;i<g;i++){
cout << "Number: " << goods[i].i << endl;
cout << "Word: " << goods[i].d << endl;
cout<<"char:"<<goods[i].c<<endl;
cout<<goods[i].s<<endl;
}
//write(); 存档,写文件
return 0;
}
【提示】
#include<bits/stdc++.h>
using namespace std;
int main(){
ofstream f("try1.txt");
f<<1234<<' '<<3.14159<<'A'<<"How are you"; //写入数据
f.close();
ifstream f1("try1.txt");
int i;
double d;
char c;
string s;
ifstream f1("try1.txt");
if (f1.is_open()) {
// 从文件中读取数据
f1 >> i;
f1 >> d;
f1>>c;
getline(f1,s);
//f1.getline(cin,s);
// 输出读取的数据
cout << "Number: " << i << endl;
cout << "Word: " << d << endl;
cout<<"char:"<<c<<endl;
cout<<s<<endl;
f1.close();
} else {
// 文件打开失败,进行错误处理
cout << "无法打开文件进行读取操作。" << endl;
}
return 0;
}