Galera, eu fiz um programa que abre um arquivo, lê e separa por linhas, botando em um vector. Então ele imprime na tela. Porém, ele insiste em dar o output "(null)
(null)"
Olhem o código:
#include <iostream.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <vector>
#include <stdio.h>
int main()
{
ifstream arq;
vector<char *> linhas;
char *texto;
char *aux=NULL;
int x=0;
long nlinhas=1;
char *delim = "\n";
char *fnome;
cout << "Digite o nome do arquivo." << endl;
gets(fnome);
arq.open(fnome);
while (arq.get(texto[x])){
x++;
}
arq.close();
aux = strtok(texto, delim);
while (aux!=NULL){
linhas.push_back();
aux = strtok(NULL, delim);
}
for (int x=0; x<=strlen(texto); x++){
if (texto[x]=='\n'){
nlinhas++;
}
}
for (int x=0; x<=nlinhas; x++){
cout << linhas[x] << '\n' << endl;
}
system("PAUSE");
return 0;
}
Onde está o erro? Será que eu estou errando ao ler o arquivo?
Pergunta
rodfraga
Galera, eu fiz um programa que abre um arquivo, lê e separa por linhas, botando em um vector. Então ele imprime na tela. Porém, ele insiste em dar o output "(null)
(null)"
Olhem o código:
#include <iostream.h> #include <stdlib.h> #include <fstream> #include <string> #include <vector> #include <stdio.h> int main() { ifstream arq; vector<char *> linhas; char *texto; char *aux=NULL; int x=0; long nlinhas=1; char *delim = "\n"; char *fnome; cout << "Digite o nome do arquivo." << endl; gets(fnome); arq.open(fnome); while (arq.get(texto[x])){ x++; } arq.close(); aux = strtok(texto, delim); while (aux!=NULL){ linhas.push_back(); aux = strtok(NULL, delim); } for (int x=0; x<=strlen(texto); x++){ if (texto[x]=='\n'){ nlinhas++; } } for (int x=0; x<=nlinhas; x++){ cout << linhas[x] << '\n' << endl; } system("PAUSE"); return 0; }Onde está o erro? Será que eu estou errando ao ler o arquivo?
Obrigado
Rodrigo
Link para o comentário
Compartilhar em outros sites
17 respostass a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.