Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-30-2005, 09:02 AM
|
#1
|
|
Senior Member
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,538
Rep:
|
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;
}
|
|
|
|
12-30-2005, 09:18 AM
|
#2
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
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.
|
|
|
|
12-30-2005, 09:21 AM
|
#3
|
|
Senior Member
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,538
Original Poster
Rep:
|
wooops!
LOL I really need to stop writeing code at 3am
thanks sorry for the stupid post.
|
|
|
|
12-30-2005, 09:28 AM
|
#4
|
|
Member
Registered: Oct 2005
Posts: 970
Rep: 
|
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 
Last edited by dmail; 12-30-2005 at 09:31 AM.
|
|
|
|
12-30-2005, 09:54 AM
|
#5
|
|
Senior Member
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,538
Original Poster
Rep:
|
yea the 768mb aparently is the space im allowed on my shell account
when i ran this code in my bochs dos it crashed the os.
|
|
|
|
All times are GMT -5. The time now is 04:52 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|