decalring a var

Started by
6 comments, last by Zahlman 18 years, 8 months ago
i was just wondering what type of var would a "\" be considered? char slash("\") string folder("pictures"); string fileprefix("Movie"); char filename[1000]; sprintf(filename,"%s%d.ppm",slash,folder.c_str(),slash,fileprefix.c_str(),frame); this is incorrect what is right the output should be /pictures/movieX.ppm
Advertisement
I think u have to have a letter after '\', eg '\n'.

If u want a slash in a string u have to use '\\' to get 1 slash.
Reject the basic asumption of civialisation especially the importance of material possessions
if the output should be /pictures than why not use a / instead of a \?

if your asking why the backslash doesn't show up, its because its an escape character.

char slash = '\\';
Char's are enclosed in single quotes ('), not double ("). Also, the character '\' needs to be escaped, which makes it '\\'.
This is C or C++ right?

"\" <- this should cause the compiler to have a fit.

the \ character is a escape character to add in command sequences that can't be typed in by ascii keyboard. for example '\n' is LF and '\r' is CR. Because of this when you actually want to use the character \ in a string or as a single char value, you must enter it in as '\\' or "\\" (first one being a single char, the second being a const char array of 2 bytes)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/lexic_10.asp

char filename[]="\\pictures\\movieX.ppm";


is all you need.

[Edited by - MustEatYemen on July 23, 2005 2:54:16 PM]
-Scott
an easier method would be:

std::string slash("\\");std::string folder("pictures");std::string fileprefix("Movie");std::string filename = slash +folder + slash + fileprefix//or evenstd:stringstream fnamefname << "\\" << folder << "\\" << fileprefix;filename = fname.str()
For the record string literals are considered to be constant character arrays, which as a compability hack can be converted into non-constant arrays implicitly.
1) "string literals" encased in double quotes are of type const char*. For backwards compatibility with C, C++ will let you ignore the constness of these strings, but an attempt to actually change them is likely to blow up in your face (because they're constants in "static storage" built right into the app and packed together; you might not even have write privileges for that bit of memory, and trying to increase the length of one of those strings will overwrite something else).

2) A single character can be represented as a character constant, by encasing it in single quotes. This gives you something of type char. (Well, the constant itself is const char, but it's a temporary anyway, and you certainly can assign it to a non-const char - and it won't be in the static storage.)

3) A backslash within a specification of text - both string literals and character constants - is used for escaping "special" characters, which couldn't be represented otherwise (for example, a return character is represented by \n). Thus, a single backslash is not valid; the compiler here will interpret the backslash and the next double-quote as an escape sequence for an actual double-quote character, and then wonder where the closing double-quote for the string literal is.

4) Use forward slashes for file names, even on systems where that's not the file path separator. You get some measure of cross-platform-ness for free - the / is required to be translated by the C++ runtime into the appropriate filepath separator for the system.

5) It is customary to use lines of the form string folder = "pictures" rather than string folder("pictures") for initialization (perhaps not so much for std::string because it's a class, but definitely for primitive types), but either will work.

6) A sprintf invocation needs to provide a formatting code in the format string (of the appropriate type) for every argument provided. In your case, you are missing format codes for the slashes - if you use a character constant for it, the code would be %c. Although you could of course just embed the /'s in the format string directly.

7) As you can probably tell, the C-style sprintf is extremely non-type-safe and error-prone. It will tend to blow up at runtime (and maybe not even in the most obvious "it's the sprintf call, stupid" kind of way, which makes it even harder to debug) if anything is the slightest bit wrong, and the compiler is completely helpless to tell you that anything's wrong with it. Since you are already smart enough to use std::string, you should be smart enough to do this the C++ way:

char slash = '/';string folder("pictures");string fileprefix("Movie");int frame = 42;stringstream filenameBuffer;// This thingy works just like any file stream, or the console streams cin and// cout, except that its "destination" is (at least conceptually) a std::string,// which can be retrieved (or set) via the .str() member function.filenameBuffer << slash << folder << slash << fileprefix << frame;// All completely typesafe through the magic of templates; the compiler// generates appropriate code for each type of appending.const char* filename = filenameBuffer.str().c_str();// It's so easy, happy-go-lucky. All ready for passing to an ifstream constructor.

This topic is closed to new replies.

Advertisement