can someone please tell me what I am doing wrong here?

BirdDad

Golden Member
Nov 25, 2004
1,131
0
71
I have this program
Code:
#include <stdio.h>
int main ()
{

    FILE *pfile;
    FILE *pfile2;
    FILE *pfile3;
    FILE *pfile4;

    unsigned long long filesize = 0;
    unsigned long long counter;
    char filename[100] = "";

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

    pfile = fopen (filename,"rb");

    if (pfile==NULL)
        perror ("Error");
    else
    {
        fseeko64 (pfile, 0, SEEK_END);
        filesize = ftello64 (pfile);
        counter = filesize;
        fclose (pfile);
        printf ("File:\n%s\ncontains %I64d bytes:\n", filename, filesize);
        while (counter > 0)
            printf (" \n counter is at %I64d of %I64d" ,counter,filesize);
            counter = counter -8;
    }
    return 0;
}
it keeps on giving the same number without subtracting the 8 each time
where am I messing up?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
You're missing { } for your while.

It's safer to always use { } with if, else, for, do, while constructs.
 

purbeast0

No Lifer
Sep 13, 2001
53,666
6,547
126
to add on what Dave said, if you don't put brackets after conditionals, then it will just interpret the next line as what to do when the conditional is met and continue on.

so basically what you have is the same as the following:

Code:
while (counter > 0) {
    printf (" \n counter is at %I64d of %I64d" ,counter,filesize);
}
counter = counter -8;

you want to move the counter logic up inside that closing bracket.

also your program SHOULD be giving you an infinite loop of that print f statement once it gets into that while loop, because counter is never getting smaller.
 

Jaydip

Diamond Member
Mar 29, 2010
3,691
21
81
Apart from what others mentioned don't use gets it's not safe use fgets instead.