File input in C++ - S1mple question...

This is a discussion on File input in C++ - S1mple question... within the C++ Programming forums, part of the General Programming Boards category; Hey sorry for making so many threads but I have SO many questions lol. Anyway heres the scoop. When you ...

  1. #1
    Registered User darknite135's Avatar
    Join Date
    Dec 2007
    Posts
    16

    Thumbs up File input in C++ - S1mple question...

    Hey sorry for making so many threads but I have SO many questions lol. Anyway heres the scoop. When you read from a file, is there any way that you can skip to the next line?

    Because your file might be like this:

    Hello
    Whats up

    I've only learned to get EVERYTHING from a file. What if I want to get only a few things?

    Heres an example:

    Pretend I have a file that stores all my data (not very clever)
    It has everything in this order:
    -----
    My first and last name
    Address
    Phone number
    My E-mail
    My best friends phone number
    My Library card number
    My library pin
    My favorite book
    My favorite movie
    ------

    So thats the file. How do I write a program that extracts...say...My E-mail, and my favorite movie. Thanks for the help xD

  2. #2
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    I've only learned to get EVERYTHING from a file.
    Really? For a while, the only way I knew of was to read in chunks.
    Anyway, a quick search in Google for c++ io, second result: a tutorial on Input/Output with files. It would be good if you read it...
    Using ifstreams and ofstreams is a lot like cin and cout. If your data just has a single word on each line, you could just do something like:
    Code:
    in/*your ifstream*/ >> myStr/*your string*/;
    until you got to the value you wanted, store that value, and then keep going until you find the next value you want. If there are more than one words per line use
    Code:
    getline(in, myStr) // This is all assuming you're using std::strings, which you should
    and do the same thing, keeping only the values you want. So basically all you're doing is inputting each line, if it's what you want, keep it. Otherwise, just throw it out (figuratively speaking).

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You could use getline() to read in a line, but then it might not read in all of the line if the the buffer is less than the distance to the newline.

    So use this:
    Code:
    char c;
    while (cin >> c && c != '\n');
    edit: too slow.
    Last edited by robwhit; 12-22-2007 at 07:38 PM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    If the std::string buffer is too small for getline when reading a line (which is kinda stupid, since it is a dynamic array), it will set the fail bit in the stream.
    So you can check with fail() and it failed, reset the flags suing clear(ios_base::fail) (I believe) just read again with getline and continue doing so until you get an entire row.
    If it succeeds, fail() will return false (indicating the fail bit is not set).
    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.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Elysia View Post
    If the std::string buffer is too small for getline when reading a line (which is kinda stupid, since it is a dynamic array)
    Yes, it is kind of stupid. I was writing that for C at first, but then I saw that it was the C++ board and converted the post, but left that part because it was possible that the line could be longer than the maximum expansion of the dynamic array, even if that might never actually happen in this program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Beginner Text File Input Question
    By Ruggles in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2006, 01:18 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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