我是C ++的新手,我不确定为什么我的输出文件是空白的。我想这可能与我的功能有关?当我把代码放入main而不是函数时,输出文件会给我信息..我不确定我做错了什么。
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <vector>
#define die(msg) {cerr << msg << endl; exit(1);}
using namespace std;
struct Eip
{
string block;
string blkSfx;
string lot;
string lotSfx;
};
void inputData(fstream &, vector<Eip>);
void outputData(fstream &, vector<Eip>);
int main()
{
fstream fs("Book1.txt", ios::in);
fstream fsout("SecuredEIP.txt", ios::out);
if(!fs.is_open()) die("Can not open Book1.txt!");
if(!fsout.is_open()) die("Can not open SecuredEIP.txt!");
vector<Eip> name;
inputData(fs, name);
outputData(fsout, name);
fs.close();
fsout.close();
return(0);
}
void inputData(fstream &fs, vector<Eip> name)
{
while(fs) {
Eip temp;
getline(fs, temp.block, '\t');
getline(fs, temp.blkSfx, '\t');
getline(fs, temp.lot, '\t');
getline(fs, temp.lotSfx, '\n');
name.push_back(temp);
}
}
void outputData(fstream &fsout, vector<Eip> name)
{
for(int i = 1; i < name.size(); i++) {
fsout << setw(4) << setfill('0');
fsout << name[i].block;
if(name[i].blkSfx == "")
fsout << " ";
else
fsout << name[i].blkSfx;
fsout << setw(3) << setfill('0');
fsout << name[i].lot;
if(name[i].lotSfx == "")
fsout << " " << endl;
else
fsout << name[i].lotSfx << " " << endl;
}
}
这是我的文本文件中的数据。
3965 1
837 9
3749 59
3752 19
3532 54
6769 49
535 10
819 13 B
3616 84
26 30
3732 8
3732 150
6536 8
71 2
答案 0 :(得分:1)
您将向量的副本传递给inputData
/ outputData
- 请改为使用这些引用,即更改:
void inputData(fstream &fs, vector<Eip> name)
和
void outputData(fstream &fsout, vector<Eip> name)
为:
void inputData(fstream &fs, vector<Eip> &name)
和
void outputData(fstream &fsout, vector<Eip> &name)