![]() |
|
#1
|
|||
|
|||
Using a string as filename to open a fileHi, new to C++ no flamingos to me plz!!!
I decided to see if I could get into trouble over here now. Going through the cplusplus.com tutorial mentioned a few threads back, I was looking into file manipulation and came accross this mystery. What is the difference between the string and a char[] that causes this compiling error? CPP / C++ / C Code:
Any Ideas Guys? I'm waiting patiently for you to help ME plz... Howard(); |
|
#2
|
|||
|
|||
Re: prob using a string as filename to open a fileQuote:
Specifically: CPP / C++ / C Code:
More generally: http://www.yolinux.com/TUTORIALS/Lin...ringClass.html I know it says "Linux Tutorial," but it's all about std::string stuff, so anyone is allowed read it, even non-Linux guys (and even non-guys)! Regards, Dave |
|
#3
|
|||
|
|||
Re: prob using a string as filename to open a fileGone awhile.... sorry... Ok, I understand a little more , still a long way to go:
CPP / C++ / C Code:
Are all these kinds of tools available to all datatypes? Guess I'll read on and see... Thanks Dave; Howard(); |
|
#4
|
|||
|
|||
Re: prob using a string as filename to open a fileQuote:
No, no, no! That's undefined behavior. The data() member function does not (that is, it is not guaranteed to) append a terminating zero byte as is required for the argument for open(). That is: It may or may not appear to work but using this way is a big no-no. (That's five no's so far.) You even gave the reference: http://www.cplusplus.com/reference/s...ring/data.html Read it again: "Notice that no terminating null character is appended (see member c_str for such a functionality)." For the argument to open() (and anywhere else you need a C-style string from a std::string), use c_str(), as I showed in my previous post, to make sure it returns a pointer to a zero-byte terminated sequence of chars. Regards, Dave |
|
#5
|
|||
|
|||
Re: prob using a string as filename to open a fileQuote:
Quote:
Don't know how I got sidetracked but in my search path on this page down at the bottom: http://www.cplusplus.com/reference/string/string/ Quote:
|
|
#6
|
|||
|
|||
Re: prob using a string as filename to open a fileQuote:
RTFP (Read the Fine Print): The library function definition: data() ==> pointer to sequence of chars in the string c_str() ==> pointer to zero-byte terminated sequence of chars in the string The not-so-subtle subtleties are easily lost since many people never actually think about what a "string" is in C (why is "string" in quotes? because there is no "string" data type in C), and important details can slip our notice. Details like: 1. Using the terminating zero byte to cause end of "string" processing and copying the zero byte automatically with strcat(), strcpy(), etc. 2. Using the terminating zero byte to cause end of "string" processing but not actually considering it part of the "string" with strlen(), printf("%s",...), etc. There is no "string" data type in C++ either. There is a string class that is part of the C++ standard library that is a poster boy for the power of that feature of the language that allows definition of new "stuff" that acts like new data types, complete with handy, dandy functions and overloaded operators. Regards, Dave |
|
#7
|
|||
|
|||
Re: Using a string as filename to open a fileQuote:
http://www.cplusplus.com/doc/tutorial/variables.html and while mentioning that string is not a fundemental data type and that it is a class object, with an example like this: CPP / C++ / C Code:
But at the bottom it does say: for more info: http://www.cplusplus.com/reference/string/string/ much to look at there...so the adventure continues... Thanks |
Recent GIDBlog
US Elections and the ?Voter?s Responsibility? by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 08:44 |
| Help wit my source code compiler errors | Krandygrl00 | C++ Forum | 1 | 06-Jun-2005 09:14 |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 11:53 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 12:28 |
| c++/cgi program can't open file in user dir | mikenowo | Apache Web Server Forum | 2 | 04-Mar-2004 20:33 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The