input/output files, won't write to output

This seemed simple at first and I've followed many tutorials on the net.
Nobody else seems to have this problem.
I'm new to C++ with Java behind me.

My program takes (from a wordlist entitled input.txt) only words between 3 and 6 letters long and sends them to output.txt.

I think it might have something to do with confusing the i/o streams ?
I'm highly confused, any help would be greatly appreciated, thank you :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void chckstr () 
/*
gets the next word from input.txt,
checks that its between 3 and 6 letters
if so, writes the word to output.txt
*/
{
        string line;
  ifstream readfile ("input.txt");
  ofstream writefile ("output.txt", ios::in|ios::binary|ios::ate); //I don't know what ios::in etc. means...
  if ((readfile.is_open())&&(writefile.is_open()))
  {
    while (! readfile.eof() )
    {
      getline (readfile,line);
      if ((!(line.empty()))&&(line.size() <= 6) && (line.size() >=3))
      {
      cout << "pass:" << line << endl; // display that this word should be written
      writefile << line << endl; //write this word to output.txt (this is my problem)
      }
      else
      {
          cout << "fail:" << line << endl; //otherwise show that it isnt being written
      }
    }
    readfile.close();        //close the filestreams
    writefile.close();
  }

  else cout << "Unable to open file"; 

             
}

int main ()
{
  // declaring variables:
               
system ("CLS"); //this will clear the screen of any text from prior run
cin.clear(); //this will clear any values remain in cin from prior run

  // process:
             
    chckstr ();
    
  // terminate the program:
  system ("PAUSE");
  return 0;
}


The outputting to console is working like a dream, I see all the words that should be written and the ones that shouldn't... but I still have an empty output.txt file :(
Here is an error:
ofstream writefile ("output.txt", ios::in ...

You should use ios::out flag.
Topic archived. No new replies allowed.