int n_BYTE1 = 0; // <-- this int holds the length of the file
FILE *stream1;
// get length of files
do {
fgetc(stream1);
n_BYTE1++;
} while (feof(stream1) == 0);
n_BYTE1--;
rewind(stream1);
The problems is this seems to work only for relatively small files. Whenever I try to use it on a larger file (170 kB for example) it returns an incorrect filesize. Usually the filesize I get is WAY too small. I have had the program tell me that a 340kB file has in fact a size of only 1010 bytes! :confused: Could it be that there is a byte somwhere in these large files that the program interprets as the EOF?? Help me out, please. I'm lost. Thanks in advance.
Regards,
Markus
Edited by - Red Ant on August 3, 2001 8:50:16 AM
Got some trouble getting the lenght of a file
Hi there,
The following code is part of a little program I wrote to compare two similar files byte for byte. The job of this part is to get the length of a file.
The typical way to get the size of a file is to use the C library function stat: (put a less than and greater than around the include filename...)
#include sys/stat.h
...
struct stat results;
if (stat("input.bin", &results) == 0)
// The size of the file in bytes is in
// results.st_size
else
// An error occurred
Edited by - kingy on August 3, 2001 8:57:50 AM
#include sys/stat.h
...
struct stat results;
if (stat("input.bin", &results) == 0)
// The size of the file in bytes is in
// results.st_size
else
// An error occurred
Edited by - kingy on August 3, 2001 8:57:50 AM
“If you try and please everyone, you won’t please anyone.”
August 03, 2001 09:16 AM
Thanks again for your help. However, I just noticed that even though I now get the correct size of the file, the same problem continues to bug me in another part of the program.
The problem is the same as before. The program just won''t read large files to the end but instead reads only ''till the 1010th or so byte. Is the function I use to read the file not suitable for large files? What function would you guys use to read large files? Thanks again for your patience.
Markus
// This loop is suppose to run through both files and compare them byte for byte. If any differences are found they are stored in an array.for (ADDRESS = 0; ADDRESS < n_READ_to; ADDRESS++) { c_BYTE1 = (char)fgetc(stream1); c_BYTE2 = (char)fgetc(stream2); if (c_BYTE1 != c_BYTE2) { DIFF_ofs[index] = offset; DIFF_B1[index] = c_BYTE1; DIFF_B2[index] = c_BYTE2; index++; } offset++; }
The problem is the same as before. The program just won''t read large files to the end but instead reads only ''till the 1010th or so byte. Is the function I use to read the file not suitable for large files? What function would you guys use to read large files? Thanks again for your patience.
Markus
Thanks again for your help. However, I just noticed that even though I now get the correct size of the file, the same problem continues to bug me in another part of the program.
The problem is the same as before. The program just won''t read large files to the end but instead reads only ''till the 1010th or so byte. Is the function I use to read the file not suitable for large files? What function would you guys use to read large files? Thanks again for your patience.
Markus
// This loop is suppose to run through both files and compare them byte for byte. If any differences are found they are stored in an array.for (ADDRESS = 0; ADDRESS < n_READ_to; ADDRESS++) { c_BYTE1 = (char)fgetc(stream1); c_BYTE2 = (char)fgetc(stream2); if (c_BYTE1 != c_BYTE2) { DIFF_ofs[index] = offset; DIFF_B1[index] = c_BYTE1; DIFF_B2[index] = c_BYTE2; index++; } offset++; }
The problem is the same as before. The program just won''t read large files to the end but instead reads only ''till the 1010th or so byte. Is the function I use to read the file not suitable for large files? What function would you guys use to read large files? Thanks again for your patience.
Markus
1. What mode did you open both files in ? For this both should really be opened as binary: fopen("blah.bin", "rb")
2. What are the data types of ADDRESS and n_READ_to, they should be at least int.
3. Where does the value of n_READ_to come from ? It should be the size of the smallest of the two files.
4. How big are the DIFF_offs, DIFF_B1 and DIFF_B2 arrays ? If they aren''t big enough to hold n_READ_to elements, you could end up overwriting the end of the array, which can cause very hard to trace crashes and other random problems.
--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com
2. What are the data types of ADDRESS and n_READ_to, they should be at least int.
3. Where does the value of n_READ_to come from ? It should be the size of the smallest of the two files.
4. How big are the DIFF_offs, DIFF_B1 and DIFF_B2 arrays ? If they aren''t big enough to hold n_READ_to elements, you could end up overwriting the end of the array, which can cause very hard to trace crashes and other random problems.
--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
August 03, 2001 10:01 AM
This forum rocks! You guys have solved all my problems. Everything works as it should now. 
S1CA, I had opened both files in read only mode (fopen("bla.txt","r"). That seems to have been the problem. I did as you told me and opened in binary mode (rb) and now it works perfectly! Many thanks! See ya!
Markus

S1CA, I had opened both files in read only mode (fopen("bla.txt","r"). That seems to have been the problem. I did as you told me and opened in binary mode (rb) and now it works perfectly! Many thanks! See ya!
Markus
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement