Searching from an input file, writing to another.

I'm trying to make a program to "copy and paste" the rest of the lines starting with " Frequencies --" from a data file to a new file. I have the .exe in the folder with the .OUT files (my inputs.) The .DAT files I create remain at 0KB and the program runs forever.



#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string.h>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
ifstream infile;
ofstream outfile;

char filename[64];
char string[256];
long linenumber=1;

cout << "Enter filename" << '\n';
cin >> filename;
cin.ignore(150, '\n');

infile.open( strcat(filename, ".OUT"), ios::in);
outfile.open( strcat(filename, ".DAT"), ios::out);

infile.unsetf(ios::skipws);

do
{
cout << linenumber << '\n';
infile.get(string, 15);
if (strcmp(" Frequencies --", string)==0)
{
infile.getline(string, 256);
outfile << string << endl;
linenumber++;
}
else
{
infile.getline(string, 256);
linenumber++;
}
} while (!infile.eof());

outfile.close();

cout << "Success";


system("PAUSE");
return EXIT_SUCCESS;
}

hey, ok,
you have while (!infile.eof()); after a curly bracket. should'nt be where the do is instead of it. and also i think the strcat(filename, ".OUT") and strcat(filename, ".DAT") make it read the filename as .OUT'filename' , anyway look it up. consider making a different file, and cut this code down to work with the basics. like using .txt's. in other words do the fancy things when you can build off basic stuff.
The "while" condition is proper (consulted my previous text from years ago) and the output file is named the way I want. The output of this program is filename.OUT.DAT.
I believe the error may lie in the do-while loop, but I'm not sure. When I run the program, It outputs infinitely more line-numbers than there are lines to the console, but nothing to the file. The output file IS created, though.
Topic archived. No new replies allowed.