2016-09-21 11 views
1

エラー:エラー:' getline(FILE *&、std :: string&) 'の呼び出しで一致する関数がありません

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); 

}

含む:

registro.hは、そこに構造体を有する:

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

};

含まれています

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

getlineの仕事してください。

私はブラジル人です。変数名を無視してください。私の英語は残念です。

+1

FILEを使用しないという選択肢がある場合、FILEを使用せずにC++タイプを使用することを選択します。しかし、私は渡して言うことが可能ですファイル*をstd :: streambuf([ここ](http://stackoverflow.com/questions/4151504/wrapping-file-with-custom-stdostreamを参照してください) )。 –

答えて

1

CライブラリのFILE *関数と、std::istreamを使用するC++ライブラリ関数を混在させています。

などを含むFILE *などのコードをすべて書き換えてstd::ifstreamと置き換える必要があります。

std::getlineの最初のパラメータはstd::istream &であり、FILE *ではありません。

+0

ストリームのすべての用途を 'FILE * 'に置き換えるコードを書き直します –

0

あなたはCとC++ APIを混乱させると思います。 C++では、この関数である:

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

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

Cに、関数が実際に

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

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

1を選択し、それに固執であるのに対し、 ! C++を選択した場合は、FILE*の代わりにstd::fstreamなどを使用します。エラーを避けるには、std::の前に忘れずにusing namespace std;を使用してください。

だから、次のように起動します:ように

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; 

と。すでにstd::stringをどのように使用しているかに注意してください。選択は既に行われています。

0

ここでコンパイルする人。

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(); 
} 

「バラ」私のTXTはこの形式で日付を持っているためである::XX/XX/xxxx.now私が必要とするすべてのgetlineの作業結構です、私はこのように置きます。 :(それはまだ働いていません。 私のTXTは、そのようなものです:

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

最初に読み込む必要の人々の数です。次は、登録と生年月日です。勘定コードは、テスト用です。printf nothing.iksemyonov私はnamespace stdを使っています。 編集: 問題は、.txt、thx人の形成によるものです

+0

これは' using namespace std'を使うのが悪い考え方の例です。 'std'にはたくさんの名前があります。代わりに 'std :: cout'を使うなど、一般的な名前を避けることができるので、' std :: cout'ではなく 'cout'だけ書くことができます。しかし、 'using namespace std'は*非常に悪い習慣です。 – kfsone

+0

また、関数の最後に 'arquivo.close()'が必要* *ではありません。 - ifstreamがスコープ外に出たときにそれを自動的に閉じます。 – kfsone

関連する問題