I’m basically trying to make this code work in windows and I have to use the sprint() and writefile() functions included in the windows API. I’m a little confused as to how to go about this, the windows C code seems a lot more complicated.
#include <stdio.h>
#include <string.h>
//Defining String used for name//
#define NAME "Rodger Rodger"
Main(){
char tbuf[35];
memset(tbuf, '\b', sizeof(tbuf));
sprintf(&tbuf[0], "Hello %s\n", NAME);
write(1,tbuf,sizeof(tbuf));
}
Output: Hello Rodger Rodger
This works however I need to do it in Windows using the writefile() and sprint() functions.
EDIT: Managed to do it in the end. This is the final code:
//C Programming in Windows //
//Timothy Ford //
#include <stdio.h>
#include <windows.h>
//Defining String used for name//
#define NAME "Timothy Ford"
int main(){
char tbuf[35];
//Handle used to define output
HANDLE Outta = GetStdHandle (STD_OUTPUT_HANDLE);
//DWORD used to store bytesWritten
DWORD written;
memset(tbuf, '\0', sizeof(tbuf));
sprintf(tbuf,"Hello %s\n", NAME);
//WriteFile used for output of string
WriteFile(Outta, tbuf, sizeof(tbuf), &written, NULL);
return 0;
}
I notice several problems with your code:
Don’t pass the address of
tbuf[0]tosprintf. Instead pass tbuf. Tbuf is already a pointer to character string:sprintf(tbuf, "Hello, %s\n", NAME);. Not that it will not work as is but I think the version I propose is both shorter and clearer.Why are you memsetting tbuf to ‘\b’? This is backspace while I believe you want to set tbuf to 0(‘\0’)
Here is the working version on ideone.