fstream.h help, opening a file from location

This is a discussion on fstream.h help, opening a file from location within the C++ Programming forums, part of the General Programming Boards category; how do you open a file from a user specified location using fstrea.h, i've been trying file.open("location specified") because i ...

  1. #1
    Unregistered
    Guest

    Exclamation fstream.h help, opening a file from location

    how do you open a file from a user specified location using fstrea.h, i've been trying

    file.open("location specified")

    because i was told thats how to do it but obviously its not working! any help would be greatly appreciated!

    MinnoW

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    try this........


    ifstream fin("c:/autoexec.bat");

    --- you're declaring an object of an ifstream to read from a file..... and initialize it to open the file above....

    ....

    there's couple ways you can open a file.....don't limit yourself to only one....

    good luck..

    Regards,
    matheo917

  3. #3
    Unregistered
    Guest
    thank you very much!!!

  4. #4
    Unregistered
    Guest
    ok, i declared it:

    ifstream file("location");

    location is found by:

    cout<<"Where is the file located(ie; c:\\frunlog.txt):";
    getline(cin,location);

    (Keep in mind i'm a beginning programmer)

    how do i open the file now? or be able to use it? do i have to do something like file.open("what do i enter here") since it would normally be the name.

    what i'm doing is opening the text and assinging the entire thing to an apstring to search through and what not. right now i am not able to open any file unless i declare the ifstream regularly and just put the text i want to open in the same directory as the prog and specify the name. I hope i didn't lose anyone. any help would be great!

  5. #5
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    oki doki....

    like i previously stated....

    ifstream fin("c:/autoexec.bat");

    declares an object (stream) and ALREADY

  6. #6
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    got cut off.......

    any ..........it already opens a file....

    now the trick is in reading the contents of a file.......

    hint: remember there's couple of different ways of "opening" and "closing" files.......
    ifstream fin("c:/autoexec.bat");
    Code:
               string str1;
               
               while (!fin.eof())
                    {
                       getline(fin, str1, '\n');
                       cout << fin << endl;
                    }
    
               fin.close();
    this snippet ..... declares a string and generates a loop that will execute until it encounters an END OF FILE .... and "getline" will read the contents into "fin" until it encounters an END OF LINE '\n'.......

    hope this helped..

    Regards,
    matheo917

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you need to use a null terminated char array as the argument for the open() method of streams. You can't use instances of a string class.

    //ok
    ifstream fin("myFile.txt");

    //ok
    ofstream fout(C:\\myDocuments\\BCB\\homework\\day44.txt");

    //ok
    char filename = "myFile.txt";
    ifstream fin(filename);

    //not ok
    apstring filename1 = "myFile.txt";
    ofstream fout(filename1);

    //if c_str() returns the char array upon which apstrings are built
    //ok
    ofstream fout(filename1.c_str());

  8. #8
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    If you are using dos or windows you have to use "\\" on unix or other similar os's (linux) you use / to select a directory. Hope this helps
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  9. #9
    Unregistered
    Guest
    YEEE HAAAA, i got to work, thanks to all of you very much!
    What i ended up doing was:

    ifstream file(location.c_str());

    then:

    while(!file.eof())
    {
    getline(file, text);
    main = main + "\n" + text;
    }

    since it kept saying "getline does not take 3 parameters" when i had getline(file, text, '/n')

    thanks again!

  10. #10
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    it does take 3 parameters.....


    hmmmm???.......

    what compiler are you using...??

  11. #11
    Unregistered
    Guest
    Microsoft Visual C++ 6.0(theres probably more info you need but i'm not sure what it would be)

  12. #12
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    i've never used Micorsoft V. C++ , but i am pretty sure it should take 3 parameters, the last parameter is just a delimeter.......
    hmm... i am using Borland's C++ Builder....and everything works fine..

    hmmmm

    ......
    anyway.... I am glad you got it to work

    Regards,
    matheo917

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, 11:39 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM

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