Thread: Newbish Question file reading question....

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    Newbish Question file reading question....

    I have a bunch of files of variable length from which I would like to read data values. Since I don't know how long any specific file is going to be I figured I should use a while loop to read from the files until EOF is reached. I tried this, which did not work so is obviously wrong.

    Code:
    		while(tmp != 'EOF'){
    			
    			tmp = fscanf(fileptr,"%d", &tmp2);
    			
    			arry[tmp2]++;
    		}
    Anyone know where I am going wrong? Help would be greatly appreciated.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Where you are going wrong ? You're using C functions in C++. That's "wrong".
    Code:
    std::stringstream ss;
    std::ifstream file("myfile.txt");
    
    if(file.is_open())
    {
      while(!file.eof())
      {
        std::getline(file, ss, '\n');
        int tmp = 0;
    
        while(ss >> tmp)
        {
          arry[tmp]++; // supposing your code is correct here...
        }
      }
    }
    Haven't tried / compiled this code though.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    208
    hmmm I just realized then, that I posted in the wrong forum. My entire code is written in C.

    Sorry, if an admin could move this though would be seller, otherwise I will just delete it and repost.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    using eof to control loop is also not too healthy
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to C programming.

    I think you are probably looking for something along the lines of:
    Code:
    while (fscanf(fileptr,"%d", &tmp2) != EOF) {
        arry[tmp2]++;
    }
    Oh, and read the FAQ on Why it's bad to use feof() to control a loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    208
    So, I used your example but it is not quite working. The while loop never gets executed. Even though I know the file exists and has data in it.

    Should the EOF be in single quotes? Double Quotes?
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    208
    Well I tried changing that and I was wrong..... for some reason this is not working

    Code:
    		while (fscanf(fileptr,"%f", &tmp2) != EOF) {
    			printf("\n\nIncrementing Particle %d", tmp2);
    			arry[(int)tmp2]=arry[(int)tmp2]+1;
    			
    		}
    I put that printf there as a test. It never gets executed dispite the fact that the file fileptr points to has 4000 lines each with a floating point value on it. This strikes me as odd because EOF shouldn't be reached when it is.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Should the EOF be in single quotes? Double Quotes?
    It is an identifier, not a string, so no quotes.

    I put that printf there as a test. It never gets executed dispite the fact that the file fileptr points to has 4000 lines each with a floating point value on it. This strikes me as odd because EOF shouldn't be reached when it is.
    One thing you can try is to post the smallest and simplest compilable code that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    208
    Ok, well here is the function it is occuring in. I think since the function is essentially self contained it should illustrate what I am trying to do nicely.

    As per an explaination. My data is organized into a bunch (upto 500) folders all in one Master folder. This program runs in the master folder and enters each sub folder, opens a file in there. The file contains the id numbers of particles in a system which we have determined to be "crystalline" in our simulation. Essentially I want the array arry, to contain a count of how many times each particle is infact crystalline.

    Code:
    void givePropPercentages(int num_prop, char *name){
    
    	FILE *fileptr;
    	float arry[NUM_PARTICLES];
    	char dirname[5], output[12];
    	float tmp2;
    	
    // initalize the array to zero
    	for (int i = 0; i<NUM_PARTICLES; i++){arry[i]=0;}
    
    // now we loop over all folders	
    	for(int i=1;i<=num_prop; i++){
    		
    		sprintf(dirname,"%d",1000+i); // this is just a numbering standard
                                                                 //  the folders start at 1001
    		chdir(dirname);
    		
    		fileptr = fopen(name, "r"); // open the file with the constant name
                                                           // since all folders have one file of this name in them
    	/*the loop which is giving me issues */
    		while (fscanf(fileptr,"%f", &tmp2) != EOF) {
    			printf("\n\nIncrementing Particle %d", tmp2);
    			arry[(int)tmp2]=arry[(int)tmp2]+1;
    			
    		}
    		
    		fclose(fileptr); // close the file
    		chdir("../");    // navigate back out to master folder
    	}
    	
    	
    /* this is where we actually do somethign with the data
         we create a new file which will hold the percentage of
         the total number of runs in which each particle is in fact crystalline
    
     */
           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("%f\n", arry[i]);
    		fprintf(fileptr,"%f\n", arry[i]);
    	
    	}
    
    }
    /*end function*/
    This all compiles nicely, you can just put a function call in main(), and assuming you have the correct file tree set up everything runs nicely except my final output file, the one which is supposed to hold the percentages just contains all zeros.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
           printf("\n\n Name the Prop. Percentage output file (12 chars max): ");
    	scanf("&#37;s", output);
    Rather than doing that, consider using fgets(). This would also help if you had a filename with a space in it, for example. Your current code would not handle that very well.

    Code:
    fileptr = fopen(output,"w");
    You never close this file. It's best to close files when you're done with them. They are automatically closed with most modern operating systems when your program exits, but you have problems if you try to open the file again, or the file is modified by another program, etc. Just close it when you're done. It's just one line.
    Code:
    fclose(fileptr);
    Also, an observation:
    Code:
    	for(int i=1;i<=num_prop; i++){
    		
    		sprintf(dirname,"%d",1000+i); // this is just a numbering standard
                                                                 //  the folders start at 1001
    		chdir(dirname);
    		
    		fileptr = fopen(name, "r"); // open the file with the constant name
                                                           // since all folders have one file of this name in them
    	/*the loop which is giving me issues */
    		while (fscanf(fileptr,"%f", &tmp2) != EOF) {
    			printf("\n\nIncrementing Particle %d", tmp2);
    			arry[(int)tmp2]=arry[(int)tmp2]+1;
    			
    		}
    		
    		fclose(fileptr); // close the file
    		chdir("../");    // navigate back out to master folder
    	}
    You're calling chdir() twice. It might be more efficient if the first chdir was "1001", succeeding ones were "../XXXX", and the last chdir was "..". Your code would execute the same way but would call chdir() less often.

    Also, why do your folder numbers start at 1001 and not 0001? If you were having problems adding the leading zeros with sprintf(), use a flag like this:
    Code:
    printf("%4d", 1);  /* prints "0001" */
    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.

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    208
    The 1001 standard has nothing to do with this code, and more to do with our simulation codes which are written in FORTRAN77 and my professor's old standards.

    As for the efficency comments I will keep those in mind.

    The fclose should be there, I could have sworn it was, but even with it is does not quite change the fact that my arry[] is not being filled properly in that while loop.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. There is no check for failing to open the file.

    2. You can avoid all the chdir stuff entirely, by creating a filename with a directory name prefixed to it.

    Code:
        for(int i=1;i<=num_prop; i++){
        	char fn[100];    
            sprintf(fn,"&#37;d/%s",1000+i,name);
            fileptr = fopen(fn, "r");
        	if ( fileptr != NULL ) {
                while (fscanf(fileptr,"%f", &tmp2) != EOF) {
                    int pos = tmp2;
                    if ( pos >= 0 && pos < PARTICLES ) {
                        arry[pos]++;
                    } else {
                        fprintf(stderr,"Out of range %d\n", pos );
                    }
                }
                fclose(fileptr); // close the file
        	} else {
        		perror("oops");
        	}	
        }
    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.

  13. #13
    Registered User
    Join Date
    May 2002
    Posts
    208
    Touch&#233;, you caught me in my bad coding.......

    I have taken your advice though and I am still just getting a final output of all zeros. The arry is still not incrementing and I am not getting any errors from the files or anything. This makes no sense to me, it must be something small but I cannot find it.

    Everything is working correctly but arry is not incrementing.

    Here is the test file I am opening in this function. So Clearly the integers are there, so I do not understand why arry is not incrementing....
    Code:
    283
    551
    567
    599
    792
    912
    1086
    1520
    1558
    1631
    1947
    2323
    2324
    2661
    2683
    2777
    2820
    2854
    2998
    3170
    3172
    3452
    3546
    3801
    3903
    3921
    3978
    Last edited by kas2002; 05-16-2007 at 08:31 AM.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  14. #14
    Registered User
    Join Date
    May 2002
    Posts
    208

    Angry

    ok, I just maybe found where the bug is. That function above takes the argument name.

    Now apparently (I realize this is something that I should have caught before :S) the file I am passing to the function is empty. It should not be because I had developed this part a long time ago and it was working. This is the part of the code that is not working.

    Code:
    		fptr2=fopen(outfile,"w");
    		
    		for(int k=0;k<count;k++){
    		
    			fprintf(fptr2,"%d\n",arrayPtr[k]);
    			printf("%d ",arrayPtr[k]);
    		}
    		fclose(fptr2);
    	}
    		
    	givePropPercentages(NUM_PROP_RUNS, outfile);
    Now I know for a fact that arrayPtr has values in it because of the print funtion below fprintf prints what is expected to the screen. However nothing is printed to the file. Why? I don't know, but I think fixing this fixes everything, since the earlier function I posted is working properly just being fed the wrong data.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Again, no check that the file was opened successfully.
    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.

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, 03:39 AM
  3. question about reading in strings from a file :>
    By bball887 in forum C Programming
    Replies: 8
    Last Post: 04-13-2004, 06:24 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM