On 21 Nov, 08:10, paul.leme...@gmail.com wrote:
Quote:
I am trying to create a series of 4K files, everything works fine
until I pass the 1022 mark - I get an error stating that he file
cannot be opened.
>
Can someone look at the below code and offer a suggestion for a
solution?
|
your program layout is horrid. This is the layout cleaned up
with a few comments
/
************************************************** **************************/
#include <stdio.h>
#include <stdlib.h>
int main ()
/*** njk better style is
int main (void)
***/
{
FILE **fp;
/*** njk this is unusual ***/
char filename [50];
/*** njk magic number ***/
long i;
long count;
long number;
int k = 32000;
/*** njk magic number and/or unhelpful variable name ***/
/*** njk was k supposed to be 1024? ***/
long datasize;
datasize = 4 * k;
printf ("How many files to create: ");
/***njk you need an fflush(stdout) ***/
scanf ("%d\n", &number);
/*** njk no error check on scanf() ***/
fp = malloc (sizeof(FILE*) * number);
if (fp != NULL)
{
for (i = 0; i < number; i++)
{
sprintf (filename, "%s%d.txt", "file", i + 1);
/*** njk why not
sprintf ("file%d.txt", i + 1);
***/
/*** njk you only use one file at a time. Why do you have an array of
FILE*s? ***/
if ((fp [i] = fopen (filename, "a+")) == NULL)
{
printf("Error: File \"%s\" cannot be opened\n",
filename);
continue;
}
count = 0;
while (count < datasize)
{
fprintf (fp[i],"%s",
"1234567812345678123456781234678\n");
/*** njk why not
fprintf (fp[i], "1234567812345678123456781234678\n");
***/
count++;
}
//fclose(fp[i]);
/*** njk why have you commented this out? ***/
}
}
else
{
printf("Error: Not enough memory\n");
getchar();
/*** njk non-standard function ***/
return 1;
/*** njk non-standard return from main() ***/
}
free(fp);
printf("All done\n");
getchar();
return 0;
}
--
Nick Keighley
printf() is your debugging friend! (We've all needed to
set breakpoints or pore over coredumps back in the day,
but judicious use of "if (TEST) printf()" is the
straightforward way to address most testing and debugging.)
(James Dow Allen clc)