Thread: File problem

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    File problem

    Write a program that reads a file containg a list of numbers, and then writes two files, one with all numbers divisible by three and another containing all the other numbers.

    Code:
    #include <stdio.h>
    
    enum{SUCCESS,FAIL};
    
    void CharReadWrite(FILE *fin, FILE *fout, FILE *fout2);
    main(void)
    {
    	FILE *fptr1, *fptr2, *fptr3;
    	
    	char filename1[]="numberlist.txt";
    	char filename2[]="divideby3.txt";
    	char filename3[]="restofnumbers.txt";
    
    	int reval=SUCCESS;
    
    	if ((fptr1=fopen(filename1, "r"))==NULL)
    	{
    		printf("Cannot open %d.\n", filename1);
    		reval = FAIL;
    	}
    
    	else if ((fptr2 = fopen(filename2, "w"))==NULL)
    	{
    		printf("Cannot open %d.\n", filename2);
    		reval = FAIL;
    	}
    
    	else if ((fptr3=fopen(filename3, "w"))==NULL)
    	{
    		printf("Cannot open %d.\n", filename3);
    		reval = FAIL;
    	}
    
    	else
    	{
    		CharReadWrite(fptr1, fptr2, fptr3);
    		fclose(fptr1);
    		fclose(fptr2);
    		fclose(fptr3);
    
    	}
    
    	return reval;
    
    }
    
    	/*function definition*/
    
    void CharReadWrite(FILE *fin, FILE *fout, FILE *fout2);
    {
    	int c;
    
    	while((c=fgetc(fin)) !=EOF)
    	{
    	fputc(c, fout);		/*write to a file*/
    	putchar(c);			/*put the character to the screen*/
    
    	}
    }
    says it can't open the file and there are probably more problems as well

    Any help thanks

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Does it say that it can't open the input file or the output. If the former, make sure that you are inputting a valid file. If the latter then you may not have enough disk space.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    if ((fptr1=fopen(filename1, "r"))==NULL)
    {
       perror(filename1);
       reval = FAIL;
    }
    The perror function might give you some more information why the file could not be opened.

    Try to close all files when leaving the function:
    Code:
    else if ((fptr2 = fopen(filename2, "w"))==NULL)
    {
       printf("Cannot open %d.\n", filename2);
       flocse(fptr1);
       reval = FAIL;
    }

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    5
    Ok I redid my program with some help and now i'm getting an illegal break error any reasons why

    Code:
    #include <stdio.h>
    
    int main()
    
    {
    	int a,b,c,num,scan,data[1000];
    	FILE *input, *divide3, *rest;
    	input=fopen("numberlist.txt","r");
    	divide3=fopen("divide3.txt","w");
    	rest=fopen("rest.txt","w");
    
    	printf("Type in the multiple to seperate files by\n");
    	scanf("%d", &a);
    
    	if (input==NULL)
    	{
    		printf("Cannot locate file\n");
    			Exit();
    	}
    
    	for (c=0; input !=NULL; c++);
    	{
    		scan=fscanf(input, "%d\n", &data[c]);
    		{
    			num = c--;
    			break;
    		}
    	}
    
    	for(c=0; c<num; c++)
    	{
    		if(data[c]%a==0)
    		{
    			fprintf(divide3, "%d\n", data[c]);
    			b++;
    		}
    		else
    			fprintf(rest, "%d\n", data[c]);
    	}
    
    	fclose(data);
    	fclose(divide3);
    	fclose(rest);
    	return 0;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM