473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

File Sizes(s)?


I am trying to create 10 files of about 2GB each. The problem is that
the first file gets created at the size that I need, but the rest does
not. Please take a look at the below code, and let me know what I am
doing wrong? Thanks.

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE **fp;
char filename[20];
int i;
int count = 0;
int k = 32000;
int datasize;

datasize = 10000 * k;

fp = malloc( sizeof(FILE *) * 10);

if( fp != NULL )
{
for(i=0; i<10; i++)
{
sprintf(filename, "%s%d.txt", "file",i+1);

if( ( fp[i] = fopen(filename, "a+") ) == NULL )
{
printf("Error: File \"%s\" cannot be opened\n", filename);
continue;
}
while (count < datasize)
{
fprintf(fp[i],"%s","1234567812345678123456781234678\n");
count ++;

}

//fclose(fp[i]);


}
}
else
{
printf("Error: No enough memory\n");
getchar();
return 1;
}

free(fp);

printf("All done\n");
getchar();
return 0;
}

Oct 27 '08 #1
3 1632
Paul Lemelle wrote:
I am trying to create 10 files of about 2GB each. The problem is that
the first file gets created at the size that I need, but the rest does
not. Please take a look at the below code, and let me know what I am
doing wrong? Thanks.

#include <stdio.h>
#include <stdlib.h>

int main()
{
You really should fix your indentation!
FILE **fp;
Why not
FILE* fp[10];
char filename[20];
int i;
int count = 0;
Move count inside the loop, it's only used there and your problem is you
don't reset the value to zero each time.

A better type for count would be size_t, int may be too small.

--
Ian Collins
Oct 27 '08 #2

"Paul Lemelle" <Pl******@comcast.netwrote in message
news:o2********************************@4ax.com...
>
I am trying to create 10 files of about 2GB each. The problem is that
the first file gets created at the size that I need, but the rest does
not. Please take a look at the below code, and let me know what I am
doing wrong? Thanks.

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE **fp;
char filename[20];
int i;
int count = 0;
int k = 32000;
datasize = 10000 * k;
while (count < datasize)
fprintf(fp[i],"%s","1234567812345678123456781234678\n");
count ++;
Your figures don't add up: 10000*k is 320million. You're then writing
320million lots of 33 characters, or around 10GB in total. And
possibly to the one file, because count is not reset (as already pointed
out).
//fclose(fp[i]);
Why is this commented out?

And, you are appending to the files, so on each run the files will get
bigger.

It's not clear either why you are using malloc for setting up a small array
of file pointers (and having to deal with allocation failures). Or even why
you need an array at all.

--
Bartc

Oct 27 '08 #3
Paul Lemelle wrote:
I am trying to create 10 files of about 2GB each. The problem is that
the first file gets created at the size that I need, but the rest does
not. Please take a look at the below code, and let me know what I am
doing wrong? Thanks.

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE **fp;
char filename[20];
int i;
int count = 0;
int k = 32000;
int datasize;

datasize = 10000 * k;

fp = malloc( sizeof(FILE *) * 10);

if( fp != NULL )
{
for(i=0; i<10; i++)
{
sprintf(filename, "%s%d.txt", "file",i+1);

if( ( fp[i] = fopen(filename, "a+") ) == NULL )
{
printf("Error: File \"%s\" cannot be opened\n", filename);
continue;
}
while (count < datasize)
{
fprintf(fp[i],"%s","1234567812345678123456781234678\n");
count ++;

}

//fclose(fp[i]);


}
}
else
{
printf("Error: No enough memory\n");
getchar();
return 1;
}

free(fp);

printf("All done\n");
getchar();
return 0;
}
In this case. count is declared inside the main() function. You need to
declare it inside the for loop.
Oct 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tyaan | last post by:
Hi.. I'm a perl noob need to know how to write a script to parse a file containing one to four of the following blocks of text? I then want to print the results in a format showing the memory size...
2
by: donbro | last post by:
If my read of the extension source (Mac/Modules/file/_Filemodule.c) is correct, the parameter sizes specified for data and resource file sizes are UInt32 where they should be UInt64. In both OS9...
2
by: noleander | last post by:
I'm trying to get Vis C++ std to compile using /O2 optimizing flag. Many people have suggested downloading the MS C++ 2003 Toolkit ... it supposedly has C++ compiler bins that one could use. I...
3
by: Felix | last post by:
I'm writing a function where I'm comparing file sizes of the same file on two separate machines (client & server) and I'm using the System.IO.FileInfo.Length property. Is there a chance the...
1
by: Galen Somerville | last post by:
And yet another VB6 to VB2005 problem. All helpful suggestions appreciated. As you can see in the code below, my structures use fixed length strings and known array sizes. Consequently I can save...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
60
by: deko | last post by:
As I understand it, most browser manufacturers have agreed on 16px for their default font size. So, this should be an accurate conversion for percentages: px % 16 = 100 14 = 87.5 13 =...
1
by: Kyote | last post by:
I'm wanting to copy/move files from 1 directory to another in my program. I'm even doing a bit of renaming to help these specific files conform slightly to my preferred naming conventions to help...
10
by: deciacco | last post by:
I'm writing a command line utility to move some files. I'm dealing with thousands of files and I was wondering if anyone had any suggestions. This is what I have currently: $arrayVirtualFile =...
2
by: Edwin.Madari | last post by:
#your thought is right. ======================================================= def sizes2fields(sizes): d = begin = 0 for i in sizes: if begin: end = begin + i else: end = i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.