C++ Files

This is a discussion on C++ Files within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I am new to C++ and I have created a simple program that opens a text file and ...

  1. #1
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    C++ Files

    Hi guys, I am new to C++ and I have created a simple program that opens a text file and then displays its contents. For this program I have also developed a menu but I was trying to create an option so that I could enter the name of a text file and then it would open it as a new file and display the contents of it. Although I can't figure out how to do this, any advice would be great.

    Thanks and happy new year!

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Write the code for what you know so far - and then we can help you fill in the blanks.

    If you know how to work with strings, you can input a string containing the name of a file, and then supply this string as the name of the to create, if that helps.

  3. #3
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Hi, thanks for your reply. I have not used strings yet although I have used char arrays. Is the string method you mentioned the best way to do this?

    Thanks.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,321
    Either one would work. You should generally prefer to use the C++ string class for C++ programs, so technically that is the better way. Regardless, you should create a variable to hold the user input, then pass that value to the file stream constructor or open function where you are currently putting the file name.

  5. #5
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Do you mean something like the following code?

    Code:
    char a[100];
    
    cin>>a;
    
    ifstream b ("a")

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    char a[255]; // max path in MS-Windows and I think *nix is 255 characters
    
    cin.getline(a,sizeof(a)); // allows input if spaces
    
    ifstream b (a); // no quotes

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,321
    >> Do you mean something like the following code?
    With Ancient Dragon's corrections that code will work just fine, although you aren't using C++ strings, you are using C style character arrays. Since either will work, it is up to you to choose, although like I said earlier C++ strings are generally perferred in C++ programs.
    Code:
    string a;
    
    getline(cin, a);
    
    ifstream b(a.c_str());

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM

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