Problem in reading file

Hello,
I have a problem to read file, here is the pseudo code :
Graph is a class

void Graph :: lecture_fichier(char *nom) {


ifstream fich;
fich.open(nom);
if(fich){
cout<<"good";
fich.close();
}
else
cout<<"bad";
}


int main(){
int op,ov;
double lv,lp;
list<int> P1;
list <int> P2;
char *nom;

cout<<"saisir le nom du fichier : "<<endl;
cin>>nom;
Graph G;
G.lecture_fichier(nom);

thank u
1
2
3
char *nom;
...
cin>>nom;


Bad.

nom is a char pointer with a random value. It will be pointing at some random memory somewhere. Could be anywhere. All you did was create a pointer; that's not enough. You also have to create something for it to point at, and make sure the pointer does point at the right thing.

cin>>nom will be attempting to write the input over some random memory. The operating system will not like this and will stop the program.
Last edited on
To solve the problem addresed by Moschops, why not use the std::string class, and then cin, or possibly even getline, since file names can have spaces in them.
(saisir le nom du fichier means enter file name in french...I think; it has been a long time since I have taken french lol.)

Secondly, you don't seem to be doing any actually reading of a file... @w@
Even if you were to fix the char pointer problem, you are still not really reading a file.
This code here:

1
2
3
4
5
6
7
8
9
ifstream fich;
fich.open(nom);
if(fich){
cout<<"good";
fich.close();
}
else
cout<<"bad";
}

Simply checks to see if the file exists, yet it does not actually READ.

If you want to read the file, then I suggest looping through each line, and inputting them into a container, such as another array, or better yet, a vector. I don't really know what you want though, maybe all you need to do is check to see if the file exists or not, lawl.
Last edited on
Topic archived. No new replies allowed.