1
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;

int main() 
{

    string temp;

    ifstream inFile;
    ofstream outFile;

    inFile.open("ZRMK Matched - 010513.txt");
    outFile.open("second.txt");

    while(!inFile.eof()) {  

        getline(inFile, temp);
        if (temp != "") {
            getline(inFile, temp);
            outFile << temp;
        }
    }

    cout << "Data Transfer Finished" << endl;

    return 0;
}

I'm having difficulty getting this to work. When I execute the program it cycles for awhile and then terminates without finishing -- it doesn't output any lines of text to the output file. Any help would be appreciated.

Caesar
  • 9,483
  • 8
  • 40
  • 66
  • Have you used the debugger? Does it output your "Data Transfer Finished" message? – Chad Feb 13 '13 at 18:23
  • Have you tried flushing the outfile? – Srikant Krishna Feb 13 '13 at 18:24
  • 1
    Is the fact that you call getline twice per iteration (first to check `(temp != "")` and second to write to outfile) intentional? – fvu Feb 13 '13 at 18:24
  • Are you trying to copy every second line in `inFile` to `outFile`? Also, [read this](http://stackoverflow.com/q/14615671/150634). – Joseph Mansfield Feb 13 '13 at 18:25
  • 1
    It would help the cause of analyzing your problem *tremendously* if we know (a) what your program is *supposed* to do, (b) what the input data looks like, and (c) why you think your code *should* work but doesn't *seem to*. – WhozCraig Feb 13 '13 at 18:29
  • 2
    The is probably not your immediate problem, but you should never use `.eof()` in a loop condition. Doing so almost always results in a buggy program. – Robᵩ Feb 13 '13 at 18:31
  • To repeat what the previous comment says, **DO NOT WRITE `while(!inFile.eof())`, THAT IS NOT HOW TO READ FROM A STREAM** – Jonathan Wakely Feb 13 '13 at 18:53
  • Do not copy files; let your operating system do that. Operating Systems are generally more efficient at file manipulation than your program. After all, your program invokes OS functions for file manipulation. – Thomas Matthews Feb 13 '13 at 19:51
  • Why are you including AND ? Isn't enough ? Also, you should close the opened streams. – Cyril Leroux Feb 14 '13 at 00:02

2 Answers2

5

Are you trying to copy every line?

while(std::getline(inFile, temp)) {
  outFile << temp << "\n";
}

Are you trying to copy every non-blank line?

while(std::getline(inFile, temp)) {
  if(temp != "")
    outFile << temp << "\n";
}

Are you trying to copy every 2nd non-blank line?

int count = 0;
while(std::getline(inFile, temp)) {
  if(temp == "")
    continue;
  count++;
  if(count % 2)
    outFile << temp << "\n";
}

Are you simply trying to copy the entire file?

outFile << inFile.rdbuf();
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

You should use a mode to open files : see std::ios_base::openmode
And don't forget to close the streams you open !
You can even try catch your code to understand the problem if an exception occurred.

#include <string>
#include <fstream>
#include <iostream>

using namespace std;

int main() 
{
    try {
        fstream inFile;
        fstream outFile;

        // open to read
        inFile.open("ZRMK Matched - 010513.txt", ios_base::in); 
        if (!inFile.is_open()) {
            cerr << "inFile is not open ! " << endl;
            return EXIT_FAILURE;
        }

        // Open to append
        outFile.open("second.txt", ios_base::app); 
        if (!inFile.is_open()) {
            cerr << "inFile is not open ! " << endl;
            return EXIT_FAILURE;
        }

        string line;
        while(getline(inFile, line)) {  
            if (!line.empty()) {
                outFile << line << endl;
            }
        }

        if (outFile.is_open()) {
            outFile.close(); // Close the stream if it's open
        }
        if (inFile.is_open()) {
            inFile.close(); // Close the stream if open
        }

        cout << "Data Transfer Finished" << endl;
        return EXIT_SUCCESS;

    } catch (const exception& e) {
        cerr << "Exception occurred : " << e.what() << endl;
    }

    return EXIT_FAILURE;
}
Cyril Leroux
  • 2,599
  • 1
  • 26
  • 25