Sign in to follow this  
ncasebee

Couple of questions.

Recommended Posts

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

Share this post


Link to post
Share on other sites
*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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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[i] << "\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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this