How do I load a file name from string

This is a discussion on How do I load a file name from string within the C++ Programming forums, part of the General Programming Boards category; I am trying to use ifstream and I have the file location "c:\text.txt" saved into a string text and want ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    23

    How do I load a file name from string

    I am trying to use ifstream and I have the file location
    "c:\\text.txt" saved into a string text and want to have ifstream load the file loaction and name from that string

    is that possible?

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    I think you have the wrong forum. ifstream is C++.

    In C:
    Code:
    FILE *fp;
    char str[] = "text.txt";
    fp = fopen(str,"r");
    
      //do stuff
    
    fclose(fp);

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In C++:

    Code:
    ifstream i("c:\\test.txt");
    string s;
    i >> s;
    ifstream file(s);
    I think that will work.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I am trying to use ifstream
    I guess you want the C++ forum then! Moving thread...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    s.c_str() you mean right?

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    23
    I know this part

    ifstream a_file ("c:\\text.txt");

    a_file>>str;

    cout<<str;

    but instead of having "c:\\text.txt" I need to replace it with

    string result;

    so it would be like this

    ifstream a_file (result);
    a_file>>str;
    cout<<str;

    if that explains it better

    thanks for the help so far

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    string filename;
    string info;
    cout << "Enter file name!  : ";
    getline(cin, filename, '\n');
    ifstream in(filename.c_str());
    in >> info;
    //or
    getline(in, info, '\n);
    in.close();
    cout << info << endl;
    Last edited by ILoveVectors; 07-14-2005 at 06:40 PM.

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    23
    thanks ILoveVectors that is exatley what I was looking for.


    thanks for every one elses help to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM

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