read() system call does not return EOF - c

I am trying to read all data from a file using open() system call. But I am having difficulties figuring out the end of file. The C EOF flag doesn't work. My program goes into an infinite loop. Here is the code. The file has less than 100 characters in it.
int main()
{
char buf[100] = {""};
i = 0;
int fd = open ("file1.txt", O_RDONLY);
int bytesread = read (fd, &buf[i], 1);
char c = buf[i];
while (c != EOF) {
i++;
int bytesread = read (fd, &buf[i], 1);
c = buf[i];
}
}

read(2) doesn't return EOF. Its return values are: 0 on reaching "end-of-file", -1 on error, positive value when as many bytes are read. Besides you are checking the data for EOF. Your loop condition is wrong.
Typically, you'd also check if read(2) was interrupted and if so, retry.
size_t i = 0;
errno = 0;
while (i < sizeof buf && read (fd, &buf[i], 1) >= 0 && errno != EINTR) {
i++;
errno = 0;
}
I am also not why you are reading only one byte at a time, which is not very efficient. You could always read chunks of data and check the return value to know the number of bytes read.
Note: Typically the macro EOF is also defined with value -1. So it could seem read(2) returns EOF but don't be confused.

The buffer doesn't holds EOF, it just hold the data in the file read.What you can do is
while(bytesread > 0 ){
i++;
bytesread = read (fd, &buf[i], 1);
}

Related

Is it necessary to use a while loop when using the read and write system calls?

I am practicing the read and write system call, the below code is working fine with a while loop and also without them. could you please tell me what is the use of while loop here, is it necessary to add it while using read and write system calls. I am a beginner. Thanks.
#include <unistd.h>
#define BUF_SIZE 256
int main(int argc, char *argv[])
{
char buf[BUF_SIZE];
ssize_t rlen;
int i;
char from;
char to;
from = 'e';
to = 'a';
while (1) {
rlen = read(0, buf, sizeof(buf));
if (rlen == 0)
return 0;
for (i = 0; i < rlen; i++) {
if (buf[i] == from)
buf[i] = to;
}
write(1, buf, rlen);
}
return 0;
}
You usually need to use while loops (or some kind of loop in general) with read and write, because, as you should know from the manual page (man 2 read):
RETURN VALUE
On success, the number of bytes read is returned (zero indicates end
of file), and the file position is advanced by this number. It is
not an error if this number is smaller than the number of bytes
requested; this may happen for example because fewer bytes are
actually available right now (maybe because we were close to end-of-
file, or because we are reading from a pipe, or from a terminal), or
because read() was interrupted by a signal. See also NOTES.
Therefore, if you ever want to read more than 1 byte, you need to do this in a loop, because read can always process less than the requested amount.
Similarly, write can also process less than the requested size (see man 2 write):
RETURN VALUE
On success, the number of bytes written is returned (zero indicates nothing was written). It is not an error if this
number is smaller than the number of bytes requested; this may happen for example because the disk device was filled.
See also NOTES.
On error, -1 is returned, and errno is set appropriately.
The only difference here is that when write returns 0 it's not an error or an end of file indicator, you should just retry writing.
Your code is almost correct, in that it uses a loop to keep reading until there are no more bytes left to read (when read returns 0), but there are two problems:
You should check for errors after read (rlen < 0).
When you use write you should also add a loop there too, because as I just said, even write could process less than the requested amount of bytes.
A correct version of your code would be:
#include <stdio.h>
#include <unistd.h>
#define BUF_SIZE 256
int main(int argc, char *argv[])
{
char buf[BUF_SIZE];
ssize_t rlen, wlen, written;
char from, to;
int i;
from = 'e';
to = 'a';
while (1) {
rlen = read(0, buf, sizeof(buf));
if (rlen < 0) {
perror("read failed");
return 1;
} else if (rlen == 0) {
return 0;
}
for (i = 0; i < rlen; i++) {
if (buf[i] == from)
buf[i] = to;
}
for (written = 0; written < rlen; written += wlen) {
wlen = write(1, buf + written, rlen - written);
if (wlen < 0) {
perror("write failed");
return 1;
}
}
}
return 0;
}

