1

error: no matching function for call to 'getline(FILE*&, std::string&)' Function code:

void CarregarArquivo(){
aluno alunos_auxiliar[MAX];
FILE *arquivo;
arquivo=fopen ("texto.txt","r");
int quantidade=0;
fscanf(arquivo,"%d",&quantidade);
if(quantidade!=0){
    quantusuario=quantidade;
    for(int i=0;i<quantidade;i++){
        getline(arquivo,alunos[i].nome);
        fscanf(arquivo,"%d",&alunos[i].matricula);
        printf("%d",alunos[i].matricula);
        fscanf(arquivo,"%d/%d/%d",&alunos[i].nascimento.dia,&alunos[i].nascimento.mes,&alunos[i].nascimento.ano);
        if(alunos[i].numero!=0){
            for(int j=0;j<alunos[i].numero;j++){
                getline(arquivo,alunos[i].materias[j].nome);
                fscanf(arquivo,"%.1f",&alunos[i].materias[j].nota);
            }
        }
    }
}
else if(quantidade == 0 && arquivo == NULL){
    quantusuario =0;
}
fclose(arquivo);

}

includes:

registro.h has structs in there:

struct aluno{
string nome;
int numero;
int matricula;
data nascimento;
disciplina materias[10];

};

includes:

#include"registros.h"
#define MAX 100
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include"funcoes.h"
#include <fstream>
#include<string>

Getline please work.

I'm Brazilian.Ignore the name of variables. Sorry about my English.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
JsHitachi
  • 69
  • 3
  • 8
  • 2
    If there's a choice to not use FILE, then I would choose not to use FILE and use C++ types. However, I'll say in passing that it is possible to wrap FILE* in a std::streambuf (see [here](http://stackoverflow.com/questions/4151504/wrapping-file-with-custom-stdostream)). – Robert Prévost Sep 21 '16 at 02:32

3 Answers3

2

You are mixing up the C library's FILE * functions, and C++ library functions that use std::istream.

You need to rewrite your code and replace all usage of FILE *, including fopen(), et al, with std::ifstream.

The first parameter to std::getline is a std::istream &, and not a FILE *.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
1

I think you are confusing the C and C++ API's. In C++, this is the function:

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

std::getline(std::istream&, std::string&)

whereas in C, the function is indeed

http://man7.org/linux/man-pages/man3/getline.3.html

ssize_t getline(char **lineptr, size_t *n, FILE *stream);

Pick one and stick to it! If you choose C++, then use std::fstream instead of the FILE* and so on. Remember to prepend std:: or use using namespace std; to avoid errors.

So it would start like this:

std::ifstream arquivo("texto.txt");
int quantidade=0;
arquivo >> quantidade;
if(quantidade!=0){
    quantusuario=quantidade;
    for(int i=0;i<quantidade;i++){
        std::getline(arquivo,alunos[i].nome);
        arquivo >> alunos[i].matricula;

And so on. Note how you already are using std::string, so your choice is already made.

iksemyonov
  • 4,106
  • 1
  • 22
  • 42
0

thx guys it compiles now. I put like this:

void CarregarArquivo(){
//FILE *arquivo;
//arquivo=fopen ("texto.txt","r");
int quantidade=0;
ifstream arquivo("texto.txt");
char barra=' ';
arquivo >> quantidade;
if(quantidade!=0){
    quantusuario=quantidade;

    for(int i=0;i<quantidade;i++){
        getline(arquivo,alunos[i].nome);
        cout << alunos[i].nome;
        arquivo >> alunos[i].matricula;
        arquivo >> alunos[i].nascimento.dia;
        arquivo >> barra;
        arquivo >> alunos[i].nascimento.mes;
        arquivo >> barra;
        arquivo >> alunos[i].nascimento.ano;
        if(alunos[i].numero!=0){
            for(int j=0;j<alunos[i].numero;j++){
                getline(arquivo,alunos[i].materias[j].nome);
                arquivo >> alunos[i].materias[j].nota;
            }
        }
    }
}
else if(quantidade == 0 && arquivo == NULL){
    quantusuario =0;
}
arquivo.close();
}

"barra" is because my txt have an date with this format: xx/xx/xxxx.now all i need is Getline working fine. :(.It is not working yet. My txt is like that:

2
Thiago
12312
21/12/1995
Math
10.0
Scott
31233
12/12/1995

First is the number of peoples a need to read. Next is registration and date of birth.The cout bellow getline is just for test and it printf nothing.iksemyonov i'm using using namespace std. Edit: The troubles are from formation of .txt,thx guys

JsHitachi
  • 69
  • 3
  • 8
  • 1
    This is an example of why it is a bad idea to use `using namepace std`. There are a *lot* of names in `std`. Instead, you can avoid common names by doing, for example, `using std::cout` so that you can write just `cout` instead of `std::cout` all the time. But `using namespace std` is a *very* bad practice. – kfsone Sep 21 '16 at 22:14
  • 1
    Also, you don't *need* the `arquivo.close()` at the end of the function - `ifstream` self-closes when it goes out of scope :) – kfsone Sep 21 '16 at 22:15