Thread: why i get this run time error

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    why i get this run time error

    i get on line 47 error
    when it tries to close the file

    why the fopen return NULL?

    and even if i eliminate every fclose
    its doing the same this inside the function
    ??

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    
     
    typedef struct vote {
    	char name [30];
    	int id,type,candidate;
    } vote;
    void report(FILE* college[3], FILE* arr[10], char* canceled);
    // Fuction returns the minimal id
    int getMin(int eof[],vote [],int *);
    // Function checks a vote record correct
    int votecheck(vote ,vote [],int i);
    // Function reads the next record from "college" file
    int readFrom(FILE *,vote [],int);
    
    
    
    int main ()
    {
    FILE* f[10];
    FILE* c[3];
    char  name[3]="canceled";
    
    c[0]=fopen("c:\\file1","r");//vote files ,each file represents the votes from a college
    c[1]=fopen("c:\\file2","r");
    c[3]=fopen("c:\\file3","r");
    f[0]=fopen("c:\\cfile1","w");//condidate files
    f[1]=fopen("c:\\cfile2","w");
    f[2]=fopen("c:\\cfile3","w");
    f[3]=fopen("c:\\cfile4","w");
    f[4]=fopen("c:\\cfile5","w");
    f[5]=fopen("c:\\cfile6","w");
    f[6]=fopen("c:\\cfile7","w");
    f[7]=fopen("c:\\cfile8","w");
    f[8]=fopen("c:\\cfile9","w");
    f[9]=fopen("c:\\cfile10","w");
     
    fclose(c[0]);
    fclose(c[1]);
    fclose(c[2]);
    fclose(f[0]);
    fclose(f[1]);
    fclose(f[2]);
    fclose(f[3]);
    fclose(f[4]);
    fclose(f[5]);
    fclose(f[6]);
    fclose(f[7]);
    fclose(f[8]);
    fclose(f[9]);
     
    report(c, f, name);
    	return 0;
    }
    void  report(FILE *college[3], FILE *arr[10], char* canceled){
    	FILE *cancel;
    	vote currtable[3], // array for three current "college" records
    		votable[3],    // array for checking the records with the same "id"
    		current;	   // struct for the current "college" record	
    	int i,j,idmin,index,flag,eof[3];
    	if(!(cancel=fopen("c:\\canceled","w"))) exit(1);
    	
    	for(i=0;i<3;i++)
    		eof[i]= readFrom(college[i],currtable,i);
    // main loop	
    	while(eof[0]!=EOF||eof[1]!=EOF||eof[2]!=EOF){
    		idmin=getMin(eof,currtable,&index);
    		for(i=flag=0;i<3;i++)
    			if(i!=index && eof[i]!=EOF && currtable[i].id==idmin)
    				flag=3;
    		if(flag){
    		fprintf(cancel,"%s%d%d\n",currtable[index].name,idmin,flag);
    			for(i=0;i<3;i++)
    				while(eof[i]!=EOF && currtable[i].id==idmin)
    				eof[i]=readFrom(college[i],currtable,i);
    			continue;
    		}
    //Filling struct table for the minimal id
    		votable[0]=currtable[index];
    		i=1;
    		while(eof[index]!=EOF && i<3){
    			eof[index]=fscanf(college[index],"%30s%9d%1d%1d%*c",
    current.name,&current.id,&current.type,&current.candidate);
    		if(current.id!=idmin){
    			for(j=0;j<i;j++)
    				fprintf(arr[votable[j].candidate],"%9d%d%d\n",
    idmin,index,votable[j].type);
    		currtable[index]=current;
    		break;
    		}
    		else{
    			flag=votecheck(current,votable,i);
    			if(flag){				fprintf(cancel,"%s%d%d\n",currtable[index].name,idmin,flag);
    			while(eof[index]!=EOF && currtable[index].id==idmin)
    			eof[index]=readFrom(college[index],currtable,index);
    			break;
    			}
    			else
    				votable[i++]=current;
    		}//else
    	}//while
    	if(i<3)  continue;
    		if(eof[index]!=EOF)
    		eof[index]=fscanf(college[index],"%30[^\n]%9d%1d%1d%*c",
    current.name,&current.id,&current.type,&current.candidate);
    		if(eof[index]==EOF ||current.id!=idmin ){
    			for(j=0;j<i;j++)
    				fprintf(arr[votable[j].candidate],"%9d%d%d\n",
    idmin,index,votable[j].type);		
    			currtable[index]=current;
    		}
    		else{			fprintf(cancel,"%s%d%d\n",votable[0].name,idmin,2);
    	while(eof[index]!=EOF && currtable[index].id==idmin)
    		eof[index]=readFrom(college[index],currtable,index);
    		}
    	}// main loop - while
    	fclose(cancel);
    }// report
    
    int votecheck(vote curr,vote arr[],int i){
    	int j,count;
    	for(j=count=0;j<i;j++){
    		if(curr.candidate==arr[j].candidate)
    			return 1;
    		if(curr.type==1 && arr[j].type==1)
    			return 2;
    		if(curr.type==2 && arr[j].type==2)
    			count++;
    		if(count>1)	return 2;
    	}
    	return 0;
    }
    
    int readFrom(FILE *coll,vote table[],int i){
    	return fscanf(coll,"%30[^\n]%9d%1d%1d%*c",table[i].name,
    				&table[i].id,&table[i].type,&table[i].candidate);
    }	
    
    int getMin(int eof[],vote table[],int *index){
    	int i,idmin;
    	for(i=0,idmin=999999999;i<3;i++)
    		if(eof[i]!=EOF && table[i].id<idmin){
    				idmin=table[i].id;
    				*index=i;
    		}
    	return idmin;			
    }

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    You open 10 files, then close all 10 of them, then proceed to try and process reports on the data in files you've closed...
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks i progressed from there
    why i get null passed in to the function
    why the "c" file array gets NULL
    because of it i still get the same bug
    i got these files:
    file1:
    donald 666666666 1 3
    john 777777777 1 0
    alex 888888888 1 1
    jdoe 999999999 1 2
    file2:
    daniel 000001111 1 3
    green 000003333 1 4
    alex 888888888 1 1
    jdoe 999999999 1 2
    file3:
    daniel 000001111 1 3
    tom 000002222 1 0
    alex 888888888 1 1
    jdoe 999999999 1 2

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    
     
    typedef struct vote {
    	char name [30];
    	int id,type,candidate;
    } vote;
    void report(FILE* college[3], FILE* arr[10], char* canceled);
    // Fuction returns the minimal id
    int getMin(int eof[],vote [],int *);
    // Function checks a vote record correct
    int votecheck(vote ,vote [],int i);
    // Function reads the next record from "college" file
    int readFrom(FILE *,vote [],int);
    
    
    
    int main ()
    {
    FILE* f[10];
    FILE* c[3];
    char  name[3]="canceled";
    
    c[0]=fopen("c:\\file1","r");//vote files ,each file represents the votes from a college
    c[1]=fopen("c:\\file2","r");
    c[3]=fopen("c:\\file3","r");
    f[0]=fopen("c:\\cfile1","w");//condidate files
    f[1]=fopen("c:\\cfile2","w");
    f[2]=fopen("c:\\cfile3","w");
    f[3]=fopen("c:\\cfile4","w");
    f[4]=fopen("c:\\cfile5","w");
    f[5]=fopen("c:\\cfile6","w");
    f[6]=fopen("c:\\cfile7","w");
    f[7]=fopen("c:\\cfile8","w");
    f[8]=fopen("c:\\cfile9","w");
    f[9]=fopen("c:\\cfile10","w");
    
    report(c, f, name);
    
    fclose(c[0]);
    fclose(c[1]);
    fclose(c[2]);
    fclose(f[0]);
    fclose(f[1]);
    fclose(f[2]);
    fclose(f[3]);
    fclose(f[4]);
    fclose(f[5]);
    fclose(f[6]);
    fclose(f[7]);
    fclose(f[8]);
    fclose(f[9]);
     
    
    	return 0;
    }
    void  report(FILE *college[3], FILE *arr[10], char* canceled){
    	FILE *cancel;
    	vote currtable[3], // array for three current "college" records
    		votable[3],    // array for checking the records with the same "id"
    		current;	   // struct for the current "college" record	
    	int i,j,idmin,index,flag,eof[3];
    	if(!(cancel=fopen("c:\\canceled","w"))) exit(1);
    	
    	for(i=0;i<3;i++)
    		eof[i]= readFrom(college[i],currtable,i);
    // main loop	
    	while(eof[0]!=EOF||eof[1]!=EOF||eof[2]!=EOF){
    		idmin=getMin(eof,currtable,&index);
    		for(i=flag=0;i<3;i++)
    			if(i!=index && eof[i]!=EOF && currtable[i].id==idmin)
    				flag=3;
    		if(flag){
    		fprintf(cancel,"%s%d%d\n",currtable[index].name,idmin,flag);
    			for(i=0;i<3;i++)
    				while(eof[i]!=EOF && currtable[i].id==idmin)
    				eof[i]=readFrom(college[i],currtable,i);
    			continue;
    		}
    //Filling struct table for the minimal id
    		votable[0]=currtable[index];
    		i=1;
    		while(eof[index]!=EOF && i<3){
    			eof[index]=fscanf(college[index],"%30s%9d%1d%1d%*c",
    current.name,&current.id,&current.type,&current.candidate);
    		if(current.id!=idmin){
    			for(j=0;j<i;j++)
    				fprintf(arr[votable[j].candidate],"%9d%d%d\n",
    idmin,index,votable[j].type);
    		currtable[index]=current;
    		break;
    		}
    		else{
    			flag=votecheck(current,votable,i);
    			if(flag){				fprintf(cancel,"%s%d%d\n",currtable[index].name,idmin,flag);
    			while(eof[index]!=EOF && currtable[index].id==idmin)
    			eof[index]=readFrom(college[index],currtable,index);
    			break;
    			}
    			else
    				votable[i++]=current;
    		}//else
    	}//while
    	if(i<3)  continue;
    		if(eof[index]!=EOF)
    		eof[index]=fscanf(college[index],"%30[^\n]%9d%1d%1d%*c",
    current.name,&current.id,&current.type,&current.candidate);
    		if(eof[index]==EOF ||current.id!=idmin ){
    			for(j=0;j<i;j++)
    				fprintf(arr[votable[j].candidate],"%9d%d%d\n",
    idmin,index,votable[j].type);		
    			currtable[index]=current;
    		}
    		else{			fprintf(cancel,"%s%d%d\n",votable[0].name,idmin,2);
    	while(eof[index]!=EOF && currtable[index].id==idmin)
    		eof[index]=readFrom(college[index],currtable,index);
    		}
    	}// main loop - while
    	fclose(cancel);
    }// report
    
    int votecheck(vote curr,vote arr[],int i){
    	int j,count;
    	for(j=count=0;j<i;j++){
    		if(curr.candidate==arr[j].candidate)
    			return 1;
    		if(curr.type==1 && arr[j].type==1)
    			return 2;
    		if(curr.type==2 && arr[j].type==2)
    			count++;
    		if(count>1)	return 2;
    	}
    	return 0;
    }
    
    int readFrom(FILE *coll,vote table[],int i){
    	return fscanf(coll,"%30s%9d%1d%1d%*c",table[i].name,
    				&table[i].id,&table[i].type,&table[i].candidate);
    }	
    
    int getMin(int eof[],vote table[],int *index){
    	int i,idmin;
    	for(i=0,idmin=999999999;i<3;i++)
    		if(eof[i]!=EOF && table[i].id<idmin){
    				idmin=table[i].id;
    				*index=i;
    		}
    	return idmin;			
    }

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Please search first:
    fopen - C++ Reference

    Read the part about opening files with "r" mode.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM