Thread: File handling in C++ vs C

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    8

    File handling in C++ vs C

    Hello i am a C programmer that is currently in between my freshman and sophomore year of college. I am attempting to teach myself C++ over the summer. I was just wondering how i can use the #include<fstream> statement to open a file name as a string variable, so that the user can input the name of the file. I don't need you to show me any code, more just a flat out explanation of how the <fstream> works and how i can implement it for my practice program.

    Thank you in advance, seeing as you managed to put enough time aside to read this far.

    Just a heads up im not going to check for a reply until the morning i got a lot of material in today and am exhausted.
    Last edited by rfclark; 07-22-2008 at 11:58 PM. Reason: don't wait up

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    This is a FAQ.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> how i can use the #include<fstream> statement to open a file name as a string variable
    To pass a string to the fstream's constructor or open function, you need to call c_str() on the string variable. This is because the current version of fstream only takes a const char* instead of a string (the next standard will fix this). For example (assuming file_name is a std::string):
    Code:
    std::ifstream in_file(file_name.c_str());
    Help on actually using the stream to input or output data probably requires a book or tutorial full of information.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    8
    Thank you very much for the faq suggestion and the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 11:43 AM
  2. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM