C fread question number of bytes left I need more help please

Onceler

Golden Member
Feb 28, 2008
1,264
0
71
I am attempting to write a program that reads in data from a file but need some help.
I need to know how many bytes are remaining in a file as it is read at runtime not compile time.
What are some ways that I can do this? I need the program to know the exact number of byte remaining in a file where I will be freading it in chunks of nine bytes and am strugling to figure out how the program can tell if there are <9 bytes left then do option B otherwise do option A.
Thanks
 
Last edited:

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
If you really need to know the bytes remaining you need to get the file size then start subtracting. Or you can keep reading until you reach end of file. Read up on: the return value of fread, feof and fseek/ftell to get the size.
 

Onceler

Golden Member
Feb 28, 2008
1,264
0
71
Thanks I had forgotten about those functions I have been so frustrated.
 

Onceler

Golden Member
Feb 28, 2008
1,264
0
71
I'm sorry but those just wont do I am testing this program out on a file that is 34,000,143,000 bytes size and those would work except that the file is so huge that it requires an unsigned long long
Any one have any more ideas?(preferably with examples).
Thank you guys for your advise.
 
Last edited:

Onceler

Golden Member
Feb 28, 2008
1,264
0
71

Crusty

Lifer
Sep 30, 2001
12,684
2
81
I want to avoid structs if possible.
I have come up with this:
Code:
#include <stdio.h>
FILE = *fp;
main()


{
    unsigned long long filesize;
    char[] s= "g:\\Mess.m2ts"
    fp = &s;
    open64("fp, "rb");
    fpos_t file_pos;
    filesize =ftell (fsetpos SEEK_END);
    printf (" the file size is : %ull",filesize);
    return 0;
}
But I can't get it to work
thanks

What? And why??? Makes no sense...
 

Onceler

Golden Member
Feb 28, 2008
1,264
0
71
well how would you go about it Crusty?
I am just a beginer who happens to need to be able to manipulate files that are huge.
 

Onceler

Golden Member
Feb 28, 2008
1,264
0
71
I am now attempting to use int64 instead of unsigned long long
should I be trying with int64 or _int64?
my code now looks like this:
Code:
 typedef unsigned __int6;
    FILE *fp;
    unsigned __int6 filesize;
    char s[]= "g:\\Mess.m2ts";
    fp = &s;
    fopen64(s, "rb");
    fpos_t file_pos;
    filesize =ftell064 (fsetpos SEEK_END);
    printf (" the file size is : %ud64",filesize);
but code::blocks stops on the filesize =ftell064 line and says :
C:\Users\Benjamin\Documents\filesize5.c||In function 'main':|
C:\Users\Benjamin\Documents\filesize5.c|7|warning: useless type name in empty declaration [enabled by default]|
C:\Users\Benjamin\Documents\filesize5.c|11|warning: assignment from incompatible pointer type [enabled by default]|
C:\Users\Benjamin\Documents\filesize5.c|14|error: expected ')' before numeric constant|
||=== Build finished: 1 errors, 2 warnings (0 minutes, 0 seconds) ===|
 
Last edited:

Schmide

Diamond Member
Mar 7, 2002
5,587
719
126
#define _LARGEFILE64_SOURCE

Edit: Sorry I put this in without context.

A few things. We need to know what compiler you're using and what target you are building for? For example in windows you would use _lseeki64 to get a 64bit value.

If compiling with gcc i believe you have to put a -D_FILE_OFFSET_BITS=64 as a compiler flag and use off_t as the receiving type and ftello() as the request.

Never needed to do this so I'm just passing on docs.
 
Last edited:

Onceler

Golden Member
Feb 28, 2008
1,264
0
71
What did you do?

I got this from the cprograming forum who in turn got it from daniweb:
Code:
#include <stdio.h>
// Code from http://www.daniweb.com/software-development/c/code/238780/fseeko64-and-ftello64-for-deal-with-large-files-eg.-file-dimension
// Edited for CB MinGW GCC

int main ()
{

    FILE * pFile;

    unsigned long long file_dim = 0;
    char file_name[260] = "";

/////////////////////////////////////////////////////////

    printf("ENTER [path] file name.ext:\n");
    gets(file_name);

    pFile = fopen (file_name,"rb");

    if (pFile==NULL)
        perror ("Error opening file");
    else
    {
        fseeko64 (pFile, 0, SEEK_END);
        file_dim = ftello64 (pFile);
        fclose (pFile);
        printf ("File:\n%s\ncontains %I64d bytes:\n", file_name, file_dim);
    }
    return 0;
}
now that I know how to accurately get the file size all I need now is help on freading in just 9 and just 9 bytes at a time from a very large file and incrementing the file position to read in the next 9 bytes after I have done calculations on the first nine or if my calculations don't use up the entire 9 then keep the leftover in a buffer and read in more minus whatever is in buffer like say I read in 9 but only the first 5 match my calculations then I would have four left over and I want them to go to the front of the buffer and read in five bytes to the end of the buffer so that buffer is always exactly nine bytes except when the last of the file is through and doesn't quite make up nine bytes for which I will need to devise some sort of check to see how many bytes are left at the end. A FIFO buffer.
Thank you
 
Last edited:

Crusty

Lifer
Sep 30, 2001
12,684
2
81
I was commenting on you saying you'd prefer to not use structs, and I was wondering WHY? There's no reason to not use a part of the language.... especially an important one.
 

Onceler

Golden Member
Feb 28, 2008
1,264
0
71
I was commenting on you saying you'd prefer to not use structs, and I was wondering WHY? There's no reason to not use a part of the language.... especially an important one.

I don't like them, they seem like OOP crap that I hate. I will use them if I have to but prefer not to.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
I don't like them, they seem like OOP crap that I hate. I will use them if I have to but prefer not to.

Ha, well, umm... structs are not very object-oriented. In fact structs (structures) are a fundamental component of C data manipulation, and you'll find it very hard to do anything, at all, of any kind, in C, without becoming familiar with them and how they are used.
 

Onceler

Golden Member
Feb 28, 2008
1,264
0
71
They seem to need an extra amount of space to implement them.
Like I said I will use them if I have to.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
They seem to need an extra amount of space to implement them.
Like I said I will use them if I have to.

They might for word alignment reasons, yes. That's not usually a huge consideration, and you can use compiler directives to pack them tighter.

Structs are so fundamental to programming in C (not to mention the system level APIs in Linux, Windows, and every other OS), that I honestly don't see how you can avoid them.