unsigned int file = open(argv[1], O_RDONLY);
printf("%u\n", file);
printf("%u\n", elf.offset);
lseek(file, elf.offset, SEEK_SET);
printf("%u", file);
OutPut:
3
52
3
Shouldn't file be set to 52?
Upon successful completion, the resulting offset, as measured in bytes from the beginning of the file, shall be returned.
try this printf("lseek_offset: %d\n", lseek(file, elf.offset, SEEK_SET));
file is a file descriptor. When you print it, you print the file descriptor, not the offset. When you lseek it to an offset of 52, the file descriptor is still 3, so it still prints 3.
You can read more about file descriptors here.
yon confuse file with file decriptor. The latter is just a non-negative integer that identifies an open file.
maybe this example can help you to understand these two concepts better:
char buf[8];
int main(){
int fd = open("test", O_RDONLY);
off_t offset = lseek(fd, 0, SEEK_CUR);
read(fd, buf, sizeof buf);
printf("first read when offset = %d : %s\n", (int)offset, buf);
offset = lseek(fd, 32, SEEK_SET);
read(fd, buf, sizeof buf);
printf("second read when offset = %d : %s\n", (int)offset, buf);
return 0;
}
and the output is:
first read when offset = 0 : 0000000
second read when offset = 32 : 4444444
here are the contents of test:
0000000\n
1111111\n
2222222\n
3333333\n
4444444\n
Related
I am using Linux. C2 (client) should read and send contents of file F1.txt to C1 (server) in successive messages of size 256 bytes (or remaining size of the file when you get near the end of the file)
First, I get the F1.txt size in bytes and sent it to the server c1.
fseek(fp, 0L, SEEK_END); //move fp to the end of the file
int fileLen = ftell(fp); // get the file size because ftell returns the current value of the position indicator
fseek(fp, 0L, SEEK_SET); //seek back to the begin of the file
write(sock, &fileLen, sizeof(int)); //send file size to server c1
Next, I send the file in successive 256 bytes by a for loop. The client c2 code is
char buffer[BUF_SIZE] = {0}; //BUF_SIZE=256
for (int i = 0; i <((fileLen/256) +1); i++)
{
memset(buffer, 0, sizeof(buffer)); //clear buffer
fread(buffer, 1, i <(fileLen/256)?(sizeof(buffer)):(fileLen%256), fp); // read BUF_SIZE elements, each one with a size of 1 bytes,
printf("Message client_c2 sent: %s\n", buffer);
write(sock, buffer, i <(fileLen/256)?sizeof(buffer):(fileLen%256));
usleep(1000);
}
fclose(fp);
The server c1 read the filesize, and read the socket within the for loop:
int receiveFileLen;
read(clnt_sock, &receiveFileLen, sizeof(int));
printf("clinet_c2 file size is %d\n",receiveFileLen);
for (int i = 0; i < ((receiveFileLen/256) +1); i++)
{
memset(buffer, 0, sizeof(buffer)); //clear buffer
read(clnt_sock, buffer, i < (receiveFileLen/256) ? sizeof(buffer) :(receiveFileLen%256) );
printf("buffer that writen into file is %s\n",buffer);
fwrite(buffer, strlen(buffer), 1,fp);
}
(1) The problem is in client c2 code's fread(fp), when i printf("Message client_c2 sent: %s\n", buffer); I found every time at the end of buffer, there is a wrong char (square shape in which shows 0002.)
(2) In server c1 code. I read the socket and fwrite it to a local txt. fwrite(buffer, strlen(buffer), 1,fp); I tried strlen(buffer)+1, but it gives me wrong chars ^B in the txt, so I use strlen(buffer).
(3) In server c1 code, I cannot read the full content of the remaining size of the file when I get near the end of the file. In the for loop, I get the previous iterations correctly written into txt, but when comes to the last iteration, i = (receiveFileLen/256) in other words, only part of socket content is read and fwrite into the txt, there is still chars that should be read remaining in the socket.
Thank you for your help!
char x[3];
char buff, c;
x[0]='y';
int offset, i;
int fd;
fd = open("test1.txt", O_RDONLY);
if(fd==-1){ printf("Error on fopen."); exit(1); }
offset = lseek(fd, 1, SEEK_END);
printf("Size of file is: %d. \n", offset);
for(i=offset-1; i>=0; i--)
{
c = read(fd, &buff, 1);
printf("The character is: %c. \n", c);
}
close(fd);
Running this gives me.
Size of file is: 6.
The character is: .
The character is: .
The character is: .
The character is: .
The character is: .
The character is: .
The test file contains only the word "TEST". I want to be able to print the word backwards.
read reads at the current position, and moves the current position forward by the amount read.
There needs to be further seeks. Also lseek( ..., 1, SEEK_END); probably ought to be lseek(...,0, SEEK_END); I would be uncomfortable with seeking to beyond the file.
for( i = ... ) {
lseek(fd, size - i, SEEK_BEGIN );
read(...);
}
Use fstat() to get the file size, as lseek() to SEEK_END is not guaranteed to return the size of a file, and you can use pread() to read a file in reverse without having to do any seeking at all. This is without any error checking - which should be added to any code that's going to be used:
struct stat sb;
int fd = open("test1.txt", O_RDONLY);
fstat( fd, &sb );
while ( sb.st_size > 0 )
{
char buff;
sb.st_size--;
pread( fd, &buff, sizeof( buff ), sb.st_size );
printf( "Char read: %c\n", buff );
}
close( fd );
I'm reading in a tarfile like this:
fh = fopen(filename, "r");
if (fh == NULL) {
printf("Unable to open %s.\n", filename);
printf("Exiting.\n");
return 1;
}
fseek(fh, 0L, SEEK_END);
filesize = ftell(fh);
fseek(fh, 0L, SEEK_SET);
filecontents = (char*)malloc(filesize + 1); // +1 for null terminator
byteCount = fread(filecontents, filesize, 1, fh);
filecontents[filesize] = 0;
fclose(fh);
if (byteCount != filesize) {
printf("Error reading %s.\n", filename);
printf("Expected filesize: %ld bytes.\n", filesize);
printf("Bytes read: %d bytes.\n", byteCount);
}
I then proceed to decode the contents of the tarfile and extract the files stored within. Everything works correctly, and the files get extracted just fine, yet fread() is returning 1 instead of filesize. The output I get is:
Error reading readme.tar.
Expected filesize: 10240 bytes.
Bytes read: 1 bytes.
According to CPP Reference on fread, the return value should be the number of bytes read.
Try this:
//byteCount = fread(filecontents, filesize, 1, fh);
byteCount = fread(filecontents, 1, filesize, fh);
I am writing a file transfer program. However for some reason pread is returning 0 despite not being at the end of the file. Can anyone take a look at my code and see the problem? It works perfectly with fread.
int file;
unsigned int size = selfp->intdata;
file=open(str, O_RDONLY);
char buf[1024];
printf("filename = %s \n", str);//prints file name requested
printf("buffer = %s \n", buf);//prints nothing currently
printf("offset = %d \n",atoi(chunkIndex)*CHUNK);//prints 0 for first chunk
printf("CHUNK = %d \n", CHUNK);//prints 1024
int myoffset = atoi(chunkIndex)*CHUNK;
int dataSize=pread(file, buf, sizeof buf, myoffset);
printf("%d\n", dataSize);//returns 0
close(file);
Thanks!
I have the following bit of code (it's "example" code, so nothing fancy):
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
char buffer[9];
int fp = open("test.txt", O_RDONLY);
if (fp != -1) // If file opened successfully
{
off_t offset = lseek(fp, 2, SEEK_SET); // Seek from start of file
ssize_t count = read(fp, buffer, strlen(buffer));
if (count > 0) // No errors (-1) and at least one byte (not 0) was read
{
printf("Read test.txt %d characters from start: %s\n", offset, buffer);
}
close(fp);
}
int fp2 = open("test.txt", O_WRONLY);
if (fp2 != -1)
{
off_t offset = lseek(fp2, 2, SEEK_CUR); // Seek fraom current position (0) - same result as above in this case
ssize_t count = write(fp2, buffer, strlen(buffer));
if (count == strlen(buffer)) // We successfully wrote all the bytes
{
printf("Wrote to test.txt %d characters from current (0): %s\n", offset, buffer);
}
close(fp2);
}
}
This code does not return the first printout (reading) as it is, and the second printout reads: "Wrote test.txt 0 characters from current (0): " indicating that it did not seek anywhere in the file and that buffer is empty.
The odd thing is, if I comment out everything from fp2 = open("test.txt", O_WRONLY);, the first printout returns what I expected. As soon as I include the second open statement (even with nothing else) it won't write it. Does it somehow re-order the open statements or something else?
The line
ssize_t count = read(fp, buffer, strlen(buffer));
is wrong, you're taking the strlen of an uninitialized buffer. You likely want the size of the buffer like so:
ssize_t count = read(fp, buffer, sizeof buffer);
You should make sure buffer really contain a nul terminated string as well when you print it as one.
if (fp != -1) // If file opened successfully
{
off_t offset = lseek(fp, 2, SEEK_SET); // Seek from start of file
ssize_t count = read(fp, buffer, sizeof buffer - 1);
if (count > 0) // No errors (-1) and at least one byte (not 0) was read
{
buffer[count] = 0;
Are you perfectly sure you are cleaning out the file every time you run?
As written, the first time you run this, you'll only see the second printout, and the second time you might see the first one.