-
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!
-
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.
-
Thanks...that did the trick!!
-
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.