getline help

This is a discussion on getline help within the C++ Programming forums, part of the General Programming Boards category; Hello All, I am trying to read in a text file with the following format: <int1>,<char1>,<char2>,<char3>,<char4>,<int2> All data is seperated ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    24

    getline help

    Hello All,

    I am trying to read in a text file with the following format:

    <int1>,<char1>,<char2>,<char3>,<char4>,<int2>

    All data is seperated by a comma. Once I have the data, I am trying to output only <char1> & <char2> until end of file.

    So, to get the data, I am using the getline function as follows:

    Code:
    #include <fstream.h>
    #include <iostream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    
    
    int main()
    {
        char Circuit1[30], Circuit2[30], Conductor[30], Comments[50];
        int Line, Number;
        char FileName[20];
    
        cout << "Enter the name of the file you want to open: ";
        cin >> FileName;
        ifstream Table(FileName);
    
    	while(!Table.eof()){
    		getline( Table, Line, ',' );
    		getline( Table, Circuit1, ',' );
    		getline( Table, Circuit2, ',' );
    		getline( Table, Conductor, ',' );
    		getline( Table, Comments, ',' );
    		Table >> Number;
    		Table.ignore( 200, '\n' );
    		
    		cout << Circuit1 << "\t";
    		cout << Circuit2 << "\t" << endl;
    		
    	}
    
        return 0;
    }
    I get the following error, but not sure what I can do to fix it.

    'getline' : undeclared identifier.

    Any ideas?

    Thanks!

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    getline() has two prototypes, one for STL strings and one for C-style strings (null terminated char arrays). You're trying to ue the version for STL strings on a C-style string. Try the other version:

    char buffer[30];
    //read line data into a char buffer
    Table.getline(buffer, 30, ',');
    //convert buffer into an int
    Line = atoi(buffer);
    //read the rest of the line into appropriate variables
    Table.getline(Circuit1, 30, ',');
    Table.getline(Circuit2, 30, ',');
    //etc.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    24
    Thanks...that did the trick!!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    #include <fstream.h>
    #include <iostream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    You really should be using the standard headers:
    Code:
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    
    using namespace std;
    >while(!Table.eof()){
    Bad idea. The eofbit is only set after you've tried and failed to read from a stream. The result is that you usually end up processing the last record from the stream twice. A better way is to use either an infinite loop and test for end-of-file within the loop body, or find some way of using the return value of your input function as a loop condition.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  2. getline() help
    By AndyBomstad in forum C++ Programming
    Replies: 9
    Last Post: 02-17-2005, 02:39 PM
  3. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 05:42 PM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 11:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM

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