LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Large file with c program (https://www.linuxquestions.org/questions/programming-9/large-file-with-c-program-397843/)

exvor 12-30-2005 09:02 AM

Large file with c program
 
When learning about random access files I was presented with this program that I recreated.


What im wondering is why it creates a file that is 768mb is size.


Is this the correct size because when a sizeof is done on just one of these stuctures i get a integer of 44. Im assumeing that 44 is in bytes.



here is the code
Code:


#include<stdio.h>

    struct clientdata {
    int acctNum;
    char lastName[15];
    char firstName[20];
    float balance;
    };

int main()
{
        struct clientdata blankclient = {0, "", "", 0.0};
        FILE *cfptr;
        int i;
        if ((cfptr = fopen("credit.dat", "w")) == NULL)
          printf("Error opening file for writeing\n");
        else
        {
                for (i = 1; 1 <= 100; i++)
                    fwrite(&blankclient, sizeof(struct clientdata), 1, cfptr);

                fclose(cfptr);
        }

        return 0;
}


Hko 12-30-2005 09:18 AM

Quote:

Code:

for (i = 1; 1 <= 100; i++)

Check out the red part above. You are checking if one is less-or-equal than hundred, which is of course always the case.

This way you have an endless loop that creates an endlessly growing file.
Replace the '1' (one) with an 'i' (small I), and your program will create a 4400-byte file as expected.

exvor 12-30-2005 09:21 AM

wooops!


LOL I really need to stop writeing code at 3am :p

thanks sorry for the stupid post.

dmail 12-30-2005 09:28 AM

I couldn't believe it when you said what the size was, so I ran it and had to abort it. Looking at the following line shows the error.
Code:


        for (i = 1; 1 <= 100; i++)

the test will always be true.

<edit>seems i was a little late at posting this, but thanks for making my windows box go belly up ;)

exvor 12-30-2005 09:54 AM

yea the 768mb aparently is the space im allowed on my shell account :p

when i ran this code in my bochs dos it crashed the os.


All times are GMT -5. The time now is 11:29 AM.