Input File Error

This is a discussion on Input File Error within the C++ Programming forums, part of the General Programming Boards category; Hello Everybody, I've been trying to figure out why my code will not read from my file for the past ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    13

    Input File Error

    Hello Everybody,
    I've been trying to figure out why my code will not read from my file for the past 7 hours - I kid you not. I have a new found respect for the people that do understand the problem and exetreme appreciation to the individual(s) who can guide me in correcting the errors. I have about 3 weeks of Bloodshed experience, so I maybe slow on the uptake.
    Thank you


    I keep getting "Error opening, CPP3_1, file" - I've tried a variety of "C:\\Users\\nathan\\Desktop\\C\\MY Work\\CPP3_1.txt" too

    Text on file: 150
    Code:
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    #define FLUSH  cin.clear(); while(cin.get()!='\n');
    
    int main( )
    {
        ifstream InFile;
        int numIn;
    
       cout << fixed << showpoint << setprecision(2);
    
       InFile.open("C:\\CPP3_1.txt");
       if(!InFile)
       { 
         cerr << "\nError opening, CPP3_1, file." << endl;
       }
    
    
       //else cout << "\nOpened file.\n";
    
       cout << "\nReading a text file.\n";
    
          
       while ( InFile >> numIn )
       {  
          cout << numIn << " \n";
       }
    
       cout << "I hate this" << endl;
       
       InFile.close();
       cout << endl;
       cin.get();
       system("pause");
       return 0;
    
    
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Well, you need to know where the file is in order to open it. Just guessing random directories is probably a bad idea.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    change your numIn type to be chat since your reading char here

    Code:
    while ( InFile >> numIn )
       {  
          cout << numIn << " \n";
       }
    And then try readin ti again. Perhaps you should also open the file for reading by specifing it in the opne function call.

    Code:
    InFile.open("C:\\YServer.txt", ios::in );
    ssharish

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    13
    I'm lost and I'm not trying guess.
    The title of my text file is "CCP3_1", the only thing on the file is the number "150". This is the address on my computer "C:\Users\nathan\Desktop\C\MY Work\CPP3_1". I've tried a myriad of slightly modified addresses since the original doesn't read.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You're trying to open C:\CPP3_1.txt, but then you say the file is elsewhere, and you've tried many variations. That sounds like guessing to me. Get with it. Do you even know what you're opening? Sounds like your file doesn't even have an extension if what you posted is accurate.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    13
    Macgyver - What do you mean "get with it"? Look man - I don't have as much C++ knowledge as you do - I'm trying to use the knowledge and resources I have to solve my problem. If you don't want to help, so be it.

    Here is how the corrected code reads now - it still will not open:

    Code:
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main( )
    {
       ifstream InFile;
       char numIn;
    
       cout << fixed << showpoint << setprecision(2);
    
      InFile.open("C:\\Users\\nathan\\Desktop\\C\\MY Work\\CPP3_1", ios::in );
       if(!InFile)
       { 
         cerr << "\nError opening, Crappy, file." << endl;
         
       }
       //else cout << "\nOpened file.\n";
    
       cout << "\nReading a text file.\n";
    
       while ( InFile >> numIn )
       {  
          cout << numIn << " ";
       }
    
       InFile.close();
       cout << endl;
       system("pause");
       return 0;
    }

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Maybe because "My work" has a space? Try it, though its just a wild guess

    If it has .txt extension include it on the above code

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Spaces are irrelevant.
    Place the file in C:\
    Then try
    InFile.open("C:\\CPP3_1.txt");
    Or
    InFile.open("C:\\CPP3_1");
    One of them should work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    13
    I tried - it didn't work - thank you

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    13
    Elysia - by moving it directly into C:\ - it worked!!! You are an extremely awesome person and I can't thank you enough!!!!

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Then your path to the file is incorrect.
    You can use that to continue troubleshooting if you wish.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jun 2008
    Posts
    13
    Elysia - you are right. The path was incorrect because I had named the folder and text file the same thing - I just never caught on to it.

    Wrong:
    Code:
     
    InFile.open("C:\\Users\\nathan\\Desktop\\C\\MY Work\\CPP3_1", ios::in );
    Right:
    Code:
    InFile.open("C:\\Users\\nathan\\Desktop\\C\\MY Work\\CPP3_1\\CPP3_1.txt");
    Thank you again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

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