-
fprintf()
Ok. here's my problem, in my program i want the string WINCOM to be copied into the FILE, FILE *standard_input. then i want that sent into the do_commands function, However, when it hits the fprintf function, my program crashes. Here's the code below.
Code:
sprintf(WINCOM,"%s %s %s %s %s","WIN", stations_1, channel, time_on, time_off);
fprintf(standard_input,"%s",WINCOM);//causes an error:
do_commands(standard_input);
-
I think we will need to see more. Did you make sure to check the return from fopen()?
-
No, made the declaration FILE *standard_input, and then did the function above. I'm trying to turn this program into a WIN32 program, i have it done as a regular console program, and it just takes in "stdin", so I was trying to make my own, call it standard input and try to just substitute it in. So i was taking what was in the text boxes and stuff i put it in WINCOM, and i checked on my debugger, everythign gets copied. and then i was attempting to copy all of that into standard_input and then put it in do_commands. in my console program, do_commands just takes in stdin. So I hope i clarified what im trying to do there.
thanks.
-
what he's saying is did you make sure that standard_input is a valid file handle? before you write to it or anything, and right after using fopen, test to make sure that standard_input!=0.
Also, if that's not the problem, you might be overflowing the WINCOM array and missing the null terminator at the end, causing fprintf to keep reading till it crashes....and it might just be not crashing on sprintf just because it happens sometimes that way....so if you are opening a valid file, try increasing the size of your array a bit or making it dynamic.
-hope that helps
-
Ok I think I'm understanding what you are saying. As of right now standard_input is really stdin? If so stdin is read only. You can not write to it.
-
ok, ive now done this, im trying to write a command to the server, this is what ive done:
Code:
sprintf(WINCOM,"%s %s %s %s %s","WIN", stations_1, channel, time_on, time_off);
fd_rd2 = _open_osfhandle(ws_sockfd, _O_RDWR); // open file for read and write
if (fd_rd2 == -1) exit(-1);
standard_input = fdopen(fd_rd2,"w");
fprintf(standard_input,"%s",WINCOM);//causes an error:
do_commands(standard_input);
program doesnt crash anymore, but then again, its not exactly working the way i want it to either.
-
where were you trying to save it to before?
-
oh i see, well then how am i suppose to make it do my commands then
-
I thought we decided _open_osfhandle() wasn't working with sockets. I'd just use send():
Code:
sprintf(WINCOM,"%s %s %s %s %s","WIN", stations_1, channel, time_on, time_off);
send(ws_sockfd, WINCOM, strlen(WINCOM), 0);
Better yet, make yourself a sockPrintf():
Code:
int sockPrintf(SOCKET sock, LPCTSTR szFormat, ...)
{
TCHAR szText[4096];
va_list args;
/* Format the text into the buffer */
va_start(args, szFormat);
_vsntprintf(szText, sizeof(szText) / sizeof(szText[0]), szFormat, args);
szText[(sizeof(szText) / sizeof(szText[0])) - 1] = TEXT('\0');
va_end(args);
return send(sock, (char *) szText, lstrlen(szText), 0);
}
/* Sample usage: */
sockPrintf(ws_sockfd, "%s %s %s %s %s", "WIN", stations_1, channel, time_on, time_off);
-
send(ws_sockfd, WINCOM, strlen(WINCOM), 0);
that seems to have worked, I think, the server goes slow when i use that though. i mean after i exit my program, as im viewing the stplog file, then it starts to show what i wanted.. I was just thinking that since I had do_commands(stdin) working on my windows console version, there would be some way to make it work on my win 32 application, but i guess not.
-
ok When i put WSACleanup(); after the send function, it showed up on the stplog program. but just thjat one. I think there might be something going wrong with my sockets.
-
If I remember right, the server mostly uses fgets() which requires a newline character at the end of your input:
Code:
sprintf(WINCOM,"%s %s %s %s %s\n","WIN", stations_1, channel, time_on, time_off);
-
Excellent, thanks it worked!
-
anon, I wanted to ask a question though, it's that how come on the regular C program, i could still use stdin, but on this Win32 program i had to use the send function instead. also why did i need the "\n" at the end. thanks
-
>> anon, I wanted to ask a question though, it's that how come on the regular C program, i could still use stdin, but on this Win32 program i had to use the send function instead. <<
I'm not sure what the original was doing. You'd have to post the code. You can't write to stdin.
>> also why did i need the "\n" at the end. thanks <<
Because the server uses fgets() to get a line of text. That means it will keep reading characters until:
- It finds a new line character.
- The buffer is full.
- The connection is broken.
So, before you added the new line character, the server was not returning from the fgets() call until the client ended the connection.