Thread: input/output

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    25

    input/output

    Hello everyone,

    I'm trying to figure out how input/output files work. I got warnings, and it compiled, but when I tried to run the program, it gave me

    input file failed
    press any key to continue

    I thought we are supposed to list the directory, and my input file is saved in the same folder, and the same directory, any suggestions??

    Thanks a million!!

    p.s. this is only the partial code.

    Code:
    int main()
    {
      string first_name;            //declare string variable first name
      string last_name;             //declare string variable last name
      string gender;                //declare string variable gender
      int age;                      //declare int variable age
      string type_coverage;         //declare string variable type of coverage
      string club_member;           //declare string variable health club
                                    //member
      string smoker;                //declare string variable smoker
      int zip_code;                 //declare int variable zip code
      float basic_premium;          //declare int variable basic premium
      float discount_premium;		//declare int variable discount premium
      float premium;				//declare int variable premium
    
    
      ifstream inFile;              //declare file streams
      ofstream outFile;
    
      inFile.open("C:\Windows\C++\proj4.in.txt");      //opens the input file
      outFile.open("C:\Windows\C++\proj4.out.txt");    //opens the output file
    
      if(!inFile)
      {
        cout << "Input file failed!" << endl;
        return 1;                   //terminates the program if input fails
      }

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    Code:
    if (!inFile.is_open())
    {
        //terminate program
    }
    notice the .is_open() part
    PHP and XML
    Let's talk about SAX

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    25
    Hi, Waldo2k2,

    Thanks for the suggestions.... however, I tried it and it gave me the same thing.... I think the program can't find the input file.....
    I tried my last project in UNIX system without the "C:\\windows\C++" part and it works with the same structure.

    Now, I'm trying to modify the same program but using the visual C++ so it is easier to debug, and I saved the input file in the same folder in the directory, but it would not work....... any more suggestions??

    Thanks so much!!

  4. #4
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    oh wow, i missed the biggest mistake,
    Code:
    inFile.open("C:\Windows\C++\proj4.in.txt");      //opens the input file
    since you used unix before you used forward slashes for the path, in windows it's the backslashes, the problem is in c++ the backslash is used for character escape sequences. So, you need to change \ to \\. then it will work. I bet those are the warnings you're getting as well.

    And by the way leave is_open() as it is, that is the correct way to do it. You can also save time by doing:
    Code:
    ifstream inFile("c:\\windows\\c++\\proj4.in.txt");
    PHP and XML
    Let's talk about SAX

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    25
    Hi Waldo2k2

    Cool!!! It worked!!! and it made sense too... it is funny that you learned the mistakes that you made and you realized that
    "duh! Didn't I know this before?!!" I guess that comes with experiences.....

    Thanks so much again!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with input/output files
    By aldin176 in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2006, 09:04 AM
  2. Input/Output Streams
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 12-23-2005, 02:58 PM
  3. filestreams (fstream) and standard input/output (stdlib)
    By Shadow12345 in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2002, 10:14 AM
  4. FILE input/output and ARRAY
    By poorman in forum C Programming
    Replies: 1
    Last Post: 03-05-2002, 04:17 PM
  5. Input/Output
    By PaulMelia in forum C Programming
    Replies: 3
    Last Post: 12-02-2001, 08:13 AM