Originally Posted by
Noobwaker
I tried to make a program that takes a file and turns it into a bunch of smaller files, but the output files are always empty... can someone tell me where I'm messing up?
Code:
#include "windows.h" /* unnecessary */
#include "stdio.h" /* Should be <stdio.h>, not "stdio.h" */
#include "string.h" /* Should be <string.h>, not "string.h" */
void main(){ /* main returns int */
FILE *fl1, *fl2;
char Fn[255], On[255], end=0;
void *goan;
int Num, Siz, l1, l2, oh, oh2;
printf("Name of file to open: ");
scanf("%s",&Fn); /* don't use & here, it's an array, you already have the address of the first element */
printf("\nBase name of output files : ");
scanf("%s",&On); /* ditto */
printf("\nNumber of files to make (less than 1,000): ");
scanf("%d",&Num);
printf("\nSize of files : ");
scanf("%d",&Siz);
printf("\n\nWorking: ");
fl1=fopen(Fn,"r"); /* ALWAYS check the return value */
for(l1=0;l1<Num;l1++){
On[0]=(char)(48+(int)(l1/100)); /* wtf? try sprintf(on, "%03d", l1); instead. */
On[1]=(char)(48+(int)((l1-100*((int)On[0]-48))/10)); /* wtf? try sprintf(on, "%03d", l1); instead. */
On[2]=(char)(48+(l1-100*((int)On[0]-48)-10*((int)On[1]-48))); /* wtf? try sprintf(on, "%03d", l1); instead. */
fl2=fopen(On,"w"); /* ALWAYS check the return value */
goan=malloc((0x1000*Siz));
if(goan==NULL){
printf("\n\n\tFAILURE\n");
fclose(fl1);
fclose(fl2);
scanf("%c",end);
scanf("%c",end);
return;
}
oh=fread(goan,1,(0x1000*Siz),fl1); /* why are you trying to read 4096 times as much as the size specified? */
printf("\noh= %d",oh);
oh2=fwrite(goan,1,oh,fl2);
if((0x1000*Siz)!=oh2){
printf("\nLooks like an error, but I could be wrong...");
/* Does this get printed? */
}
fclose(fl1); /* Why are you closing your file for reading handle inside the loop? */
free(goan);
}
fclose(fl1);
printf("\n\n\tDone\n");
scanf("%c",end);
scanf("%c",end);
}