Couple of questions.

Started by
4 comments, last by Eowan 19 years ago
I'm reading through an SDL book right now, and it has a couple of things I'm not quite comfortable with. "%d"? What is this? fprintf(stdout, " TEXT" , Function call); I would try right it like this. Cout << "Text" << Frisky.GetAge() ; I haven't really done Text to File output yet though. Is this some form of that? Thx
Advertisement
*printf are functions from C which use "control sequences" starting with % to express the formatting of output, and are retained in C++ for backwards compatibility. Do it the way you like; there's little utility to learning C output functions in C++, other than to be able to read and work with others' code.
The C IO function family (prinf, fprintf, sprintf, snprintf...) control the formatting of their output through what is known as a "format string". In it, each variable you want to print out is represented by a placeholder, composed of a % sign, followed by some optional argument and a letter. The letter determines the type of the variable to be output. Unlike C++'s IOStreams, C's printf rely on the vararg mechanism to accept a variable number of arguments, of any (C) type, and cannot infer that type directly (you have to yourself make sure that the types and numbers of variables specified in the format string match those of the variables you pass to the function).

Here, "%d" indicates a signed integer printed out in decimal form without additional formatting options. Thus

printf("Hello %d world", 42); would print out Hello 42 world.

fprintf prints to a file (the first argument, as you seem to have correctly deduced given your cout attempt).

Unfortunately, the correspondence between formatting options in C's printf routines and C++'s IOStreams is a bit complicated. There have been libraries written to simplify that (such as boost::format), but I won't get into it right now (unless you really want to).

So, to do the conversion, begin by looking for percent (%) signs and break your stream output at that point, so:

fprintf(stdout, "Hello %d world", myvar );

would become

std::cout << "Hello " << myvar << " world";

Only if there are other things in between the percent and the letter (most often, digits), do you have to worry about how formatting is done with C++'s IOStreams (if you find such format string, come back here and we'll give you a hand). You can also ignore most of the difference between the letters ("%d" vs "%s" vs. "%f"), since IOStream automatically detect the type of the variable. Again, some letters do imply specific formatting options ("%f" vs. "%g" vs. "%e"), but let's not worry about it for now.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Thx. I'm going to read about text file input/output in my Big C++ Book.
Both formats should work. I think I saw in one of the appendixes that this older I/O stuff was covered briefly. I'll read that too.



Another question has come up. What is this? It came up in my SDL book, however, I think it's related to all Graphic apis.

int main(int argc, char* argv[])




I have never seen this. Parameters being passed to main? What is this? I have never ever passed parameters into main. What are these parameters and what do they do? Where do they come from? Haha.
in c++ main can return a value, which is often accessable through whatever OS you're using.(i dont think it applies in linux, not really sure there...) argc and argv are the number of command line arguments and a char pointer to the actual command line. the name of your program is included. It has no direct relation graphics api besides SDL requiring its use on certain platforms.

so if you have the program
[source lang=cpp]#include <iostream>using namespace std;int main(int argc, char *argv[]){if(argc > 1)   {    for(int i = 0; i <= argc - 1; i++)       {        cout << argv << "\n"; // \n is formatting for new line...       }     return 1;    }return 0;}

your program would output its name and all the command line options and return 1 to the OS. (msvc++ call what main returns an exit code).
or with no command line options it woul print nothing and return 0 to the os.
Hope this helps.
The return value is usefull when you invoke programs from shell scripts, or the traditional .bat files on MS DOS/Windows. The script can the perform actions depending on the return values.

This topic is closed to new replies.

Advertisement