File reading and writing

This is a discussion on File reading and writing within the C Programming forums, part of the General Programming Boards category; I tried to make a program that takes a file and turns it into a bunch of smaller files, but ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    21

    File reading and writing

    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"
    #include "stdio.h"
    #include "string.h"
    
    void main(){
    	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);
    	printf("\nBase name of output files : ");
    	scanf("%s",&On);
    	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");
    	for(l1=0;l1<Num;l1++){
    		On[0]=(char)(48+(int)(l1/100));
    		On[1]=(char)(48+(int)((l1-100*((int)On[0]-48))/10));
    		On[2]=(char)(48+(l1-100*((int)On[0]-48)-10*((int)On[1]-48)));
    		fl2=fopen(On,"w");
    		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);
    		printf("\noh= %d",oh);
    		oh2=fwrite(goan,1,oh,fl2);
    		if((0x1000*Siz)!=oh2){
    			printf("\nLooks like an error, but I could be wrong...");
    		}
    		fclose(fl1);
    		free(goan);
    	}
    	fclose(fl1);
    	printf("\n\n\tDone\n");
    	scanf("%c",end);
    	scanf("%c",end);
    }

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote 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);
    }
    Last edited by cwr; 03-06-2006 at 11:03 PM.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    21
    The first file is really small, around 100 bytes, and all the rest are 0 bytes.

    Code:
    #include "windows.h"
    #include "stdio.h"
    #include "string.h"
    
    void main(){
    	FILE *fl1, *fl2;
    	char Fn[255], On[251], end=0;
    	void *goan;
    	int Num, Siz, l1, l2, oh, oh2;
    
    	printf("Name of file to open: ");
    	scanf("%s",Fn);
    	printf("\nBase name of output files : ");
    	scanf("%s",On);
    	printf("\nNumber of files to make (less than 1,000): ");
    	scanf("%d",&Num);
    	printf("\nSize of files (KBs): ");
    	scanf("%d",&Siz);
    	printf("\n\nWorking: ");
    	fl1=fopen(Fn,"r");
    	if(fl1==NULL){
    		printf("\n\n\tFAILURE- file1\n");
    		fclose(fl1);
    		scanf("%c",end);
    		scanf("%c",end);
    		return;
    	}
    	for(l1=0;l1<Num;l1++){
    		sprintf(Fn, "%03d_", l1);
    		memcpy(&Fn[4],On,251*sizeof(char));
    		fl2=fopen(Fn,"w");
    		if(fl2==NULL){
    			printf("\n\n\tFAILURE- file2\n");
    			fclose(fl1);
    			fclose(fl2);
    			scanf("%c",end);
    			scanf("%c",end);
    			return;
    		}
    		goan=malloc((0x400*Siz));
    		if(goan==NULL){
    			printf("\n\n\tFAILURE- malloc\n");
    			fclose(fl1);
    			fclose(fl2);
    			scanf("%c",end);
    			scanf("%c",end);
    			return;
    		}
    		oh=fread(goan,1,(0x400*Siz),fl1);
    		printf("\noh= %d",oh);
    		oh2=fwrite(goan,1,oh,fl2);
    		if((0x400*Siz)!=oh2){
    			printf("\nLooks like an error...");//prints every time
    		}
    		fclose(fl2);
    		free(goan);
    	}
    	fclose(fl1);
    	printf("\n\n\tDone\n");
    	scanf("%c",end);
    	scanf("%c",end);
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    1. You're missing a header file
    2. A lot of your printf or scanf calls are using mis-matched conversion strings and parameters.

    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c:5: warning: return type of 'main' is not `int'
    foo.c: In function `main':
    foo.c:24: warning: format argument is not a pointer (arg 2)
    foo.c:25: warning: format argument is not a pointer (arg 2)
    foo.c:36: warning: format argument is not a pointer (arg 2)
    foo.c:37: warning: format argument is not a pointer (arg 2)
    foo.c:40: warning: implicit declaration of function `malloc'
    foo.c:45: warning: format argument is not a pointer (arg 2)
    foo.c:46: warning: format argument is not a pointer (arg 2)
    foo.c:56: warning: implicit declaration of function `free'
    foo.c:60: warning: format argument is not a pointer (arg 2)
    foo.c:61: warning: format argument is not a pointer (arg 2)
    foo.c:9: warning: unused variable `l2'
    > printf("\nLooks like an error...");
    This will always happen at some point, unless your file just happens to be the exact same size as the value you input.

    > fl1=fopen(Fn,"r");
    You should use "rb" and "wb" if you intend on using fread/fwrite

    > goan=malloc((0x400*Siz));
    There is no point in calling this (and free) inside the loop.
    Just allocate it once outside the loop before you start, then free it again when you leave the loop.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    21
    I don't know what you meant about me missing a header file, or calling mis-matched conversion strings, but I took your other 3 pieces of advice, and changing "r" and "w" to "rb" and "wb" made it work. Thanks.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > scanf("%c",end);
    Writes a character where you don't expect - could be anywhere in memory
    That's what the warnings all mean.

    scanf("%c",&end);
    All scanf parameters need to be pointers of one sort or another.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    21
    Oh yea, oops. hehe...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmetation fault (reading and writing file in c)
    By tasosa in forum C Programming
    Replies: 5
    Last Post: 04-13-2009, 06:04 AM
  2. Reading out of and writing into the same file
    By Wiretron in forum C Programming
    Replies: 8
    Last Post: 12-30-2006, 01:04 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. file writing and reading
    By Micko in forum C Programming
    Replies: 8
    Last Post: 01-13-2004, 10:18 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21