c++ - Inputting and Outputting Data from file in Vector Error? -
i new c++ , not sure why output file blank. think might have functions? when put code main , not function, output file give me information.. not sure doing wrong.
#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 = 1; < 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; } }
here data text file.
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
you passing copies of vector inputdata
/outputdata
- make these references instead, i.e. change:
void inputdata(fstream &fs, vector<eip> name)
and
void outputdata(fstream &fsout, vector<eip> name)
to:
void inputdata(fstream &fs, vector<eip> &name)
and
void outputdata(fstream &fsout, vector<eip> &name)
Comments
Post a Comment