getline overloaded error

Dec 5, 2014 at 4:43am
Hi, I am currently having trouble to have getline to read line from the file.
Error is: "no instance of overloaded function "getline" matches the argument list"

code is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
  std::ifstream config("config.txt");
		
	string process[4];
	int linecount = 1;
	if (config.is_open)
	{
		while (config.peek() !=EOF)
		{
			getline(config, process);
			linecount++;
		}
	}
Dec 5, 2014 at 4:53am
You probably meant getline(config, process[linecount]);
Note that your program will not function properly if the file has more than four lines.
Dec 5, 2014 at 5:11am
I actually want to store the contents of each line in a reused array
My file will look something like this:

chrome,1,3,high

so I want to store the name in Process[1] and so on...
so how do I get it to read the line then split the values into the array after each comma
and reiterate with every line?
Dec 5, 2014 at 12:41pm
bad form, but something like ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
string line;
while (getline(config, line))
{
    const char *tok = ",";
    char *pch = const_cast<char *>(line.c_str());
    pch = strtok (pch, tok);
    while (pch != NULL)
    {
       process[linecount++] = pch;
       pch = strtok (NULL, tok);
    }

    // etc.
}
Last edited on Dec 5, 2014 at 12:41pm
Dec 5, 2014 at 1:25pm
bad form
It is undefined behavior. And this code actually does not even work for me.

Never cast away const and then change that variable. You should never ever change const variable.

getline has third parameter which takes delimiter:
1
2
3
4
std::getline(config, process[0], ',');
std::getline(config, process[1], ',');
std::getline(config, process[2], ',');
std::getline(config, process[3]);
Dec 11, 2014 at 9:00am
@MiiNiPaa thanks for calling me on that.

One should not use bad form, even when it's not the main point being illustrated. Especially since it sets a bad example.

The habit of always being correct and not cutting corners is a good one, and helps create better products when one is under pressure of a looming deadline.

Indicating "bad form" up-front is not an excuse, that's just laziness.
Topic archived. No new replies allowed.