JsHitachi
JsHitachi

Reputation: 79

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

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.

Upvotes: 1

Views: 2840

Answers (3)

JsHitachi
JsHitachi

Reputation: 79

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

Upvotes: 0

iksemyonov
iksemyonov

Reputation: 4196

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.

Upvotes: 1

Sam Varshavchik
Sam Varshavchik

Reputation: 118292

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 *.

Upvotes: 2

Related Questions