fread stalls on socket but fget doesnt? - c

I'm working on a proxy server in c. I've gotten rather far using a combination of fread and fgets in different places but would like to reconcile and understand the difference. In the following example, I'm trying to use fread in a place i previously used fget successfully. Instead my server now hangs at the fread line. What's the difference and why is my program hanging?
void HandleTCPClient(int clntSocket)
{
FILE *request = fdopen(clntSocket, "r");
char reader[2000];
size_t q; //typo before
while((q=fread(reader, 1, sizeof(reader), request))>0) { //hangs here!
printf("i read something!\n");
}
return;
}
thanks!!
EDIT: so if i make the line "while((q=fread(reader, 1, 1, request))>0) {"
i get "i read something" all over my screen...
not sure what this means. So is it correct that fread will literally do nothing if there isn't at least your buffer's size number of characters present in the stream?

fgets returns when a newline is read while fread will block until requested number of bytes are available in the stream or on EOF. In your case, the call blocks because you do not have 2000 bytes of data ready in the stream.

Using fread() instead of recv() to read on a TCP socket seems strange to me..?
Anyway, fread is blocking as long as there is nothing to read. You should always check that a socket is ready to perform reading or writing, using select() for example on linux.

Related

why does fread from stdin not stop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
I am trying to read input from stdin with fread(). However i am have a problem, the loop will not terminate and instead keeps reading.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "argument err");
return -1;
}
FILE *in = fopen(argv[1], "w");
if (in == NULL) {
fprintf(stderr, "failed to open file");
return -1;
}
char buffer[20];
size_t ret;
while ((ret = fread(buffer, 1, 20, stdin)) > 0) {
if (fwrite(buffer, 1, ret, in) != ret) {
if (ferror(in) != 0) {
perror("write err:");
}
}
}
return 0;
}
How can i make this loop terminate when EOF is reached? i have tried using ctrl+D but that just seems like a strange way to stop taking input.
I guess what i want is to use fread() to read multiple arbitrary amounts of data in chunks of 20 bytes and then somehow stop.
How can i make this loop terminate when EOF is reached?
When do you think EOF is reached? Really. When you are providing input interactively, how is the system or the program supposed to know that you've entered all the data you want the program to consume?
i have tried using ctrl+D but that just seems like a strange way to stop taking input.
It is exactly the way to signal a soft EOF to a POSIX terminal. Since you want the loop to stop when EOF is encountered, it seems absolutely natural to me to use ctrl+D for the purpose when providing data interactively. That's not the only way you could signal the end of the input, but it has a lot going for it.
I guess what i want is to use fread() to read multiple arbitrary amounts of data in chunks of 20 bytes and then somehow stop.
Again: how is the program supposed to know when it has consumed all the "multiple arbitrary amounts" of data that you decide to provide on a given run? An EOF signal is an eminently reasonable choice for multiple reasons, and the way to deliver that from a POSIX terminal interface is ctrl+D.
As pointed out before you are reading from an eternal stream, this means that stdin don't naturally have a EOF (or <=0) value.
If you want your loop to terminate, you will have to add a termination condition, like a certain character, word or all type of value. After that you could use a break or a return in some case. You could also search if your terminal emulator support the insertion of an EOF value into the stdin, which is pretty common (But very platform dependent).
ADD: On my system, typical linux, CTRL+D is for an EOF insertion in stdin. It seems that you found this out yourself, and if you want your program to know where to stop you will need to use this.
You cand also send a signal to your program, usually done with a shortcut like CTRL+D, CTRL+C, CTRL+T etc... there is all sort of signal, which can be sent by your system or/and your TE and you just have to implement in your program the corresponding signal receiver.
How can i make this loop terminate when EOF is reached? i have tried using ctrl+D but that just seems like a strange way to stop taking input.
fread and fwrite are there to read data records, so they (both) take the number of records to read and the size of the record. If the available data doesn't fit on a full record, you will not get the full record at all (indeed, the routines return the number of full records read, and the partial read will be waiting for the next fread() call.)
All the calls in stdio.h package are buffered, so the buffer holds the data that has been read (from the system) but not yet consumed by the user, and so, this makes me to wonder why are you trying to use a buffer to read data that is already buffered?
EOF is produced when you are trying to read one record and the fread() call results in a true end of file from the system (this normally requires two calls, the first to complete the remaining data, the second resulting in no data ---zero bytes--- returned from the system) So you have to distinguish two cases:
fread() returns 0 in case it has read something, but is not enough to complete a record.
fread() returns EOF in case it has read nothing (the true end of file is reached)
As I've said above, fread() & fwrite() will read/write full records (this is useful when your data is a struct with a fixed length, but normally not when you can have extra data at the end)
The way to terminate the loop should be something like this:
while ((ret = fread(buffer, 1, 20, stdin)) >= 0) {
if (fwrite(buffer, 1, ret, in) != ret) {
if (ferror(in) != 0) {
perror("write err:");
}
}
}
/* here you can have upto 19 bytes in the buffer that cannot
* be read with that record length, but you can read individually
* with fgetc() calls. */
so, if you read half a record (at end of file) only at the next fread() it will detect the end of file (by reading nothing) and you will be free of ending. (beware that the extra data that doesn't fill a full buffer, still needs to be read by other means)
The cheapest and easiest way to solve this problem (to copy a file from one descriptor to another) is described in K&R (in the first edition) and has not yet have better code to void it, is this:
int c;
while ((c = fgetc(in)) != EOF)
fputc(c, out);
while it seems to read the characters one by one, it actually makes a call to read(2) to completely fill a full buffer of data, and return just one character, next characters will be taken from the buffer, saving calls to read(), and the same happens to fputc() (it fills the buffer until it's full, then flushes it, in a single call to write()).
Many people has tried to defeat the code above, without any measurable gain in efficience. So, my hint is be simple, that the world is complicated enough to force you to go complex.

How to rewind a file descriptor using only system calls?

I have this sample code where I'm trying to implement for my operating systems assignment a program that copies the contents of an input file to an output file. I'm only allowed to use POSIX system calls, stdio is forbidden.
I've thought about storing the contents in a buffer but in my implementation I must know the file descriptor contents size. I googled a little and found about
off_t fsize;
fsize = lseek (input, 0, SEEK_END);
But in this case my file descriptor (input) gets messed up and I can't rewind it to the start. I played around with the parameters but I can't figure a way to rewind it back to the first character in the file after using lseek. That's the only thing I need, having that I can loop byte by byte and copy all the contents of input to output.
My code is here, it's very short in case any of you want have to take a look:
https://github.com/lucas-sartm/OSAssignments/blob/master/copymachine.c
I figured it out by trial and error. All that was needed was to read the documentation and take a look at read() return values... This loop solved the issue.
while (read (input, &content, sizeof(content)) > 0){ //this will write byte by byte until end of buffer!
write (output, &content, sizeof(content));
}

Errors when implementing a fwrite to get date from socket

I see a plenty of examples but none addresses what I want to accomplish. I need to read the bytes from a socket and write them in to a file. In this Code Project blog I see where in the client script a while loop iterates through a read call:
while((n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
So I modified the code do that fputs(recvBuff, f1) where f1 is a pointer to a pdf file. A pdf file is also a file I'm fetching from the server so I need to reassemble it, however the fputs operated with a string and corrupts the file, so I need a byte "writer" so fwrite would have been the choice but I can't get fwrite to work. I ended up modifying my code to resemble some of the examples to test it out but to no avail.
If in fwrite the first parameters is the 'data' how would I pass it? I've tried the read() call as in the while loop above but that seem to return an integer rather then a byte stream. Any ideas?
I'm new to programming but am new to C and would appreciate a little push in a right direction. Thanks.
You want something more like this. fwrite doesn't return a stream it returns the number of items (i.e. the 3rd parameter) successfully written. In this case the "item" is a single char and you are attempting to write "bytesRead" number of them. Good form dictates that you should check that the result fread returns is the same as you requested be written but this rarely fails on a disk file so many people skip it in non-critical situations. You may want to add that on yourself.
FILE *f1;
int sockfd;
char recvBuff[4096];
size_t bytesWritten;
ssize_t bytesRead;
while((bytesRead = read(sockfd, recvBuff, sizeof(recvBuff))) > 0)
bytesWritten = fwrite(recvBuff, 1, bytesRead, f1);

Really slow disc image creation (iso 9660) in C

As a fun project I thought I'd write a program to make iso files. As far as I can tell it works, but it reads only 4KB every 30 seconds. I used eject -x 11 to slow my cdrom drive to a reasonable speed. Without it the drive runs at full speed and kills the process pretty quickly. Any suggestions to make this faster/better will be much appreciated.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUFFSIZE 4092
int main(int argc, char **argv)
{
FILE *fp = fopen("/dev/cdrom", "r");
FILE *file = fopen(strcat(argv[1], ".iso"), "w");
printf("Copying...\n");
while(!feof(fp))
{
char *line=(char *)malloc(sizeof(char) * BUFFSIZE);
fgets(line, BUFFSIZE, fp);
fprintf(file, "%s",line);
free(line);
}//end while
fclose(fp);
fclose(file);
printf("Done!\n");
return 0;
}//end main
fgets() handles text and is line oriented, and wastes time looking for newlines. Furthermore, it and fprintf() don't handle NUL bytes, and can get very confused by them. You want to use binary IO, i.e. fread() and fwrite(). There is also no need to continually free() and reallocate your buffer.
If you want to use Unix IO primitives rather than the C wrappers, you could use read() and write() or mmap() instead.
Buffered IO is hardly appropriate in
this scenario, neither is fgets which
scans the input for a newline. Look
into mmap.
Continuous buffer
de/re-allocation slows you down.
Fprintf is not right for writing binary data. It's also slow.
I am no C guru, but sounds like you need to implement some form of buffered readers and writers of data to see performance improvements.
First of all, I wouldn't go with formatted input (fgets & text mode), but with raw binary input (fopen with the b flag, fread and fwrite for writing). This way stdio doesn't need to perform the substitutions needed for the text mode (if needed on your platform), and you have a series of fixed-size reads that should perform better than fgets' waiting for the \n.
Then, I would get rid of that dynamic memory allocation; the continuous allocation/deallocation is probably killing the performance of your application. Just allocate once for all a static buffer on the stack (e.g. 8192 bytes) and use it all the time.
Move the malloc and free outside the loop.

problem with fread

i am trying to read one byte at a time from a file:
size_t result_new = 1;
char buf6[1];
if( (result_new = fread(buf6, 1, 1, pFile)) != 1)
{
printf("result_new = %d\n", result_new);
printf("Error reading file\n");
exit(1);
}
result_new is becoming 0 and it is printing the error. any idea what can be wrong. im sure pFile is fine.
thanks
According to the documentation:
fread() and fwrite() return the
number of items successfully read or
written (i.e., not the number of
characters). If an error occurs, or
the end-of-file is reached, the
return value is a short item count (or zero).
So why don't you check error code that will answer your question? You can use perror, for example.
If you only need one byte, getc would be a much better choice than fread. The interface is simpler and it's likely to be a lot faster.
http://www.cplusplus.com/reference/clibrary/cstdio/fread/ has an example with reading from a file. It is a c++ page but should work for c
Keep in mind when using fread and fwrite that strange errors can occur in some cases when the file is opened for normal text writing. Opening the file for binary will eliminate this potential problem. It's mainly due to "new lines", which seem for some reason to differ between binary and text file reading and writing.

Resources