Thread: Problem with FILE *

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    39

    Problem with FILE *

    Here is a snippet from the code I wrote that has been causing my console application to crash...


    FILE *pfile;
    char *receivedmsg;
    int bytes_received, bufflen = 4000;
    int iFileDescriptor /* valid SOCKET */

    receivedmsg = (char *)malloc(sizeof(bufflen));
    /* checking omitted */

    pfile = fopen("Test.html", "w");
    if(pfile == NULL) {
    printf("Could create file...exiting.\n");
    return 0;
    }
    while(bytes_received) {
    bytes_received = recv(
    iFileDescriptor
    ,receivedmsg
    ,bufflen /* this is what I set the buffer size to */
    ,0
    );
    if(bytes_received == -1) {
    printf("An error occurred while receiving message from remote server.\n");
    break;
    }
    else if(bytes_received == 0) {
    printf("Remote server closed connection.\n");
    break;
    }
    else {
    if(!fwrite(receivedmsg, sizeof(char), bytes_received, pfile))
    printf("An error occurred while writing to file!\n");
    }
    }
    } /* End of while loop */


    I have already isolated the problem -- it is the fwrite function call. I confirmed this by trying the above code with the API calls _lopen and _lwrite instead of fopen and fwrite, which worked perfectly. Problem solved, right? Wrong, because my objective is to find out WHY fwrite did not work. Any ideas regarding why fwrite is not working in this example?? -- I suspect pfile is the problem.

    Additional information:
    When testing I established a sockets connection between my computer and CNN's server. I attempted to download their home page.
    Yes, I parsed out the portion of the server response that includes some binary data, which appeared between the response header and the HTML code in this case. (Does any one know why that binary data is included?)
    I also tried fputs, which also resulted in a crash. That is why I suspect pfile, the stream, is the problem.
    I tried several variations of the fwrite call, which included: fwrite(receivedmsg, sizeof(bytes_received), 1, pfile); - and several others, none worked.
    I tried using char receivedmsg[4000] instead of dynamically allocating the memory -- same result.

    There must be a way to do this using C, right? What am I doing wrong?

    Thanks in advance.

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Cool

    1. Well where are you reading in the data?
    Mr. C: Author and Instructor

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You never test if malloc() failed to allocate memory...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    39
    Magos wrote, "You never test if malloc() failed to allocate memory..."
    Not in the snippet I provided, but I did in my source code, of course. (Notice in the snippet I wrote, "/* checking omitted */." That meant I omitted the check to see if the pointer == NULL.)

    Mister C wrote, "1. Well where are you reading in the data?"

    The recv() is reading in the data from a socket to receivedmsg
    . iFileDescriptor is the socket passed to recv(), and bufflen is the max size of the buffer.

    Sorry about the way the code appears in my post...must be hard to read.

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Hard to read, because your forgot to use the code tags.
    If you use code tags, more people will want to help you and faster.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    39
    Sorry, I forgot...

    Code:
    	FILE *pfile;
    	char *receivedmsg;
    	int bytes_received, bufflen = 4000;
    	int iFileDescriptor /* valid SOCKET */
    
    	receivedmsg = (char *)malloc(sizeof(bufflen));
    	if(receivedmsg == NULL)	{
    		printf("Could not allocate memory.\n");
    		return 0;
    	}
    
    	pfile = fopen("Test.html", "w");
    	if(pfile == NULL) {
    		printf("Could create file...exiting.\n");
    		return 0;
    	}
    	while(bytes_received) {
    		bytes_received = recv(
    			iFileDescriptor
    			,receivedmsg
    			,bufflen /* this is what I set the buffer size to */
    			,0
    			);
    		if(bytes_received == -1) {
    			printf("An error occurred while receiving message from remote server.\n");
    			break;
    		}
    		else if(bytes_received == 0) {
    			printf("Remote server closed connection.\n");
    			break;
    		}
    		else {
    			if(!fwrite(receivedmsg, sizeof(char), bytes_received, pfile))
    					printf("An error occurred while writing to file!\n");
    			}
    		}		
    	} /* End of while loop */

  7. #7
    fou
    Guest
    Code:
    receivedmsg = (char *)malloc(sizeof(bufflen));
    This only allocates four bytes of memory. Get rid of the sizeof.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    To be even more correct:

    receivedmsg = (char *)malloc(sizeof(char) * bufflen);
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You are using an unitialised variable:

    >int bytes_received
    >while(bytes_received)
    You have no idea what value bytes_received holds the first time you use it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You also have a memory leak. If pFile equals NULL, then you return 0, but you don't free the memory you previously allocated. Also forget to free the memory later, when you don't need it anymore.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM