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

Posted on 2009-04-18
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
    LVL 3

    Expert Comment

    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...
    LVL 4

    Accepted Solution

    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

    Thanks a lot!
    LVL 4

    Expert Comment

    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...

    LVL 1

    Expert Comment

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now