I'm making a program that takes the words from one text or data file and transferes it to another. there are not supposed to be any spaces in the second file. I need to know why I can't make this code work. It would not be the completed code but more like a place to start.

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

int main ()
{
    char in;
    ifstream infile;
    ofstream outfile;

    infile.open("in.dat",ios::in);
    outfile.open("out.dat",ios::out);

    if ((!infile) || (!outfile))
    {
        cout << "error opening file\n";
        return 0;
    }

    do
    {
        infile >> in;
        cout << in;
    } while(!infile.eof())


    infile.close ();
    outfile.close ();
    return 0;
}

I am having a problem with the compiler I think. When I try to build the program I get an error message that says:

syntax error : missing ';' before identifier 'infile'

but you will notice that i do have a semicolol there, right after char in;
thanx to anyone who helps

I can't find the problem but wouldn't it be better to have a while loop instead of a do-while. You never check to make sure the infile has anything in it to start with.

What exact line does the compiler say is wrong?

do..while loops end with a semicolon:

} while(!infile.eof());

But, tell me what happens when the input file is empty? Your do..while loop will still process a character, but it will be uninitialized and the result is undefined behavior. A while loop is better, but don't use stream.eof() as the loop condition, bad things can happen. Try this instead:

while ( infile.get ( in ) )
  cout<< in;

Also, the way you use the file streams is awkward. You can open the file through the constructor (and ignore the ios::in and ios::out parts because you use ifstream and ofstream, respectively. They're only needed for fstream objects) and the destructor closes the files automagically:

#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
  char in;
  ifstream infile ( "in.dat" );
  ofstream outfile ( "out.dat" );

  if ((!infile) || (!outfile))
  {
    cout << "error opening file\n";
    return 0;
  }

  while ( infile.get ( in ) )
    cout<< in;

  return 0;
}

thanx a ton :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.