#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    VA
    Posts
    4
    Rep Power
    0

    C code for read a set of scores from a text file


    Hi,

    I am just starting to learn C. I need to write a short program to READ a set of scores from a text file, COUNT the scores over 80, COPY the scores over 80 to a new file, and PRINT the number of scores over 80 to the monitor.

    Can anyone provide me with the code for this?

    Please provide the code for C.

    Thanks in advance,
    geebee
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    What is the format of the input file to read and how should the output file be formatted?
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    VA
    Posts
    4
    Rep Power
    0
    It is reading fro a Text file, and outputting in a choice you like.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Boss, I know it's reading from a text file. What I was asking was what the text file looks like. Does it just contain just the scores, each one on a separate line or what? Assuming that is true, here's some code that should work:
    Code:
    #include <stdio.h>
    int main(void) {
      FILE *in, *out;
      int x;
    
      if ((in = fopen("input.txt", "r")) == NULL) {
        fprintf(stderr, "Could not open input.txt for reading.\n");
        return 1;
      }
    
      if ((out = fopen("output.txt", "w")) == NULL) {
        fprintf(stderr, "Could not open output.txt for writing.\n");
        fclose(in);
        return 2;
      }
    
      while (!feof(in)) {
        fscanf(in, "%d", &x); /* read a number */
        if (!feof(in) && x > 80)
          fprintf(out, "%d\n", x);
      }
    
      fclose(in);
      fclose(out);
    
      return 0;
    }
    Remember, you owe me an A from this class.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo