Thread: file opening/ file io questions

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    221

    file opening/ file io questions

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream.h>
    
    using namespace std;
    
    int main()
    {
        int array[20], i;
    
        ifstream instream;
    
        instream.open("test.dat");
    
        for (i=0; i<10 ; i++)
        {
            instream >> array[i];
        }
    
        for (i=0; i < 10; i++)
        {
            cout << array[i] << endl;
        }
        system("PAUSE");
        return 0;
    }
    the file, i know, is in the same directory as where the cpp file is saved.. why woudlnt the file be opening?

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    i'm pretty sure that you should get the character first and then insert it into the array after
    C++ can hurt.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    cin.get
    or something?
    how would u do that.. i havent learned any of the cout./cin. functions yet

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    1) You never test if the file has been successfully opened ( instream.fail() ).
    2) #include <fstream> instead of #include <fstream.h> if you're using namespaces.
    3) No need to include iostream if you've included fstream.
    4) Are you sure the file contains 10 integer values in text form, separated by either spaces or linebreaks?
    5) Shouldn't matter, but you could try to open it as open("test.dat", ios::in);
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by scottmanc
    i'm pretty sure that you should get the character first and then insert it into the array after
    That shouldn't matter.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    Originally posted by Magos
    1) You never test if the file has been successfully opened ( instream.fail() ).
    2) #include <fstream> instead of #include <fstream.h> if you're using namespaces.
    3) No need to include iostream if you've included fstream.
    4) Are you sure the file contains 10 integer values in text form, separated by either spaces or linebreaks?
    5) Shouldn't matter, but you could try to open it as open("test.dat", ios::in);
    1. i did have a test, but i took it out for diagnosic purposes.
    2. ah!
    3. ah! x2
    4. yea, i created the file myself
    5. ill try that


    thanks

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    Well you should close the file after using it

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Sorry, a little correction. i made a test. If you use fstream.h (no namespace) you don't have to include iostream.h, but if you use fstream (namespace) you need iostream.
    Sorry for that!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Ivan!
    Well you should close the file after using it
    LOL, i missed that!

    Anyway, I compiled your code (after removing the .h in the includes) and it worked fine. You sure that your textfile is valid?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    What happens if u removes the '.h', I dont get it.
    But i'm just a n00b

  11. #11
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    the code compiles fine
    but it just wont open the file for editing

    im including the file im using to open.. (i renamed it to .txt so i could upload it)

  12. #12
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    i added the full path to where the file was located.. and it works
    io thought if u saved the .cpp file in the same path where the file u want opened is located, you dont have to include the path.. the
    "filename " means current path, or something

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    if you remove the ".h" you need to include a namespace such as

    using namespace std;
    C++ can hurt.

  14. #14
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by revelation437
    the code compiles fine
    but it just wont open the file for editing

    im including the file im using to open.. (i renamed it to .txt so i could upload it)
    What I meant was, I compiled the code (worked fine), made a txt file containing 10 numbers both separated by spaces and linebreaks. Then I ran the program and it successfully loaded the file with its ten numbers.

    I'm telling you, the code is fine. The problem lies elsewhere. I even tested with your txtfile and that works too (though it contains 11 integers so the last one is cut).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  15. #15
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    question 2: (dunno if i should post it here or in another thread... so here it goes)

    i want to check the pointer in the for loop to see if its the end of file
    would
    for (i=0; instream != EOF; i++)

    work?

    if not, how can i do something like that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM