reading from the open socket then writing to a file...

This is a discussion on reading from the open socket then writing to a file... within the C Programming forums, part of the General Programming Boards category; more like appending. check this out. It doesn't work for me. I cant understand why. When it is run, it ...

  1. #1
    Unregistered
    Guest

    reading from the open socket then writing to a file...

    more like appending. check this out. It doesn't work for me. I cant understand why.
    When it is run, it asks for your message... but then it doesn't append it. Any ideas?

    void postmessage (int sock)
    {
    int a = 0;
    char buffer[255]
    write(sock, "Message?\n", 10);
    bzero(buffer,255);
    n = read(sock,buffer,255);
    if (n < 0) error("ERROR reading from socket");
    if ((outputfile = fopen("messages.txt", "w")) == NULL)
    fprintf(stderr, "Cannot open %s\n", outputfile);
    a = 0;
    while(ch = buffer[a] != '|', a< 255)
    {
    n = putc(ch, outputfile);
    if (n < 0) error("ERROR writing to file");
    a = a + 1;
    };
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >fopen("messages.txt", "w")
    "w" is for write mode, you'll need "a", which is for append mode.

    Personally, I'd do the loop a bit different. The problem with yours is that it could loop 255 times, but there might not be 255 bytes received from the socket. (what if the user did not supply the | character?).

    Code:
    bytesread = read(sock, buffer, 255);
    
    for (i = 0; i < bytesread; i++)
    {
    	if (putc(buffer[i], outputfile) == EOF)
    	{
    		printf ("Write error\n");
    		break; /* stop the for loop */
    	}
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unregistered
    Guest

    Uh oh...

    When I do that, it just skips over the read part and doesn't let me input anything from my telnet client and skips back to the prompt. Any idea why thats caused? I can't figure it out... (this is really my first program... )

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Try this:
    Code:
    void postmessage (int sock)
    {
    	int i, bytesread;
    	char buffer[255];
    	FILE *outputfile;
    	write(sock, "Message?\n", 10);
    	bzero(buffer,255);
    
    	bytesread = read(sock, buffer, 255);
    	
    	printf ("read %d bytes >%s<\n", bytesread, buffer);
    	fflush(stdout);
    	
    	if (bytesread < 0) 
    	{
    		fprintf(stderr, "ERROR reading from socket");
    		return;
    	}
    	
    	if ((outputfile = fopen("messages.txt", "w")) == NULL)
    	{
    		fprintf(stderr, "Cannot open %s\n", outputfile);
    		return;
    	}
    
    	for (i = 0; i < bytesread; i++)
    	{
    		if (putc(buffer[i], outputfile) == EOF)
    		{
    			printf ("Write error\n");
    			break; /* stop the for loop */
    		}
    	}
    	
    	fclose(outputfile);
    }
    Make the mode "w" or "a" as you want.

    This code leaves a few holes, like getting more data from the same socket.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,853
    >>write(sock, "Message?\n", 10);

    you are asking it to send 10 bytes but only give it 9 as the terminator is not sent (not to mention if is UNICODE in which case is 18 bytes long). Use strlen() (or lstrlen() for 32 bit WIN OS compatibility).

    Strings within " " are null terminated by the compiler in C (AFAIK).

    WSAGetLastError() may help in debugging if a call to a socket fails.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >you are asking it to send 10 bytes but only give it 9 as the terminator is not sent
    10th byte is the '\0', even this will be written(sent) on the descriptor 'sock' since the number of bytes to be sent is 10 as passed to: write(sock, "Message?\n", 10);

  7. #7
    Unregistered
    Guest

    Talking

    Thx, I can probly get it to work marginally well now. Thx again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. Replies: 6
    Last Post: 05-12-2005, 03:39 AM
  4. writing a file to an open socket...
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 06-30-2002, 08:43 PM

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