T
Tim Baker
Hi,
I'm trying to master the basics of file operations, but am coming up with a
problem.
The code below should request a file to open, then report whether it was
successful in opening or not. When I try to compile it it seems that the
ifstream fuction is being passed a pointer to the string rather than the
string itself. I tried putting &s and *s ion various places, but to no
avail. Can someone tell me what I'm doing wrong?
thanks,
Tim
******* Testfile.cpp*******
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <string>
int main(int nNumArgs, char* pzArgs[])
{
string filename;
cout << "What file do you want to open?\n";
cin >> filename;
ifstream inputFile(filename, ios::nocreate);
if (inputFile.bad())
{
cout << "Couldn't find that file\n"
<< "Please check the filename and try again\n";
}
else
{
cout << "hooray, you opened " << filename << " like a pro\n";
}
inputFile.close();
}
I'm trying to master the basics of file operations, but am coming up with a
problem.
The code below should request a file to open, then report whether it was
successful in opening or not. When I try to compile it it seems that the
ifstream fuction is being passed a pointer to the string rather than the
string itself. I tried putting &s and *s ion various places, but to no
avail. Can someone tell me what I'm doing wrong?
thanks,
Tim
******* Testfile.cpp*******
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <string>
int main(int nNumArgs, char* pzArgs[])
{
string filename;
cout << "What file do you want to open?\n";
cin >> filename;
ifstream inputFile(filename, ios::nocreate);
if (inputFile.bad())
{
cout << "Couldn't find that file\n"
<< "Please check the filename and try again\n";
}
else
{
cout << "hooray, you opened " << filename << " like a pro\n";
}
inputFile.close();
}