I'm trying to make a small program to convert a basic text file to XML without using any other libraries. It is for an assignment, for the record. I've tried different options, but am at a loss.
When I run the program, it writes the information fine until it reaches the companies loop. After troubleshooting, I realize the issue is it isnt being fed into the vector properly. Advice?
Between line 34 and 35 there should be an ignore or get or something to read in the extra newline left in the buffer from operator >>. I would suggest putting something like
@Keskiverto It was to find out if the line was even being fed into the vector, thats all. I meant to delete it before I posted this.
@giblit That worked. I didn't even think of that. Appreciate it.
The second issue, however, is that the loop to access the vector isn't working properly. I haven't really messed with them much, but I thought I was using the right method of access, just like an array. It wont print anything to the file between the <company></company> tags.
int i = 0; i < companies.size(), i++; Here, you actually have i++ as your condition which checks if i is true (not 0) then increments. But, you set i equal to 0 first so it will never actually run. Maybe you meant for(int i = 0; i < companies.size(); ++i)
You read too much. (Two lines per iteration.)
Lets do more:
1 2 3 4 5
infile.ignore(1024, '\n');
while ( getline(infile, company) && endData != company )
{
companies.push_back( company );
}
Reads can fail. Now we test some.
Note1: Line 20, use const. You will not change endData, so you should be explicit.
Note2: Line 61, if you have range-for, then you have auto too. for ( autoconst & element : companies )
Sure, it is less explicit and you have to trace back to figure out the value_type of companies during code revision, but it is still a neat feature.
Note3: Lines 13-17 and 69-70.
1 2
ifstream infile( "data.txt" ); // in by default, open on construction
ofstream infile( "data.xml" ); // out by default, open on construction
Destructor of fstream closes open file, so explicit call to close() is not necessary.
It's not reading the second stuff twice, it's not finding the end of file correctly. Unfortunately I can't seem to find why it is not.
A few things I notice though:
1) line 36 only checks if it doesn't reach end of file, what if something else erroneous happened? I would do while(infile) instead.
2) line 38 could be removed if you moved line 30 there.
3) line 46 would probably be better if it was infile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
4) line 66 shouldn't it be /accounts instead of accounts? outfile << "\t</account>" << endl;
5) line 71 and 72 aren't necessary the destructor will call these for you.
while ( infile >> empNumber >> lastName >> firstName >> city >> state )
{
companies.clear();
infile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
while (getline(infile, company) && endData != company){
companies.push_back(company);
}
Regarding (2), creating the vector outside the loop allows it to retain its capacity and therefore decreases the number of reallocations that the push_backs are bound to induce. A micro-optimization. One could go one step further with it and reserve() some sane amount before the loop.