Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

File size information in C

Name: Anonymous 2010-05-29 15:30

Hey, /prog/

When you have a pointer to a FILE structure, what's the standard way of determining the size (in bytes) of the open file?

Name: Anonymous 2010-05-29 15:49

Read'em and count, you lazy bastard.

Name: Anonymous 2010-05-29 15:51

Does sizeof work in this case?

Name: Anonymous 2010-05-29 16:03

yes.

Name: Anonymous 2010-05-29 16:03

Use fseek to position the file pointer at the end of the file, then get the position using ftell and finally use fseek or rewind to position the file pointer back at the start.

ftell returns the offset as a long int, so it may fail on files whose sizes cannot be represented as a long int.

Name: Anonymous 2010-05-29 16:21

>>5
Thx anon, works great.

Name: Anonymous 2010-05-29 18:23

>>1
fstat or fseek+ftell.
>>3
Don't be silly. sizeof is for seeing how much storage a concrete variable needs. It's a compile time operator (the value is known at compile time), and thus it wouldn't work for such purposes.

Name: Anonymous 2010-05-29 18:25

Name: Anonymous 2010-05-29 18:34

>>8
The alternative to fseek/ftell is to read the whole file until you get an EOF. The OP did after all ask how to do it in C, not how to do it in POSIX or ENTERPRISE BULLSHITE.

Name: >>7 2010-05-29 18:35

>>8
There's also the problem of race conditions in multi-threaded environments. You'd have to lock the FILE object, get the current position, seek to end, get position, seek to current position, and exit the lock. However, I've used fseek+ftell+rewind quite a few times mostly for writing simple tools operating on binary files. There's no risk for those cases.

Name: Anonymous 2010-05-29 18:58

Sorry, just testing.

Name: Anonymous 2010-05-29 19:36

>>9
Platform specific interfaces beat implementation defined behaviour any day.

Name: Anonymous 2010-05-29 21:05

>>8
Honestly, I thought this thread was a game called "don't say fstat". Aw shit, I lost. I'll take my drink.

Name: Anonymous 2010-05-29 21:49

>>8
FIO19-C

Name: Anonymous 2010-05-29 22:27

A binary stream need not meaningfully support fseek calls with a whence value of SEEK_END.
Except for the fact that nearly every fucking implementation out there will correctly do this.

Name: Anonymous 2010-05-29 23:34

>>15
Are you sure about that? Most network-supplied streams couldn't possibly implement SEEK_END meaningfully.

Name: Anonymous 2010-05-30 1:00

>>16
2/10

Name: Anonymous 2010-05-30 1:25

>>16
Then fseek will fail and either the code wasn't designed for unbounded streams or the user is an idiot for trying to use one with it.

Name: Anonymous 2010-05-30 2:03

if you use fstat, make sure you use st_blksize and st_blocks to get the size, since st_size can overflow. if off_t is 32 bits, it'll overflow at 4GB. if off_t is 64 bits, it'll overflow at 16EB.

Name: Anonymous 2010-05-30 2:31

>>19
16EB should be enough for everyone.

Name: Anonymous 2010-05-30 4:36

>>13
fstat is not C.

Name: Anonymous 2010-05-30 4:50

>>20
16EB is nothing compared to the approximately 210122 bits of information in the universe.

Name: Anonymous 2010-05-30 5:18

>>21
FUCK YOU ZHIVAGO

Name: Anonymous 2010-05-30 5:38

https://www.securecoding.cert.org/confluence/display/seccode/PRE07-C.+Avoid+using+repeated+question+marks


size_t i = /* some initial value */;
/* assignment of i */
if (i > 9000) {
   if (puts("Over 9000!?""?!") == EOF) {
     /* Handle Error */
   }
}


Fucking /b/ is everywhere

Name: Anonymous 2010-05-30 6:31

>>24
Checking return value of puts
WHAT THE FUCK

Name: Anonymous 2010-05-30 7:09

>>25
Secure coding, my friend.  Check the return value of everything.  Gotta do shit properly if your program is going to be secure.

Name: Anonymous 2010-05-30 17:57

>>26

How the fuck do you handle such a shit?

Name: Anonymous 2010-05-30 18:47

>>27
delete system32

Name: Anonymous 2010-05-30 20:55

>>28
$ locate system32
$

:(

Name: Anonymous 2010-05-30 21:02

>>28
Get out.

Name: Anonymous 2010-05-31 5:40

>>30
THIS IS WHERE I BELONG

Name: Anonymous 2010-05-31 6:05

>>31
I agree. I love you

Name: Anonymous 2010-05-31 6:10

>>32
LOVE MY ANUS

Name: Anonymous 2010-05-31 6:25

>>31-32
samefag

Name: Anonymous 2010-05-31 6:31

>>34
WARNING: Incorrect samefag detection

Name: Anonymous 2010-05-31 7:05

>>35
You mean he missed one?

Name: Anonymous 2010-05-31 10:13

>>28,34,35
All of you twits need to fuck off back to the imageboards that spawned you.

Name: Anonymous 2010-05-31 10:17

>>27


size_t i = /* some initial value */;
/* assignment of i */
if (i > 9000) {
  if (puts("Over 9000!?""?!") == EOF) {
    if (ferror(stdout)) {
      if (fprintf(stderr, "printf failed\n") < 0) {
        if (fprintf(stderr, "fprintf failed\n")  < 0) {
         if (fprintf(stderr, "fprintf failed\n")  < 0) {
          if (fprintf(stderr, "fprintf failed\n")  < 0) {
           if (fprintf(stderr, "fprintf failed\n")  < 0) {
            if (fprintf(stderr, "fprintf failed\n")  < 0) {
             if (fprintf(stderr, "fprintf failed\n")  < 0) {
              if (fprintf(stderr, "fprintf failed\n")  < 0) {
               if (fprintf(stderr, "fprintf failed\n")  < 0) {
               }
              }
             }
            }
           }
          }
         }
        }
      }
    }
  }
}

Name: Anonymous 2010-05-31 10:54

>>38
Needs recursion

Name: Anonymous 2010-05-31 10:59

>>38
while (fprintf(stderr, "printf failed\n") < 0)
I rolled up your loops.

Name: Anonymous 2010-05-31 22:21

>>40
You forgot your semicolon.

Name: Anonymous 2010-06-01 7:25

>>40
Needs more funroll.

Name: Anonymous 2010-11-28 15:56

Name: Anonymous 2010-12-17 1:25

Are you GAY?
Are you a NIGGER?
Are you a GAY NIGGER?

If you answered "Yes" to all of the above questions, then GNAA (GAY NIGGER ASSOCIATION OF AMERICA) might be exactly what you've been looking for!

Don't change these.
Name: Email:
Entire Thread Thread List