-
file
1.
Code:
#include <iostream>
#include <fstream>
using namespace std;
void f(const char *fname)
{
ifstream fin(fname,ios::in | ios::binary);
if(!fin)
cout << "File not opened";
}
int main()
{
system("notepad ABC");
f("ABC");
system("pause");
}
When the notepad opens the file something is written into the file
and saved.But when f() opens the file the output shown : File not opened.How can i open the file from f()?
(Tried on Dev C++ 4.9.9.2 and Code Blocks 1.02rc)
2. Is _unlink for removing a file from C++ outdated as i need to include io.h for it 2 work?How can i do so using new headers?
-
'ABC" is probably not the name of the file -- try "ABC.TXT" -- and if it was not saved in your program's current working directory then you will have to specify the full path, such as "C:\\MYDIR\\ABC.TXT"
-
avoid using system() calls
it's potentially dangerous - who's to say I don't create a virus that replaces pause.exe with my own pause.exe (the virus). Your program would be happily spreading my virus and you would never know =)
and why are you opening the file as binary when you wrote it as plain text?
and if you use ifstream ios::in is assumed.