Advice on some c++ problems

willmac

Registered
Hi, I have a couple of c++ questions that I can't find the answer to. First of, if I use the code below to open a user defined text file, how do I prevent the user from overwriting an existing file?

char filename[50];
cout <<"Enter a file name for output: ";
cin >> filename;
cout << endl;


std::eek:fstream file;
file.open(filename);

Secondly (and this is really bugging me), how do I set up a variable that will allow a user to enter text including whitespaces? When I tried this with char I got a error.

Thanks for the help

Will
 
answer 1:

file.open(filename, ios::noreplace);

answer 2: use the getline function instead

cin.getline(filename, sizeof(filename));
 
sorry, I have to correct regarding answer 1:

file.open(filename, ios::eek:ut | ios::noreplace);
 
Thanks for you're suggestions rhg, but when I tried to impliment them I got some errors. Any more help you could give would be great.

On suggestion 1,

code
char filename[50];
cout <<"Enter a file name for output: ";
cin >> filename;
cout << endl;

std::eek:fstream file;
file.open(filename, ios::eek:ut | ios::noreplace);

error
main.c: In function `int main()':
main.c:24: `noreplace' is not a member of type `std::basic_ios<char,
std::char_traits<char> >'

Any ideas?

On suggestion 2, I also got some errors as follows,

code
char Target[50];
cout << "Target: ";
cin.getline >> Target;
cout << endl;

error
main.c: In function `int main()':
main.c:33: invalid use of member (did you forget the `&' ?)

I am not sure if I understood suggestion 2 and have probably impimented it incorrectly.

Thanks

will
 
cin.getline >> Target;

wrong syntax! Try instead:

cin.getline(target, sizeof(Target));

The prototype is:
istream& getline(char* ptr, int len, char delim = '\n');

About the noreplace issue I'll come back later, ok?
 
OK, I found out that there are in fact some incompatibilities regarding the gcc 3.1 which comes with the Mac OSX Developer Tools. There are some of the ios flags missing, don't ask me why, I didn't find any hint that the noreplace flag has been obsoleted. I guess these are still unresolved quirks with gcc 3.1.

My test program compiled well and works ok when switching back to gcc2. This is done by executing the following command:

sudo gcc_select 2

(to switch back to gcc 3.1, use sudo gcc_select 3)

The following docs could be interesting for you:
http://www.sunsite.ualberta.ca/Documentation/Gnu/gcc-2.95.2/html_mono/iostream.html
http://developer.apple.com/technotes/tn2002/tn2071.html
 
Hi rhg, have tried you're suggestions and I managed to get the problems shown above solved. However, when I compiled the app with gcc 2 instead of 3 the try/catch part of my app (that I included to catch non-numerical input) failed. This must be something to do with gcc 2.

Thanks for you're help

will
 
Hint: Use the -fexceptions compiler option to enable try/catch with gcc 2. I assume this is the default for gcc 3 but not for gcc 2.
 
Fantastic. Thanks rhg. By the way, where can I find more information like this? Could you recommend a good book or website to help me learn more about c++?

will
 
Back
Top