#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    File handling in c++


    Hello everyone, ive written a program to read inputs in a text file. The file named Input.txt contain integers 2 3 and 4. My program is as follows:

    Code:
    #include<iostream>
    #include<cstdlib> 
    #include<fstream>
    using namespace std; 
    int main ()
    {
        int x,y,z;
        
        ifstream f;
        
        f.open("Input.txt");
        
        f>>x>>y>>z;
       
       cout<<"x="<<x<<endl;
       cout<<"y="<<y<<endl;
       cout<<"z="<<z<<endl;
       
       f.close();
       system("pause");
       
       return 0;
       
    }
    the program is outputing x=2293616 , y=0 and z=0.
    Can someone hint me on the problem?
    :)
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Are you sure that's the content of the file. I tried it, and I got

    x=2
    y=3
    z=4
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Are you sure your file opened correctly? Maybe you should check to insure it is indeed opening correctly.

    Jim
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0
    Nope im not getting it. It's the simplest program in my tutorial u_u
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    What aren't you getting? If the file doesn't open then your variables will never be assigned values. Try initializing the variables before you try to read the file.

    Code:
    #include<iostream>
    #include<cstdlib> 
    #include<fstream>
    using namespace std; 
    int main ()
    {
        int x = 10, y = 20, z = 30;
        
        ifstream f;
        
        f.open("Input.txt");
        
        f>>x>>y>>z;
       
       cout<<"x="<<x<<endl;
       cout<<"y="<<y<<endl;
       cout<<"z="<<z<<endl;
       
       f.close();
       system("pause");
       
       return 0;
       
    }
    To check if your file opened correctly you should test the state of the stream after you try to open the file:

    Code:
    if(!f)
       cout << "ERROR file didn't open correctly" << endl;
    Jim
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by EmilyJohn
    How We Can Handle File In C And C++?
    In the prescribed manner. Now blow the dust off your textbook, open it, and read it. If you encounter problems, then come back and ask specific questions.

    BTW, you would handle file I/O in C++ in exactly the same manner as in C. Unless you decide to use iostreams.

    Also, next time start your own thread rather than hijacking somebody else's.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0
    Thank you Jim... i will try that :)

IMN logo majestic logo threadwatch logo seochat tools logo