Thread: Saving and opening txt file...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    11

    Saving and opening txt file...

    Hello,
    I would be very thankful, if somebody would explain me (i would be very thankful for code examples) how to create and load txt files by user input. Meaning that I could type in the name and program would save it as filename.txt, and same thing with loading, that user could type in the name and program would open typed_in_filename.txt
    I hope I made my point clear enough...
    Best regards,
    Kitu

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Google for 'ifstream' and 'ofstream'.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>Google for 'ifstream' and 'ofstream'.
    With the extraordinary numbers of bad examples I would hesitate to suggest this. Perhaps a direct link to a well known high quality example would be better. Or, because this is a relatively simple operation an example can be typed up in the post.
    Code:
    #include <cstdlib>
    #include <fstream>
    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string filename;
    
      std::cout << "Enter a file name to open: ";
      if (std::getline(std::cin, filename)) {
        std::ifstream in(filename.c_str());
    
        if (!in.is_open()) {
          std::cerr << "Error opening file for reading" << std::endl;
          return EXIT_FAILURE;
        }
    
        // in can be used like cin now
      }
    
      return EXIT_SUCCESS;
    }
    Using that example a simple reference is all that is needed to understand what is happening and modify the code to open a file for writing.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Princeton's code uses the current standard header files and prefered namespace syntax to minimize exposure of members of namespace std. If you have an older compiler, this syntax won't work--and you might consider updating your compiler. To write to a file you can do something like this:

    string filename;
    std::getline(std::cin, filename);

    //use the C style string in filename to associate file with ofstream
    std:fstream fout(filename.c_str());

    //check if association didn't work and file failed to open
    if(!fout.is_open())
    {
    //send message to error log
    std::cerr << "Error opening file for reading" << std::endl;

    //exit function due to failure
    return EXIT_FAILURE;
    }

    //else you are ready to write material to filename using fout. Material may be text or binary.

    You may also use an fstream with a variety of flags to indicate intention to read or write to file if you don't want to use an ifstream to read file and an ofstream to write to file.

    Just about every step in the above code has alternate syntax that could be used and still have valid code. However the priniciples of declaring a stream to use, associating a file with a stream, checking to be sure the association worked and the file is open for use, and then using the same syntax with the file stream as you would with cin/cout still holds.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    I'll comment that code out for you better:
    Code:
    #include<iostream.h>
    #include<string>
    #include<fstream.h>
    int main()
    {
    std::string filename; //uses <string>
    getline(cin, filename); //input "hi.txt" as an example
    
    //use the C style string in filename to associate file with ofstream
    fstream fout(filename.c_str()); open the file for writing.
    
    //check if association didn't work and file failed to open
    if(!fout.is_open())
    {
    //send message to error log 
    cerr << "Error opening file for reading" << std::endl;
    
    //exit function due to failure
    return EXIT_FAILURE;
    }
    else
    {
    fout<<"It worked"<<endl;
    return EXIT_SUCCESS;
    }
    }
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    11
    Thanks!
    But one more question: Is it possable, that user types there file, and program would open/create file named file.txt?

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>Is it possable, that user types there file, and program would open/create file named file.txt?
    Yes, however you would be required to append the appropriate extension before opening the stream.
    Code:
    std::string buffer;
    if (std::getline(std::cin, buffer)) {
      buffer.append(".txt");
    
      // Open the file using buffer.c_str()
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. open and edit file at command prompt
    By nmullane in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2005, 11:56 PM
  2. opening webpages
    By spiderman in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2005, 08:01 AM
  3. Replies: 2
    Last Post: 06-16-2005, 10:03 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. getting the file name
    By mr ed in forum C++ Programming
    Replies: 0
    Last Post: 09-18-2001, 05:55 AM