Question concerning IO file
-
Hi all, I want to create and read/write a file. If I use fstream class, I can open only the file already exits. If I want to create a new file. What can I do? Thank you very much. Tran Phuong Nga
tpndtbk wrote: If I use fstream class, I can open only the file already exits. If I want to create a new file. What can I do? use
ios::in
in Second Parameter of fstream object initialization ----------------------------- "I Think this Will Help" ----------------------------- Alok Gupta visit me at http://www.thisisalok.tk -
tpndtbk wrote: If I use fstream class, I can open only the file already exits. If I want to create a new file. What can I do? use
ios::in
in Second Parameter of fstream object initialization ----------------------------- "I Think this Will Help" ----------------------------- Alok Gupta visit me at http://www.thisisalok.tkHI, I did like following: fstream *fout; fout = new fstream("data.dat", ios::in | ios::out | ios::binary); fout->write((const char*)name, sizeof(Names)); //Names is a struct and name is a variable of Names type fout->close(); ----------- So what is wrong in these codes? I run this code, no errors but the file is not created! Thank you
-
HI, I did like following: fstream *fout; fout = new fstream("data.dat", ios::in | ios::out | ios::binary); fout->write((const char*)name, sizeof(Names)); //Names is a struct and name is a variable of Names type fout->close(); ----------- So what is wrong in these codes? I run this code, no errors but the file is not created! Thank you
I tried your code (except I did not use a pointer) and it worked as expected. Perhaps it is a permission-related issue.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
HI, I did like following: fstream *fout; fout = new fstream("data.dat", ios::in | ios::out | ios::binary); fout->write((const char*)name, sizeof(Names)); //Names is a struct and name is a variable of Names type fout->close(); ----------- So what is wrong in these codes? I run this code, no errors but the file is not created! Thank you
As with David's comment above, but also be aware of the location that your file is being created. tpndtbk wrote: fout = new fstream("data.dat", ios::in | ios::out | ios::binary); You supplied only the filename, not a definite path, which means that the file will be created in the current directory. This, in most cases, may very well be in your ..\debug\ subfolder.
I Dream of Absolute Zero
-
As with David's comment above, but also be aware of the location that your file is being created. tpndtbk wrote: fout = new fstream("data.dat", ios::in | ios::out | ios::binary); You supplied only the filename, not a definite path, which means that the file will be created in the current directory. This, in most cases, may very well be in your ..\debug\ subfolder.
I Dream of Absolute Zero
-
As with David's comment above, but also be aware of the location that your file is being created. tpndtbk wrote: fout = new fstream("data.dat", ios::in | ios::out | ios::binary); You supplied only the filename, not a definite path, which means that the file will be created in the current directory. This, in most cases, may very well be in your ..\debug\ subfolder.
I Dream of Absolute Zero
And there's a strange thing that: If I write: fstream *fout; fout = new fstream("data.dat", ios::in | ios::out | ios::binary); if (!fout->is_open()) { cout << " can't open the file" << endl; return 1; } fout->write((const char*)name, sizeof(Names)); ---> the result is "can't open the file"; But if I don't check if the file is open. fstream *fout; fout = new fstream("data.dat", ios::in | ios::out | ios::binary); fout->write((const char*)name, sizeof(Names)); --> there's no error but there's no file created. I think if the file doesn't exist, the instruction "fout->write((const char*)name, sizeof(Names));" should cause an error. Right?