ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
// Define block size
#define BLOCK 512
void function(char *argv);
int main(int argc, char *argv[])
{
// Accept one input file
if (argc != 2)
{
printf("Usage: ./Test inputfile\n");
return 1;
}
// printf("argv: %s", argv[1]);
// Open file and check success, as otherwise defaults to NULL.
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
printf("File not valid.\n");
return 1;
}
// Allocate memory buffer with the defined block size
uint8_t *buffer = malloc(BLOCK * sizeof(uint8_t));
int counter = 0;
while (fread(buffer, BLOCK, 1, file) != EOF)
{
// Printf as a test until EOF
printf("test\n");
// Do something...
}
// Free memory and close file
free(buffer);
fclose(file);
return 0;
}
This programme should read an input file in 512 byte blocks until reaching EOF, which it does not seem to reach. The line for `printf("test\n") I added prints lines of test indefinitely. Can anyone tell me what I am doing wrong?
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803
Rep:
Quote:
Originally Posted by lxs602
[snip]
This programme should read an input file in 512 byte blocks until reaching EOF, which it does not seem to reach. The line for `printf("test\n") I added prints lines of test indefinitely. Can anyone tell me what I am doing wrong?
Thanks.
From the fread(3) manpage:
Code:
RETURN VALUE
On success, fread() and fwrite() return the number of items read or written. This number
equals the number of bytes transferred only when size is 1. If an error occurs, or the end of
the file is reached, the return value is a short item count (or zero).
fread() does not distinguish between end-of-file and error, and callers must use feof(3) and
ferror(3) to determine which occurred.
One thing to try: Looping on calls to feof and freading only if it returns "nope, not at EOF yet.
Reply #2 uses the fact that "zero is 'false-y'" and thus ends the while loop. This is very commonly done. But, the equivalent reply #3 is more explicit.
Another solution is to find out the size of the file and read the entire file in 1 shot.
Code:
int fileSize;
FILE *file = fopen(argv[1], "r");
fseek(file, 0, SEEK_END); //Go to end of file
fileSize=(int)ftell(file); //This returns a long, which is 32 bit (64 bit warning!)
fclose(file);
file = fopen(argv[1], "r");
if(fread(buffer, 1, fileSize, file)< fileSize)
{
Error Detected;
}
fclose(file);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.