printf

Redirecting stdout in win32 does not redirect stdout

拜拜、爱过 提交于 2021-02-20 01:54:39
问题 I'm trying to redirect stdout so that printf in a windows application will go to a file of my choice. I'm doing this: outFile = fopen("log.txt", "w"); *stdout = *outFile; setvbuf(stdout, NULL, _IONBF, 0); but printf still writes to the console (or nowhere on a GUI based win32 application) I can redirect 'std::cout' by doing this: outFileStr = std::ofstream(outFile); std::cout.rdbuf(outFileStr.rdbuf()); But printf seems to be doing its own thing. Unfortunately, I need to redirect printf as I'm

How does printf extract digits from a floating point number?

浪尽此生 提交于 2021-02-18 22:43:31
问题 How do functions such as printf extract digits from a floating point number? I understand how this could be done in principle. Given a number x , of which you want the first n digits, scale x by a power of 10 so that x is between pow(10, n) and pow(10, n-1) . Then convert x into an integer, and take the digits of the integer. I tried this, and it worked. Sort of. My answer was identical to the answer given by printf for the first 16 decimal digits, but tended to differ on the ones after that.

How does printf extract digits from a floating point number?

孤者浪人 提交于 2021-02-18 22:42:52
问题 How do functions such as printf extract digits from a floating point number? I understand how this could be done in principle. Given a number x , of which you want the first n digits, scale x by a power of 10 so that x is between pow(10, n) and pow(10, n-1) . Then convert x into an integer, and take the digits of the integer. I tried this, and it worked. Sort of. My answer was identical to the answer given by printf for the first 16 decimal digits, but tended to differ on the ones after that.

Which of sprintf/snprintf is more secure?

做~自己de王妃 提交于 2021-02-17 08:20:26
问题 I wish to know which of these two options is the more secure one to use: #define MAXLEN 255 char buff[MAXLEN + 1] sprintf(buff, "%.*s", MAXLEN, name) snprintf(buff, MAXLEN, "%s", name) My understanding is that both are same. Please suggest. 回答1: The two expressions you gave are not equivalent: sprintf takes no argument specifying the maximum number of bytes to write; it simply takes a destination buffer, a format string, and a bunch of arguments. Therefore, it may write more bytes than your

Which of sprintf/snprintf is more secure?

天大地大妈咪最大 提交于 2021-02-17 08:20:11
问题 I wish to know which of these two options is the more secure one to use: #define MAXLEN 255 char buff[MAXLEN + 1] sprintf(buff, "%.*s", MAXLEN, name) snprintf(buff, MAXLEN, "%s", name) My understanding is that both are same. Please suggest. 回答1: The two expressions you gave are not equivalent: sprintf takes no argument specifying the maximum number of bytes to write; it simply takes a destination buffer, a format string, and a bunch of arguments. Therefore, it may write more bytes than your

Why does the same vsnprintf code output differently on Windows (MSVC) and Unix (Clang)

你说的曾经没有我的故事 提交于 2021-02-16 18:51:15
问题 On Unix (Clang 3.8.1), this code outputs: 6: 32 8: a8e On Windows (MSVC 19.00.24215.1), this code outputs: 6: 12345 6: a12345e #include <iostream> #include <stdarg.h> static std::string getFormattedString(const char* fmt, va_list ap) { int count = vsnprintf(NULL, 0, fmt, ap) + 1; std::cout << count << ": "; if (count <= 0) { return "unable to format message"; } std::string result = std::string(count, '\0'); if (vsnprintf(&result[0], count, fmt, ap) < 0) { return "error";} return result; }

address passing in scanf but not in printf [duplicate]

China☆狼群 提交于 2021-02-16 09:28:12
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Why scanf must take the address of operator Why do we pass the variable in the case of printf(), whereas the address of the variable in the case of scanf()? why to pass address in scanf 回答1: why to use '&' in scanf( ) but not in printf( ) 'printf'()' only need the values in order to output them. 'scanf()' stores values, so it needs a place to store them. This is done by providing the addresses (in pointers) of

C programming - issues with printing string with backspace characters stored

孤者浪人 提交于 2021-02-10 16:57:20
问题 I am writing a basic C program which takes a word from the user as input and prints that word twice on the same row. The issue that I am facing in printing the word twice is mentioned after the code that I have written below to do this job void print_word() { char ch; char str[15]; int i = 0; /* i will be used as index to access the elements of str */ printf ("\n Enter a word of your choice : ") ; ch = getch() ; /* Dont echo character */ while ( !isspace(ch) && i < 14) /* while loop executes

C programming - issues with printing string with backspace characters stored

陌路散爱 提交于 2021-02-10 16:55:34
问题 I am writing a basic C program which takes a word from the user as input and prints that word twice on the same row. The issue that I am facing in printing the word twice is mentioned after the code that I have written below to do this job void print_word() { char ch; char str[15]; int i = 0; /* i will be used as index to access the elements of str */ printf ("\n Enter a word of your choice : ") ; ch = getch() ; /* Dont echo character */ while ( !isspace(ch) && i < 14) /* while loop executes

Segmentation fault when casting int to char in C

跟風遠走 提交于 2021-02-10 10:23:00
问题 I have a very simple C program. In main, I have this operation: int main(){ int theNumber = 9009; printf("%s", (char *)theNumber); } And when I run it, it gives me a seg fault. Any idea why? Am I doing this wrong? Expected Output This code should convert theNumber to a string and then print it. So the output would be: 9009 Actual Output A segmentation fault. 回答1: This is trying to print whatever is found at the address 9009 . Seeing as the operating system is not giving your program access to