Thread: Specific data extraction from .txt files using fstream.h

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    16

    Specific data extraction from .txt files using fstream.h

    Hello guys.

    I bought a logbook today for my car, and I wondered why the hell not just create a software app for it? :P

    I want to store the information and I am obviously going to use fstream.h to handle this I/O, but I am just wondering:
    I want the program to be able to update itself on startup with previously entered information (Kinda like a logbook, RIGHT? :P ) and I am not very experienced with the file I/O use.
    How would I extract certain lines of information from the text file, and assign it to a variable?
    Even just an example to extract and display the starting odometer reading would be appriciated.. I mean, there's no point to this if I have to type in every single thing, especially when certain values will remain the same..

    Thanks in advance!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You will be using <fstream>, not <fstream.h>. If you use the later, then it's time to upgrade your compiler.
    As for your question, here are two examples:

    file >> variable;

    This extracts an identifier that can be converted into the type of the variable until the next whitespace character (enter, space, tab).
    It can be used to extract a number from a row that looks like "123 456", for example.

    std::getline(file, variable);

    Reads an entire row from file into variable. Variable must be a std::string.
    You can convert the row into some other type by using stringstreams or boost:

    T myvar = boost::lexical_cast<T>(variable);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    16
    I still don't see how that is going to give me the ability to extract specific data from a file? Isn't there some sort of command to extract a line (like... cin.getline(12)) or something?? Totally lost

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Well, if you were, for example, to get the first line, you'd have line one; do you know what you'd have if you got the next line?

    Soma

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by xScen3xHdstyl3x View Post
    I still don't see how that is going to give me the ability to extract specific data from a file? Isn't there some sort of command to extract a line (like... cin.getline(12)) or something?? Totally lost
    I told you how to read a line. What more do you want to know?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Packet data extraction
    By shinpin in forum C Programming
    Replies: 14
    Last Post: 08-09-2011, 07:29 AM
  2. Getting Specific Data from a file
    By darknite135 in forum C++ Programming
    Replies: 4
    Last Post: 12-31-2007, 11:41 PM
  3. Searching for specific string using fstream
    By comwiz in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2007, 03:37 AM
  4. Data extraction from file
    By peterxor in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2003, 04:44 PM
  5. I need some help reading in data with fstream
    By Geo-Fry in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2003, 08:25 AM