Please problem with Dev C++ 4.9.92

I following code does compile with problem, but is not running well

#include <fstream>
#include <iostream>

using namespace std;
int main ()
{
char fileName[80];
char buffer[255];
cout << "Please re-enter the file name: ";
cin >> fileName;

ifstream fin(fileName);
if (fin)
{
cout << "Current file contents: \n";
char ch;
while(fin.get(ch))
cout << ch;
cout << "\n***End of file contents.***\n";
}
fin.close();
cout << "\nOpenning " << fileName << " in append mode...\n";
ofstream fout(fileName,ios::app);
if (!fout)
{
cout << "Unable to open " << fileName <<" for appending.\n";
return (1);
}

cout << "\nEnter text for the file: ";
cin.ignore(1,'\n');
cin.getline(buffer,255);
fout << buffer << "\n";

fout.close();

fin.open(fileName);

if (!fin)
{
cout << "Unable to open " << fileName << " for reading.\n";
system("PAUSE");
return (1);

}

cout << "\nHere's the contents of the file:\n";
char ch;
while (fin.get(ch))
cout << ch;
cout << "\n***End of the file contents.***\n";

fin.close();
system("PAUSE");
return 0;
}


The program doesn't open an existing file, I think, is not recognizing the line fin.open(fileName); but in DOS after compiling with bcc32 the program run without problem.
How can I make this program run in Dev C++?
From what I see, you have to convert the string to a C string for it to work:

ifstream fin(filename.c_str());

EDIT: And Dev C++ is pretty damn old and not updated for a long time. Now would be a good time to switch to another IDE like Code::Blocks.
Last edited on
and please in future use code tags ----------> the button on the right that looks like <>
Topic archived. No new replies allowed.