Thread: Reading from a file

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    29

    Reading from a file

    Im trying to read values from a .txt file, the first line of which tells you how many other lines there are and how many values are in each line, for example if the first line reads
    4 5 1
    That means that there are 4 lines of text after the first and each of these lines contains 5 values. the 1 tells you whether to include or exclude the min and max values (0 - include 1- exclude). so the whole file for this example would be

    4 5 1
    23 12 95 23 56
    45 43 74 94 12
    12 65 55 82 34
    07 12 76 53 23


    With the min and max excluded from each line.
    I then have to add up the averages for each line and then print the highest value to screen and show which line led to that value. this is the code I have written so far (I added the printfs to make sure i was scanning correctly)

    Code:
    int contestants, audience, judges, i, score;
        
        FILE *ifp;
        
        ifp = fopen("filename.txt", "r");
        
        fscanf (ifp, "%d%d%d", &contestants, &audience, &judges);
        printf("%d\n%d\n%d", contestants, audience, judges);
        printf("\n");
        
        
        if (j == 1) {
        
            for (i = 0; i<=contestants; i++)
            
        fscanf(ifp, "%d", &score);
        printf("%d\n", score);
        
    }
    I'm kind of stuck at this point and im not sure im going about solving it the correct way, any help is appreciated thanks!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The parameters are one problem - they're just copies of variables, not the real thing. They will "die" when the function goes out of scope, and I doubt you want that.

    So, two ways to go:

    1) bring in a pointer with the address of those variables, so the variable won't go out of scope when the function ends,

    or

    2) use the local variables to do all the calculations you need, before that one function, is done.

    Where do you want to do the averaging, etc.?

    As far as getting data from the file:
    You want to fscanf() the number of rows, and the number of columns in each row, and then use that in your for loop, to get the rest of the data:

    fscanf() //get your first three data items: row, column and exclude (0 - do not, 1 do)

    then something like this: (pseudo pseudo code)
    Code:
    total = 0;
    for(i = 0; i < row * col; i++) {
      fscanf() a value
      do your calculations with that value, including
      total += that value;
    }
    
    average = total /(row * col);
    etc.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    Ok i have gotten farther but I'm still not getting it all, here is what I have now

    Code:
    int contestants, audience, judges, i, score, row, col, average, total;
        
        FILE *ifp;
        
        ifp = fopen("filename.txt", "r");
        
        fscanf (ifp, "%d%d%d", &contestants, &audience, &judges);
        printf("%d\n%d\n%d", contestants, audience, judges);
        printf("\n");
        
        row = contestants;
        col = audience;
        
        if (judges == 0) {
            total = 0;
                
                for(i = 0; i < row * col; i++) {
                    fscanf(ifp, "%d", &score);
                    
                    total += score;
    }
    
             average = total /(row * col);
    
    
    }
    I guess the main thing I don't understand is how to assign each value in each row to a variable, or maybe I don't even have to do that, yea I'm lost haha

  4. #4
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    For loop should probably go from 0 to contestants*audience-1.
    Make an integer array of size contestants*audience.
    In the for loop, scan your values into the array with element i.

    If you want, you can scan all of the data into the array, close the file (fclose), and then work with the array. Or you can scan as you go.

    If you use this single-array approach, you will need to determine when to calculate the average. There are two main ways of doing this: Using an if statement with a modulo. If the iterator modulo the number of people in the audience equal zero, calculate the average. Or you could perhaps have a nested for loop, one that traverses from i and skips to i+audience, and inside of that, one that traverses the values between i and i+audience.

    Of course there are a lot of approaches to this, and I'm not going to go in-depth into working with the averages when your current code still needs a lot of work to get where it needs to be.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Since you apparently have not been introduced to an array, let's just work thru this, without it.
    Code:
       int r, c, hiRow, average, total, rows, cols, exclude, score, hiscore;
       FILE *ifp;
        
        ifp = fopen("filename.txt", "r");
        if(ifp==NULL) {
           printf("\nError opening filename.txt file");
           return 1;
        }
        fscanf (ifp, "%d%d%d", &rows, &cols, &exclude);
        printf("Rows: %d   Columns: %d  Exclude" %d\n", rows, cols, exclude);
        for(r=0;r<rows;r++) {
          for(c=0;c<cols;c++) {
            fscanf(... , "%d", &score);
            //add your exclude logic code, in here
            if(score is to be included) then {
                total += score;
                if(r==0 && c==0) {
                   hiscore = score;
                   hirow = 0;
                }
                else {
                   if(score > hiscore) {
                      hiscore = score;   
                      hirow = r;
                   }
                } 
    
    
      average = total /(row * col);
    }
    You have to be careful to do everything you need, before you loop back around for another input from the file. There's no "going back to the scores", in this kind of logic.

    The above is not runnable code, as is. It's meant to give you good idea's, if you want to use this kind of logic, but it is not meant to be a runnable program, as is.

    If you have trouble understanding the logic, try doing this exercise SLOWLY, by hand on paper, and see if it makes more sense, as you become more familiar with the problem.

  6. #6
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    You also have to take into account that you said that the mins and maxes were already removed from the file (I think you may be incorrect -- you're suppose to do it yourself). You're going to need two separate cases: one for when you calculate the plain ol' average, and one for when you have to remove the min and the max score for each contestant, then calculate the average of that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM