Converting set of string of text file into char array

Hi.
I try to convert set of string inside a text file into array list. But I encounter some error. I use the method of reading a text file line by line. After that, I try to convert the set of string into char array list.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main () {
	char a[256];
	char buffer[256];
	ifstream myfile ("names.txt");

	while (! myfile.eof() )
	{
    myfile.getline(buffer,100);
    
    cout <<buffer << endl;
    
    //convert file inside text file into char array
    strcpy(a, buffer.c_str());
    cout << a << endl;
    }
	
	system("PAUSE");
}


Inside my text file is :
123
dfgdg

I find this error :
Error 1 error C2228: left of '.c_str' must have class/struct/union


Thanks for the help....
Last edited on
myfile.getline(buffer,100);

http://www.cplusplus.com/reference/string/getline/

You didn't use getline() properly. The first argument should be an istream, the second the variable you want to put the line in.
Wrong getline toexii, it's correct.
Just remove the .c_str() from line 19 and you're good to go (c_str() is a function from the string class to convert it into a char array)
My bad indeed.
This is the one you're using:
http://www.cplusplus.com/reference/iostream/istream/getline/

I didn't know there was more than one getline..

Sorry :)
@Warnis:
I need to convert the string class into char array. If I delete the c_str(), then the buffer will still be a string class when I pass it into
strcpy(a, buffer);
Topic archived. No new replies allowed.