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.