Storing and accessing data from and to .txt files.

This is a discussion on Storing and accessing data from and to .txt files. within the C++ Programming forums, part of the General Programming Boards category; How would you store string data inside a .txt file, so you can request data from the .txt file that ...

  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
    19,408
    That sounds very... generic. Have you learnt about what's in the <fstream> standard header?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,674
    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.
    I used to be an adventurer like you... then I took an arrow to the knee.

  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
    19,408
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    19,408
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    19,408
    Check out the tutorial on file I/O, and also check out cppreference.com's entries on C++ I/O.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

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