fprintf()

This is a discussion on fprintf() within the C Programming forums, part of the General Programming Boards category; Ok. here's my problem, in my program i want the string WINCOM to be copied into the FILE, FILE *standard_input. ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    215

    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);

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I think we will need to see more. Did you make sure to check the return from fopen()?

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    215
    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.

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    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

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    215
    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.

  7. #7
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    where were you trying to save it to before?

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    215
    oh i see, well then how am i suppose to make it do my commands then

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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);

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    215
    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.

  11. #11
    Registered User
    Join Date
    May 2004
    Posts
    215
    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.

  12. #12
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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);

  13. #13
    Registered User
    Join Date
    May 2004
    Posts
    215
    Excellent, thanks it worked!

  14. #14
    Registered User
    Join Date
    May 2004
    Posts
    215
    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

  15. #15
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> 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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  2. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  3. sprintf and fprintf segmentation error
    By kona1 in forum C Programming
    Replies: 5
    Last Post: 06-21-2005, 11:55 AM
  4. fprintf to stderr crash programs
    By jlai in forum Windows Programming
    Replies: 2
    Last Post: 04-12-2005, 09:51 AM
  5. fprintf
    By bennyandthejets in forum Windows Programming
    Replies: 10
    Last Post: 11-16-2002, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21