opening files

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();
}
 
T

Tim Love

Tim Baker said:
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?
In
ifstream inputFile(filename, ios::nocreate);
your filename is a string instead of the required "const char *" so
try
ifstream inputFile(filename.c_str(), ios::nocreate);
instead.

Also consider replacing
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <string>
by something newer like

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
 
V

void

Uzytkownik Tim Baker napisal, On 2004-04-29 15:41:
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);
RTFM!!!
http://www.cppreference.com/
http://www.cplusplus.com/ref/#libs

filename.c_str()
Best
Darek
 
J

John Harrison

Tim Baker said:
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>

You aren't using the above header file.
#include <iostream.h>
#include <fstream.h>

Both these includes are incorrect, use this

#include <iostream>
#include said:
#include <string>

Add this

using namespace std;

(see below for explanation)
int main(int nNumArgs, char* pzArgs[])
{
string filename;
cout << "What file do you want to open?\n";
cin >> filename;

ifstream inputFile(filename, ios::nocreate);

ifstream takes a const char*, ios::nocreate is unnecessary and non-standard.

ifstream inputFile(filename.c_str());
if (inputFile.bad())

if (!inputFile.is_open())
{
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();

Its no necessary to close the file, the destructor will take care of that
for you.

Either you are learning from an out of date book, or you are using an out of
date compiler (or both). Its good to get into the habit of using modern
standard C++ if your compiler will accept it. Don't use nocreate, remember
no .h on C++ includes, and standard library names live in the std namespace,
hence 'using namespace std;'

john
 
T

Tim Baker

Either you are learning from an out of date book, or you are using an out
of
date compiler (or both). Its good to get into the habit of using modern
standard C++ if your compiler will accept it. Don't use nocreate, remember
no .h on C++ includes, and standard library names live in the std namespace,
hence 'using namespace std;'

john

Thanks for the helpful replies guys, I guess it's time for me to buy a new
book!

Tim
 
T

Tim Baker

Sumit Rajan said:
Just curious: Which one are you currently using?

c++ for dummies :-D
I would have bought a proper book, but this was £2.50 second hand!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

TF-IDF 1
Crossword 2
Homework help - File encrpytion/decryption problem... 5
Crossword 14
Lexical Analysis on C++ 1
Problem while file handling 5
Codeforces problem 0
c++ conversion files 22

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,266
Latest member
DavidaAlla

Latest Threads

Top