Need Help With getline and C-Strings

Hello, I'm trying to write a program that reads lines from a txt file and stores one line into a C-String (array of char) not a C++ String Object, using the ifstream getline() method, then I want to print the contents that are in the C-String. When I try to compile this code as is I get:

"error: no matching function for call to 'getline(std::ifstream&, char[100])'"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
using namespace std;

int main(int argc, char * argv[])
{
	int i;
	char inputfile[100];
	char outputfile[100];
	char line[100];
	ifstream in;
	ofstream out;
	strcpy(inputfile, argv[1]);
	strcpy(outputfile, argv[2]);
	in.open(inputfile);
	if(!in)
	{
		cout << "Error opening the input file. Exiting." << flush;
		return 0;
	}
	out.open(outputfile);
	if(!out)
	{
		cout << "Error opening the output file. Exiting." << flush;
		return 0;
	}
	while(!in.eof())
	{
		getline(in,line);
		cout << line << endl;
	}
	in.close();
	out.close();
	return 0;
}

I know how to use getline with C++ String objects but for this program I want to use C-Strings, any help or clarification would be appreciated. Thank you.
Last edited on
Why do you need C-Strings? If you have API's that require them, there is always std::string::c_str() (member function) that can be used to convert it...

Apart from that, std::getline() only takes a std::string as input. You probably want to be using something like in.getline(line, 100, '\n') instead. Also, looping on EOF is a bad idea, loop on the actual getline command instead, because EOF is only received when the program has actually tried going past the end of the file.
That is what I was looking for, I want to write this without the use of C++ string objects because I'm going to be tokenizing the C-Strings. I'm not familiar with using getline so I did not know it was possible to replace cin with in when using: cin.getline(char * s, size, delims). But it makes sense with cin being a istream&. Thank you for your time and help!
Last edited on
Topic archived. No new replies allowed.