How to read a block device till the last byte

I want to read a block device file, block by block until last byte even if the block is full of zeroes.
My code is this. size is the no. of bytes I want it to read in one go - could be anything 4K, 8K etc.
for (int i = 1; i <= num; i++){
read_bytes = read(fd, buf, size);
if (read_bytes < 0) {
perror("read failed");
return 1;
}
lseek_offset = lseek(fd, size, SEEK_CUR);
if (lseek_offset < 0){
perror("lseek failed.\n");
return 1;
}
}
When a block is filled with zero bytes (not the length of block but the data in block), lseek fails with EINV.
And I can see from df -h that that disk is half full and rest is zero bytes since it was formatted with ext4 before using it.
As already mentioned by #Mat in the comments read already updates the file offset so you should remove the lseek call. From read(2) man pages:
On files that support seeking, the read operation commences at the current file offset, and the file offset is incremented by the number of bytes read. If the current file offset is at or past the end of file, no bytes are read, and read() returns zero.
Also note that the read call might fail due to an interrupt, so you should check the value of errno for that (I'm guessing you'd still like to continue reading).
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
for (int i = 1; i <= num; i++) {
read_bytes = read(fd, buf, size);
if (read_bytes < 0 && errno != EINTR) {
perror("read failed");
return 1;
}
}
Finally, note that you are not guaranteed that the size number of bytes will be read in one go (see read(2)), most likely because of a signal interrupt. So here's an idea of the top of my head. You could check file size in a while loop within a single iteration of the for loop to determine how much you still need to read. For example:
for (int i = 1; i <= num; i++) {
size_t remain = size;
while(remain) { // keep reading until you've read size bytes
read_bytes = read(fd, buf, remain);
if (read_bytes < 0 && errno != EINTR) {
perror("read failed");
return 1;
}
.... // determine the amount of bytes read in the last iteration
remain = ... // update the size to the bytes still needed to be read
}
}

Sending and receiving a file - sockets - c - windows

