Newbish Question file reading question....

This is a discussion on Newbish Question file reading question.... within the C Programming forums, part of the General Programming Boards category; Is the file opened correctly? You might test by printing some text to the file just before the loop....

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Is the file opened correctly? You might test by printing some text to the file just before the loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    May 2002
    Posts
    208
    I thought a lot of file errors would be immediatly obvious anyway considering this code was just for me. Which is why I left out all the file error checking. I'll put it in and see what happens, but I know the file is being created because it is there afterwards.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  3. #18
    Registered User
    Join Date
    May 2002
    Posts
    208
    Code:
    fptr2=fopen(outfile,"w");
    		if (fptr2 != NULL){
    			for(int k=0;k<count;k++){
    		
    				fprintf(fptr2,"&#37;d\n",arrayPtr[k]);
    				printf("%d ",arrayPtr[k]);
    			}
    		}
    		else{perror("oops");}
    this was all I changed but now I get a bus error long before this segment of code gets executed.....

    This program is getting the best of me.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    Well that sounds like you have other problems to contend with.

    The first step would be to check that all pointers are pointing at valid memory. Setting them all to NULL when you declare them (unless you explicitly initialise them to something else) will gain some consistency when it comes to crashing at least.

    If it's only a couple of hundred lines long, then posting it might be an option, but if it's already in the 1000's of lines then it probably isn't.

    Which OS/Compiler are you using?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Dev-C++ would be my guess: Dev C++
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    Registered User
    Join Date
    May 2002
    Posts
    208
    Actually I am using XCODE and g++ now. XCODE just as an editor though. The code is by no means long, I have posted it below.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<unistd.h>
    
    #define NUM_PARTICLES 4000
    
    void givePropPercentages(int num_prop, char *name);
    
    int main(){
    	
    	int NUM_PROP_RUNS;
    	char filename[100],title[20], outfile[12];
    	FILE *fptr,*fptr2;
    	int count=0, j=0;
    	int *arrayPtr;
    	float tmp;
    
    	printf("\n Number Of Propensity Runs: ");
    	scanf("%d",&NUM_PROP_RUNS);
        printf("\n Name of NQ6_AVG.cpp Output File (12 chars max): ");
    	scanf("%s",title);
    	printf("\n Enter A Name for the Output File (12 chars max): ");
    	scanf("%s", outfile);
    	
    	for(int i=1;i<=NUM_PROP_RUNS;i++){
    		
    		sprintf(filename,"%d/%s",1000+i,title);
    		printf("Opening %s",filename);
    		fptr = fopen(filename,"r");
    		
    		/*
    			This loop figures out how many of our particles actually 
    			have crystalline like behavior on average over the whole
    			run. This is determined by the condition that the NQ6 value
    			be greater then 8.
    		*/
    		
    		for (int k=0;k<NUM_PARTICLES;k++){
    		
    			fscanf(fptr, "%f",&tmp);
    			
    			if(tmp>8.0){count++;}
    		
    		}
    		
    		/*
    			Now we malloc ourselves a array of the correct size to
    			store the PARTICLE NUMBERS of those which we dertermined
    			to be in fact crystalline.
    		*/
    		printf("\n\n Count: %d\n\n",count);
    		arrayPtr = new int [count];
     
    		if(arrayPtr == NULL) { 
    			printf("error: not enough memory"); 
    			return 1;
    		}
    		
    		rewind(fptr);
    		
    		for (int k=0;k<NUM_PARTICLES;k++){
    		
    			fscanf(fptr, "%f",&tmp);
    			
    			if(tmp>8.0){
    				arrayPtr[j]=k;
    				j++;
    			}
    		
    		}
    		
    		fclose(fptr);
    		
    		fptr2=fopen(outfile,"w");
    		if (fptr2 != NULL){
    			for(int k=0;k<count;k++){
    		
    				fprintf(fptr2,"%d\n",arrayPtr[k]);
    				printf("%d ",arrayPtr[k]);
    			}
    		}
    		else{perror("oops");}
    		
    		fclose(fptr2);
    	}
    		
    	givePropPercentages(NUM_PROP_RUNS, outfile);	
    	return 0;
    }
    
    void givePropPercentages(int num_prop, char *name){
    
    	FILE *fileptr;
    	float arry[NUM_PARTICLES];
    	char dirname[5], output[12];
    	float tmp2;
    	
    	for (int i = 0; i<NUM_PARTICLES; i++){arry[i]=0;}
    	
    	    for(int i=1;i<=num_prop; i++){
        	char fn[100];    
            sprintf(fn,"%d/%s",1000+i,name);
            fileptr = fopen(fn, "r");
        	
    		if ( fileptr != NULL ) {
                
    			while (fscanf(fileptr,"%d", &tmp2) != EOF) {
                    int pos = tmp2;
    			    if (pos >= 0 && pos < NUM_PARTICLES) {
                        arry[pos]++;
                    } 
    				else {fprintf(stderr,"Out of range %d\n", pos );}
                }
                fclose(fileptr); // close the file
        	} else {perror("oops");}	
        }
    	
    	printf("\n\n Name the Prop. Percentage output file (12 chars max): ");
    	scanf("%s", output);
    	
    	fileptr = fopen(output,"w");
    	
    	for (int i = 0; i<NUM_PARTICLES; i++){
    		
    		arry[i] = arry[i]/num_prop;
    		//printf("%d\n", arry[i]);
    		fprintf(fileptr,"%f\n", arry[i]);
    	
    	}
    	
    	fclose(fileptr);
    }
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    > arrayPtr[j]=k;
    One thing you don't do is reset j back to 0 from one file to the next.

    for ( int k=0, j = 0 ; k < NUM_PARTICLES && j < count ; k++ )
    Will ensure that the respective loop variables are initialised each time, and also make sure that you won't overstep the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #23
    Registered User
    Join Date
    May 2002
    Posts
    208
    ok, I do initalize j with the first variables but I did what you said and it did get ride of the bus error.

    but there has to be a problem after fptr2 because as you can see I print out arry[] and it gives me the correct index numbers when I compare it by hand. However, outfile is still empty....
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  9. #24
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    > fptr2=fopen(outfile,"w");
    Inside main, you always do "w" for every input file you process, so all the results except for the last run are just lost.

    > givePropPercentages(NUM_PROP_RUNS, outfile);
    You then pass the name onto this function, which treats the old outfile as the new input file.

    > sprintf(fn, "&#37;d/%s", 1000 + i, name);
    But inside the function, the old output (now the new input) is apparently multitudinous, and in a sub directory.


    > arrayPtr = new int [count];
    new is C++, not C.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  3. question about reading in strings from a file :>
    By bball887 in forum C Programming
    Replies: 8
    Last Post: 04-13-2004, 07:24 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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