【发布时间】:2026-01-31 02:50:01
【问题描述】:
我需要一些帮助... 我有这个数据文件accounts.txt:
stefan 20 50 60
anelia 2130 452 5200
atanas 52.3 560 45
peychev 258 852 654
ivan 1 2 3
petyr 4 5 6
me 48 84 57
you 57 48 56
Jordan 1000 0 0
asd 12 13 14
bdp 15 16 17
我需要将文件中的信息读入结构数组。 这是我的代码:
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
const int N=1000;
int n;
struct account {
char name[30];
double blv;
double usd;
double euro;
};
void sortirane (int n);
void main() {
cin>>n;
....
}
这是函数:
void sortirane(int n) {
ifstream file1("accounts.txt");
if(!file1) { cerr<<"Error!"; return; }
account b[N];
for(int i=0;i<n;i++)
file1>>b[i].name>>b[i].blv>>b[i].usd>>b[i].euro;
for (int j=0;j<n;j++)
cout<<b[j].name<<b[j].blv<<b[j].usd<<b[j].euro;
}
不幸的是,第一个循环结束后数组为空...
【问题讨论】:
-
尝试自己做功课,如果您有具体问题,请在此处提问,向我们展示您的尝试。在那之前,删除这个问题,因为它完全是题外话。具体来说,使用调试器。
-
不要使用全局变量,使用
std::string代替 C 字符数组,使用std::vector代替原始数组。