Reading in data from a file

This is a discussion on Reading in data from a file within the C++ Programming forums, part of the General Programming Boards category; So I need to read in a file, and process the data on each line. The basic format is (character, ...

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    Reading in data from a file

    So I need to read in a file, and process the data on each line. The basic format is (character, space, code).
    Code:
    C .--.
    As of yet, I have been able to read in the file and get this correct information, but when it comes to the line with the space character, it looks like:
    Code:
      ..-.
    My function to read in each line fails on this one, because the >> skips the spaces. I would like to use getline(), but I only need to read in the first character, ignore the second, and read the rest of the line into another variable. The getline and get functions take in character arrays, anyone have ideas on how to fix this problem? I could just use character arrays for the whole thing, but that's wasteful; there has to be another way to accomplish this, anyone know?

    Here's my code to read in the lines:
    Code:
    	while (!file.eof())
    	{
    		char ch;
    		char text[9];
    
    		file >> ch;
    		file.ignore();
    		file >> text;
    	}
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1) This data:
    Code:
    C .--.
    could never be read as this:
    Code:
     ..-.
    2)
    Code:
    file >> ch;
    file.ignore();
    file >> text;
    The >> operator stops reading in data at the first whitespace character(space, tab, newline), and when it starts reading in data, it skips any leading whitespace . Therefore, file.ignore() does what the >> operator was going to do anyway, so it is redundant.

    3)
    My function to read in each line fails on this one, because the >> skips the spaces. I would like to use getline(), but I only need to read in the first character, ignore the second, and read the rest of the line into another variable.
    Then why not use both >> and getline()? That is when ignore() will prove useful. The >> operator reads up to but not including the trailing whitespace. Unlike with the >> operator, getline() doesn't skip leading whitespace, so if you want to skip one character, e.g. a space, you can use ignore(1). If you want to skip two characters, you can use ignore(2).

    Another difference with getline() is that after it stops reading in data when it encounters the specified delimiter('\n' is the default delimiter), it removes the delimiter from the stream while the >> operator leaves the whitespace in the stream.
    Last edited by 7stud; 02-27-2005 at 08:39 PM.

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I don't think you understand what my goal is. I need to read in from a file, which gives a morse-like code system. Every character that is to be encoded is on a new line. The first character on a line is the character to be encoded. The second character is a space, then the remaining characters are a string of dots and hyphens. The problem I run into is on the line for the encoded space character. Since the first and second character of that line is the space character, the >> ignores both. I would like to use file.getline() but it takes in as a parameter a character array, when I need to pass in just one char.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I don't think you understand what my goal is.
    I can only go by what you say:
    I only need to read in the first character, ignore the second, and read the rest of the line into another variable.

    The problem I run into is on the line for the encoded space character. Since the first and second character of that line is the space character, the >> ignores both. I would like to use file.getline() but it takes in as a parameter a character array, when I need to pass in just one char.
    You can use get(char& ch), ignore(), and getline() to do what you want. There is also a getline() for string types:

    #include <string>
    ...
    string str;
    getline(inFile, str);
    Last edited by 7stud; 02-27-2005 at 08:49 PM.

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    1) This data:
    Code:

    Code:
    C .--.

    could never be read as this:
    Code:

    Code:
      ..-.
    I never said it was. That second example is the space character. There are two leading spaces before the dots and dashes. The first space needs to be read in as the character that is to be coded, and the second one needs to be ignored, and the rest as the encoded representation of the space character.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    how about file.get()?
    signature under construction

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by Raven Arkadon
    how about file.get()?
    I want to use this, but the variable I have for storing the first character is of type char. file.get() and file.getline() take in a character array (char*) to store the input into.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    int fstream::get(...) has plenty of overloads.

    one overload accepts no arguments and simply reads one byte.

    note that the return type is int.
    in case the call fails some error constant is returned - dont know what the name of the constant is... basically the value is negative then
    signature under construction

  9. #9
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by Raven Arkadon
    int fstream::get(...) has plenty of overloads.

    one overload accepts no arguments and simply reads one byte.

    note that the return type is int.
    in case the call fails some error constant is returned - dont know what the name of the constant is... basically the value is negative then

    Praises (problem's fixed). I'm soooo tired right now. Too much movie watching.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. reading data from a file
    By brianptodd in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2003, 06:50 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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