I tried and looked up TONS of pages over the net, found NOTHING that fits windows and is working always, I tried this one over the TCP protocol (streaming byte-by-byte untill you bump into 3 - I tried it on files with no 3 in it :PPPP and by 3 I mean the ASCII value 3 and not the digit '3').
Server side:
int sendFile(SOCKET s, const char* file_path)
{
FILE* fp = fopen(file_path, "rb");
int i, err = 0, bytesSent, isOk = 1;
char ch = 0;
if(!fp)
{
fclose(fp);
return 1;
}
while(ch != EOF && isOk)
{
fread(&ch, sizeof(char), 1, fp);
if(ch != EOF)
{
bytesSent = send(s, &ch, sizeof(char), 0);
if(bytesSent <= 0)
{
return 1;
}
}
else
{
isOk = 0;
}
}
ch = 3;
bytesSent = send(s, &ch, sizeof(char), 0);
fclose(fp);
return 0;
}
Client side:
int recvFile(SOCKET s, const char* file_path)
{
FILE* fp = fopen(file_path, "wb");
int bytesRecieved;
char ch;
if(!fp)
{
fclose(fp);
return 1;
}
bytesRecieved = recv(s, &ch, sizeof(char), 0);
if(bytesRecieved <= 0)
{
return 1;
}
while(ch != 3)
{
fwrite(&ch, sizeof(char), 1, fp);
putch(ch);
bytesRecieved = recv(s, &ch, sizeof(char), 0);
if(bytesRecieved <= 0)
{
return 1;
}
}
fclose(fp);
return 0;
}
The sockets are functioning well and sending and receiving well (I'm talking about sending regular messages, without the functions).
It's not returning 1, it's just turns into an infinite loop.
No idea why it's not working, any idea ? I'm totally desperate.
Instead of comparison with EOF, you should use the feof function while reading from file. EOF is just an error code returned by some functions, not an actual character in the file.
Also, I notice that the thing with the character 3 seems a way to signal the end of the file. You should consider normalizing the data transfer. You could send at the beginning of the communication the size of the file, and the client reads exactly the size of the file, to avoid having problems with files that contain that character (3 is still a valid character).
For inspiration, take a look at an old project of mine that implements this behavior (although it's linux only).
I once fell in trouble reading on tcp socket by chunks smaller than what was used for writing. On a socket you write packets of a determined length (write or send), and implementation may discard the end of the packet if read size is shorter.
From man page on recv : All three routines [recv, recvfrom, recvmsg] return the length of the message on successful completion. If a message is too long to fit in the supplied buffer, excess bytes may be discarded depending on the type of socket the message is received from.
When reading, you should allways use a buffer of a size at least equals of the longest buffer used in writing.
You could dump what you receive to confirm.

read() seems to go into infinite loop

I am trying to read data from a socket. For some reason I cannot understand, it seems the read function goes into an infinite loop. The reason that's what I think happens is because while using eclipse to debug, the debugger does not get past the read function. Also, when I run the program with terminal, it runs forever. Help!!
Additional info: Running on Linux, and, not sure if has anything to do with this issue, but I create threads in the program.
Another something I think I should mention: The first time read() is called it works as expected and reads the whole message that's in the socket. The problem starts when read() is called again the second time (when there is nothing left to read). I expected that read would return 0 which would end the function, but instead, read goes into an infinite loop.
Here's where it's all happening:
read_write_res block_read_reply(int fd, void* buf, int max, int* read_size) {
int flag = 1;
if (read_size != NULL)
*read_size = 0;
int i;
while (1) {
i = read(fd, buf, max); /* HERE is where the debbuger gets stuck */
if (i == 0 && flag == 1) //nothing to read
continue;
if (i == 0 && flag == 0)
return READ_WRITE_SUCCESS;
if (i < 0){
return READ_WRITE_FAILURE;
if (i > 0 && read_size != NULL)
*read_size += i;
}
flag = 0;
max -= i;
buf = (char*) (buf) + i;
}
return READ_WRITE_SUCCESS;
}
If read returns 0, it means you have reached end-of-file status. For a socket or pipe, this means there will be nothing more to read. EVER. So performing continue; in this case is definitely not what you want to be doing.

Socket programming problem in C

From the below piece of code, why I am getting Reading Socket for response
int Read(int sock, char *p, int size)
{
int remain, read=0;
remain = size;
while (remain > 0 ) {
if ((read = recv(sock, p, remain, 0)) < 0) {
/* Error */
return(read);
} else if (read == 0 || *p == 0x0a) {
/* EOF */
break;
}
remain -= read;
p += read;
}
return(size - remain);
}
while (!done)
{
printf("***Reading Socket for response***");
rsplen= Read(myVsHandle.sock,(char *)encXMLResponse,MAX_RSP_LEN);
if (rsplen < 0 )
{
printf("Internal Communication Error");
return -1;
}
else if (rsplen >0)
printf("Revieved response");
done++;
return 0;
else if (rsplen == 0)
{
printf("Reading socket");
}
You are waiting for MAX_RSP_LEN bytes to be read - is there that many bytes to be read? Maybe your process is stuck in a blocking read().
Also depending on the sort of socket you are recv()ing from, there is no guarantee on the amount of data you will read, so specifically looking for a value 0x0a may not work.
Your problem could be that you are not ending your output with a newline. Try ending your outputs with a newline (\n). stdout is line buffered, so you may not see anything for a long time if you don't output a newline.
Another possibility is that you don't return from Read() unless you read the specified number of bytes. Depending upon the value of MAX_RSP_LEN, and the amount of data available, Read() may wait forever.
Also, your test: *p == 0x0a looks suspicious. What are you testing here?
Edit: There is another "bug":
else if (rsplen >0)
printf("Revieved response");
done++;
return 0;
else...
You are missing curly braces. In the current form, the code shouldn't compile. Please post actual code.
This:
if ((read = recv(sock, p, remain, 0)) < 0) {
Should be
if ((read = recv(sock, p, remain, 0)) > 0) { // Greater then 0, because recv returns the number of bytes received if successful, if it fails -1.
You're missing curly braces around the:
else if(rsplen > 0)
... statements
It should be:
...
}else if (rsplen >0){
printf("Revieved response");
done++;
return 0;
} ...

Resources