Thread: Storing and accessing data from and to .txt files.

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    62

    Storing and accessing data from and to .txt files.

    How would you store string data inside a .txt file, so you can request data from the .txt file that is already there, or store additional data?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That sounds very... generic. Have you learnt about what's in the <fstream> standard header?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Write the strings to the file, one per line would likely be best. When reading from the file, just store everything in a container of some sort, std::vector<std::string> for example.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by hk_mp5kpdw View Post
    Write the strings to the file, one per line would likely be best. When reading from the file, just store everything in a container of some sort, std::vector<std::string> for example.
    Yes, the data in the application is stored in a vector. But how do you "extract" the data of a vector to a text file; AND still be able to make the app recognize it, and edit it later on?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But how do you "extract" the data of a vector to a text file; AND still be able to make the app recognize it, and edit it later on?
    You come up with a file format. For example, as mentioned by hk_mp5kpdw, each element of the vector could be on a single line. Then since your vector holds structs as elements, the members of the struct could be comma separated, or separated by pipes, or perhaps you have a fixed width to store them, etc.

    Actually, I would suggest using SQLite and coming up with a database schema instead of a custom file format, but that may be a little advanced at this point, unless you already have some knowledge of relational databases.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by laserlight View Post
    You come up with a file format. For example, as mentioned by hk_mp5kpdw, each element of the vector could be on a single line. Then since your vector holds structs as elements, the members of the struct could be comma separated, or separated by pipes, or perhaps you have a fixed width to store them, etc.

    Actually, I would suggest using SQLite and coming up with a database schema instead of a custom file format, but that may be a little advanced at this point, unless you already have some knowledge of relational databases.
    Using the simplest .txt storage methods is a good start point, no? In case you want to help out, I'd love to understand it.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Using the simplest .txt storage methods is a good start point, no?
    If you want to learn about file handling, then yes. What do you already know?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    62
    Quote Originally Posted by laserlight View Post
    If you want to learn about file handling, then yes. What do you already know?
    I know that a header named "<fstream>" exists. And as I said, it dosent have to be very complex at the beginning stage of the learning process. Using the most easy way will do it for a starter.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Check out the tutorial on file I/O, and also check out cppreference.com's entries on C++ I/O.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    62
    Thanks

Popular pages Recent additions subscribe to a feed