Problem with getline function

Hi guys,
I have searched for ages about an error in my c++ code. I am having loads of trouble with using the 'getline()' function with ifstream. There are many forum posts on this topic, none of which have helped me solve this problem.
Anyway, Here's my code. Oh and BTW this is a very short console program that reads a line from a file and writes it to another file (.txt files only).

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 <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
    char filename[256];
    char line[256];
    ifstream filein;
    ofstream fileout;

    cin.getline(filename, 256);
    filein.open(filename);

    if (!filein.is_open())
    {
        exit(EXIT_FAILURE);
    }

    filein.clear();
    filein.getline(); /* this is my problem area!! */
    filein >> line;
    filein.close();

    fileout.open ("james213.txt", ios::trunc);
    cout << "Target file opened..." << endl;
    fileout << line << "   ";
    cout << "Data written to target file..." << endl;
    fileout.close();
    cout << "Target file closed" << endl;


    cin.get();
}


Here are my errors:
 
24|error: no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::getline()'. 


So far, nothing i have tried has worked at making this compile.

Thanks in advance to anyone that helps me!!

This -> filein.getline(); should be like this -> filein.getline(line,256);

Also, you should get rid of this -> filein >> line;

hey, thanks for your help.
originally i had filein.getline(line, 256) , funny how a space can make a huge difference.
hi, also what would i need to do if i wanted to write more than 1 line into the target file.
Thanks.
funny how a space can make a huge difference.

It can't.

hi, also what would i need to do if i wanted to write more than 1 line into the target file.


1
2
fileout << line << endl;
fileout << line2 << endl;

?!
Last edited on
jammas615 wrote:
funny how a space can make a huge difference.

The space doesn't cause a problem there. Maybe you forgot the semicolon at the end.

jammas615 wrote:
what would i need to do if i wanted to write more than 1 line into the target file.

-> fileout << endl;

Take a look here, it'll help:

http://cplusplus.com/doc/tutorial/files/
http://www.learncpp.com/cpp-tutorial/136-basic-file-io/
thanks for your help and replies.
Topic archived. No new replies allowed.