Is the file opened correctly? You might test by printing some text to the file just before the loop.
Is the file opened correctly? You might test by printing some text to the file just before the loop.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
this was all I changed but now I get a bus error long before this segment of code gets executed.....Code: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");}
This program is getting the best of me.
Jeff Paddon
Undergraduate Research Assistant
Physics Department
St. Francis Xavier University
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.
Dev-C++ would be my guess: http://cboard.cprogramming.com/showthread.php?t=19989
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.
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
> 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.
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
> 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, "%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.