How do I use getline to read a file into an array?

Posted on 2009-04-18
Medium Priority
Last Modified: 2012-05-06
I am trying to read a file into a 2D integer grid array. When I run this code, I get two errors - one at each use of getline. The error is: "no matching function for call to 'getline[(((long unsigned int)(((int)rows) - 1)) + 1u)][(((long unsigned int)(((int)cols) - 1)) + 1u)], int, char)' ". Is something wrong with my input to getline or the integer array?
#include <iostream>
#include <fstream> 
using namespace std;
int main () 
	// open the file
	ifstream sampleGrid("/Users/vshen/Documents/EECS 311/Program 1/sample_grid.txt");
	if (sampleGrid)
		cout << "File opened." << endl;
	// exit the program if the file can't be opened
	if (!sampleGrid)
		cerr << "File cannot be opened." << endl;
		exit (1);
	// figure out the number of rows and columns... or just assume they're 10
	int rows = 10;
	int cols = 10;	
	// create the array that will store the grid
	int rowsread = 0;
	int colsread = 0;
	int myArr[rows][cols];
		getline(myArr, 20, '\t');
		if(rowsread == rows)
			rowsread = 0;
			getline(myArr, 20, '\t');
	return 0;

Open in new window

Question by:vwps

Expert Comment

ID: 24177792
With getline(), you try getting line from a file, the signature of the function will be:
getline(char**, size_t*, FILE*)
getdelim(char**, size_t*, int, FILE*)

Make sure your myArr is what you want to put (Do you want to dereference once first? It was a 2D array)
Check also the size you pass into the getline/getdelim will not overflow your char array...

Accepted Solution

ewest02 earned 2000 total points
ID: 24177811
look at the definition of getline()

basic_istream& getline(char_type *str, streamsize count);
basic_istream& getline(char_type *str, streamsize count, char_type delim);

The first argument is a pointer to char_type. You on the other hand are passing a pointer to a 2D array of integer.

You will need to read in  the integer values as strings ( eg "123") or at least a series of char and then convert from char string to integer.


Author Closing Comment

ID: 31571901
Thanks a lot!

Expert Comment

ID: 24177822
NOTE:  The variants of getline() and getdelim() as suggested by doraemon2555 are not C or C++ standard functions. These are GNU C extensions. Use them at your own risk...


Expert Comment

ID: 24690432
You read it to 2d array. Then you need to use getline like this

int i=0;
                getline(myArr[i], 20, '\t');

Open in new window


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question