Jump to content
  • Advertisement
Sign in to follow this  
blaze02

Dynamic printf

This topic is 4466 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <stdio.h>
int main()
{
	char printfformatstring[128];
	sprintf_s(printfformatstring, 128, "\\n");
	printf(printfformatstring);
	return 0;
}
Why does this not printf a newline? Instead, it prints '\' and 'n'. This technique for changing the the format specification string works fine for variables (using "%%" to generate a '%'). But the idea fails when I try to use "\\" to generate '\'. When debugging it is clear to see that the printfformatstring passed to printf is "\n". I have moved from dynamic bump mapping to trying to dynamicate printf and fprintf. It seems appropriate to put this in the beginner forum. And forget about the "no flaming" rule. I'm not a beginner, flame away (just don't flame my favorite word "dynamicate").

Share this post


Link to post
Share on other sites
Advertisement
Ahh, I see why it happens.
I forget that the newline character is a single character. Thus, it can't be generated by putting a '\' next to a 'n'.

Share this post


Link to post
Share on other sites
No. the \ character is an escape character in C/C++. To output a backslash, you do '\\'. To output a newline, you do '\n'. If you want a backslash followed by a newline, then you do "\\\n"

Share this post


Link to post
Share on other sites
Quote:
Original post by jflanglois
No. the \ character is an escape character in C/C++. To output a backslash, you do '\\'. To output a newline, you do '\n'. If you want a backslash followed by a newline, then you do "\\\n"


No what? Everything I said was right.

Share this post


Link to post
Share on other sites
My guess is that the newline character is not being added to your string, but instead the literal '\n'. This is what your string will have:

printfformatstring[0] == '\';
printfformatstring[1] == 'n';
printfformatstring[2] == NULL;

Whereas this is what you want:

printfformatstring[0] == '\n';
printfformatstring[1] == NULL;

Share this post


Link to post
Share on other sites
Put another way: backslash-escaping only happens within string literals. It's something the compiler does specially, only to string literals, at compile-time. At run-time, the printf() function doesn't care about backslashes; they're just characters. It only cares about % signs, because those are the escape characters that printf() understands.

That said, why on earth are you messing around with this ancient C text manipulation stuff :(

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Put another way: backslash-escaping only happens within string literals. It's something the compiler does specially, only to string literals, at compile-time. At run-time, the printf() function doesn't care about backslashes; they're just characters. It only cares about % signs, because those are the escape characters that printf() understands.

That said, why on earth are you messing around with this ancient C text manipulation stuff :(


Because C file i/o is 100 times better and easier than C++. I'm trying to shift a small string "->" to the right an arbitrary number of spaces. I'm giving sprinf "%%%ds" so that it will generate "%?s" depending on the integer value. And then I pass that to fprintf.

Share this post


Link to post
Share on other sites
Quote:
Original post by blaze02
Quote:
Original post by Zahlman
That said, why on earth are you messing around with this ancient C text manipulation stuff :(


Because C file i/o is 100 times better and easier than C++. I'm trying to shift a small string "->" to the right an arbitrary number of spaces. I'm giving sprinf "%%%ds" so that it will generate "%?s" depending on the integer value. And then I pass that to fprintf.



int numSpaces = 10;
std::string s(numSpaces , ' ');
s += "->";
std::cout << s << std::endl;






Making it output to a c++ fstream is just a matter of changing one line.

Is that all thats required? Or is it more complex.

IMO the c++ text manipulation is easier.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!