0

I started having C++ classes at university last week. At the moment I just know some C language which makes me a little confused when learning C++. I have this exercise that says: "Write and use the fstream library to read a text file. The function should write on the screen each text line read of the file (with spaces)."

I wrote the code below that can write the line but without spaces. I've also heard about getline, but I don't know how to use it, the compiler allways says "no matching function for call to getline".

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

file_read(){
    ifstream origem;
    origem.open("ficheiro.txt");


    if (!origem) {
        cerr << "Error" << endl;
        return -1;
    }
char outp[100];

while(!origem.eof() ){
    origem >> outp;
    cout << outp;
    }

return 0;
}

For example: If i have in the ficcheiro.txt"My dog has a bone", the programm will write it like "Mydoghasabone"

So with the getline I tried this:

...
    ifstream origem;
    origem.open("ficheiro.txt");


    if (!origem) {
        cerr << "Error" << endl;
        return -1;
}
    char outp[100];

    getline(origem, outp);
    origem >> outp;
    cout << outp;

    return 0;
}

The compiler said:[Error] no matching function for call to 'getline(std::ifstream&, std::char [100])'

My problem is just to read the file including the spaces!


I'm also having some trouble learning C++, I started learning 'Classes' and using the CMD and I don't even know what I'm learning. Do you know where can I learn C++ in a more understandable way?

Bikke21
  • 31
  • 7
  • And what happened when you tried `std::getline`? – Sam Varshavchik Mar 15 '20 at 02:12
  • the compiler said "no matching function for call to getline", and i couldn´t run the programm – Bikke21 Mar 15 '20 at 02:17
  • `std::getline` is the correct way to go. If you cannot get that to compile, then unless you [edit] the quesiton and show a [mre] that anyone can cut/paste ***verbatim as shown*** and reproduce your compilation error, it is unlikely that anyone can help you figure out the reason. There could be many. – Sam Varshavchik Mar 15 '20 at 02:21
  • 1
    should i write `std::`, im already using `"using namespace std;"` ... I'll do that minimal reproducibal example thing... Thanks for trying to help me, btw. – Bikke21 Mar 15 '20 at 02:34
  • 1
    You will do yourself a favor if [you forget that `using namespace std;` exists](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) at least you fully understand what that really does. – Sam Varshavchik Mar 15 '20 at 02:36
  • so I should write always the `std::`? ... I edited the question, see if its more understandable – Bikke21 Mar 15 '20 at 02:51
  • [Why !.eof() inside a loop condition is always wrong.](https://stackoverflow.com/q/5605125/9254539) – David C. Rankin Mar 15 '20 at 05:41

1 Answers1

1

Try this

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int file_read()
{
    ifstream origem;
    origem.open("ficheiro.txt");


    if (!origem) {
        cerr << "Error" << endl;
        return -1; 
    }
    char outp[100];

   while( origem.getline(outp,100) )
        cout << outp;

return 0;
}

int main()
{
    file_read();
}

The compiler said:[Error] no matching function for call to 'getline(std::ifstream&, std::char [100])'

if you want to use getline() change outp from char to string like

string outp;

while( getline(origem,outp))
    cout << outp;

Because getline() is for string.

srilakshmikanthanp
  • 2,231
  • 1
  • 8
  • 25