Thread: Read Strings From Text File

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    2

    Unhappy Read Strings From Text File

    Hey eveyone. A file, named “grades.txt”, contains student grades for a course in the followingformat separated by spaces: (an example file follows)
    First name Last name Midterm Final
    Ali Caliskan 60 40
    Veli Dalgaci 80 10
    Turkan Sevimli 90 50
    Ali Yilmaz 30 70
    Ahmet Koc 50 50

    Write a program which calculates the overall grades of the students and writes them to two separate files: “passed.txt” and “failed.txt”. The midterm is 40 percent of the grade, and the final is 60%. The passing grade is 50. Example output files follow:
    First name Last name Midterm Final Overall
    passed.txt:
    Turkan Sevimli 90 50 66
    Ali Yilmaz 30 70 54
    Ahmet Koc 50 50 50
    failed.txt:
    Ali Caliskan 60 40 48
    Veli Dalgaci 80 10 38
    I can read "grades.txt", but i can not separate numbers.. This is my code;
    Code:
    
    
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void) {
    int i=0;
    char* string[100];
    char line[100];
    char junk[100];
    
    
    FILE *file = fopen("grades.txt", "r");
    if(!file) {
        printf("Could not open file. Exiting application. Bye");
        return 1;
    }
    while(!feof(file)) {
        fscanf(file,"%[^ \n\t\r]s",line); //Get text
        printf("%s\n", line);
    
    
        fscanf(file,"%[ \n\t\r]s",junk); //Remove any 'white space' characters
    }
    fclose(file);
    }
    I separated (Turkan) (Sevimli) (90) (50), but how can i elect numbers and add them?
    if a student who has higher grade than 50, program should send it to passed.txt, else failed.txt.
    Last edited by Batuhan Edgüer; 01-01-2017 at 05:02 PM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    To read numbers, you probably want to use the "%d" format specifier of scanf. To skip whitespace, you can prepend a space to the format string (you don't need the 'junk' variable). If you are using the square bracket format %[...], don't put 's' after the square brackets. e.g.

    Code:
    fscanf(file, " %[^ \n\t\r]", line); // Skip white space and then read a non-whitespace token into 'line'.
    Note that 'line' is a misleading name here. That code does not read a whole line, but rather a 'name' or 'token'.

    To complete the assignment you will need additional code to read the numbers. You will probably want two additional FILE pointers for the 'passed' and the 'failed' text files (open those for writing). Then you need some logic to determine whether each student which you read should be recorded in the 'passed' or the 'failed' file.

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    2
    Quote Originally Posted by c99tutorial View Post
    To read numbers, you probably want to use the "%d" format specifier of scanf. To skip whitespace, you can prepend a space to the format string (you don't need the 'junk' variable). If you are using the square bracket format %[...], don't put 's' after the square brackets. e.g.

    Code:
    fscanf(file, " %[^ \n\t\r]", line); // Skip white space and then read a non-whitespace token into 'line'.
    Note that 'line' is a misleading name here. That code does not read a whole line, but rather a 'name' or 'token'.

    To complete the assignment you will need additional code to read the numbers. You will probably want two additional FILE pointers for the 'passed' and the 'failed' text files (open those for writing). Then you need some logic to determine whether each student which you read should be recorded in the 'passed' or the 'failed' file.
    So how can do it? I'm newbie in "C"

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Batuhan Edgüer View Post
    So how can do it?
    Try to do it first and if you get stuck, post your question here. It may help to start by writing pseudocode of how you want to solve the problem. Another trick is to start by solving the simplest possible problem (i.e. an input file containing just one student). Once you've got it working with the simplest case, you can extend your program incrementally to handle larger input (using a loop, etc.), e.g.

    Code:
    open "grades.txt" for reading
    read a word into 'line'
    read the first number into 'x'
    read the second number into 'y'
    let z = x + y
    if z >= 50, then ...
    ...

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Batuhan Edgüer View Post
    So how can do it? I'm newbie in "C"
    In that case, you should be practising and learning the language, rather than copying code from other sources.

    Also, please don't cross-post.

    How To Ask Questions The Smart Way

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >while(!feof(file)) {

    Does not read the entire file correctly. Use

    Code:
    while (fgets(<string name> sizeof(<string>) fptr) != NULL)
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read a text file into an array of strings?
    By amitj93 in forum C Programming
    Replies: 6
    Last Post: 03-05-2013, 05:03 PM
  2. Read Strings From Text File Into Char Array
    By somniferium in forum C Programming
    Replies: 8
    Last Post: 09-13-2012, 12:41 AM
  3. Read Strings in from text file and store into array
    By RRTT in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2011, 04:52 AM
  4. Read max value from strings of numbers in text file
    By james890 in forum C++ Programming
    Replies: 14
    Last Post: 04-15-2010, 03:26 PM
  5. read certain strings from file
    By shatred in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2006, 02:48 PM

Tags for this Thread