ifstream problem

This is a discussion on ifstream problem within the C++ Programming forums, part of the General Programming Boards category; I'm trying to use files, but have an error. Code: #include <iostream> #include <fstream> using namespace std; int main(int argc, ...

  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    ifstream problem

    I'm trying to use files, but have an error.
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main(int argc, char *argv[]) {
     if(argc != 3) {
      cout << "Usage: cpp <infile (.cpp)> <outfile (.html)>" << endl;
      return 1;
     }
     ifstream in(argv[1], (ios::in | ios::trunc));
     if(!in.is_open()) {
      cout << "Error opening '" << argv[1] << "'." << endl;
      return 1;
     }
     ofstream out(argv[2], ios::out);
     if(!out.is_open()) {
      cout << "Error opening '" << argv[2] << "'." << endl;
      return 1;
     }
     in.close();
     out.close();
    }
    I ran the program with the command line: "cpp one.cpp out.html" where one.cpp and out.html are files that are in the same directory, but I always get "Error opening 'out.cpp'." Anyone got any ideas? Thanks !
    Do not make direct eye contact with me.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You're trying to open an input stream using an output only flag (trunc). You cannot write to an istream, therefore you cannot truncate it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Duh - thanks, I must have been thinking backwards .
    Do not make direct eye contact with me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  2. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 08:29 PM
  3. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 01:30 PM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 06:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21