在矢量错误中输入和输出文件中的数据
Inputting and Outputting Data from file in Vector Error?
我对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
您正在将向量的副本传递给 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)
相关文章:
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- VI工作室代码错误无法打开输出文件主.exe
- 在代码块中查找输出文件
- 将未签名的字符写入输出文件
- 为什么我的代码不写入输出文件?
- 完成搜索任务时为空输出文件
- 如何将地图<列表<string>、矢量<string>>输出到输出文件?
- 无法在代码块 IDE 中打开输出文件.exe
- C++:更改 IF 语句中的输出文件
- 编译 gsoap 输出文件 (onvif) 时出现问题
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- while 循环中的流输出文件() 通常生成空文件
- 如何使用 Docker 访问输出文件
- 提升进程间共享内存删除、权限和输出文件
- 查找素数,然后将质数放入输出文件中
- GDB / C++ - 根据循环条件将多个变量记录到输出文件中
- 带有文件结束函数的 while 循环重复输出文件中的最后一个数字两次
- 我的输出文件未显示解决方案
- 输入文件中的第一个字符是输出文件中的最后一个字符,反之亦然
- C++,我正在尝试创建一个有效的.bmp输出文件