Why is my program returning me 9 when the file has "test1" written? The program looks for number of characters within the file. I wanted to run off the 'while(fgetc(file) != EOF)' method but this seems not to be working.
I would appreciate some help on this. Thank you
#include <stdio.h>
#include <stdlib.h>
#define FORMATLOG "FORMAT ERROR: invalid parameter: s5e4 <filename.txt>"
#define TXFILELOG "FILE ERROR: Can't open file or file does not exist"
enum { true, false };
int interface(char *filename) {
FILE *f_text = fopen(filename, "r+");
size_t size;
char c;
if(f_text == NULL) {
puts(TXFILELOG);
return NULL;
}
fseek(f_text, 0, SEEK_END);
size_t length = (size_t) ftell(f_text);
printf("%d", length);
fclose(f_text);
return true;
}
int main(int argc, char **argv) {
if(argc != 2) {
puts(FORMATLOG);
return false;
}
return interface(argv[1]);
}
Why the complication??
See http://linux.die.net/man/2/stat
ie
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
struct stat buf;
stat("filename", &buf);
printf("Size:%d\n", buf.st_size);
return 0;
}
(oops missed out the error check but I guess you get the picture)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.