Thread: Problem Editing A Text File In C

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    4

    Question Problem Editing A Text File In C

    Hay all
    i have a problem editing a text file in c

    this is code


    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    typedef struct
    {
        char *name;
        float gpa;
    }student;
    
    
    int main()
    {
        FILE *originalFile = fopen("C:\\Users\\jurs\\Desktop\\students.txt","r");
        FILE *newFile = fopen("C:\\Users\\jurs\\Desktop\\students_temp.txt","w");
    
    
        int studentCount;
        fscanf(originalFile, "%i", &studentCount);
    
    
        student *passingStudents = malloc(sizeof(student) * studentCount);
    
    
        int passingCount = 0;
        for (int i = 0; i < studentCount; i++)
        {
            int namelen;
    
    
            fscanf(originalFile, "%i", &nameLen);
    
    
            char *name = malloc(sizeof(char) * (nameLen + 1));
            name[nameLen] = '\0';
            float gpa;
    
    
            fscanf(originalFile, "%s %f", name, &gpa);
    
    
            if (gpa >=65.0)
            {
                passingStudents[passingCount].name = name;
                passingStudents[passingCount].gpa = gpa;
    
    
                passingCount++;
            }
        }
    
    
        fprintf(newFile, "%i\n",passingCount);
    
    
        for (int i = 0; i < passingCount; i++)
            fprintf(newFile, "%i %s %.2f\n", strlen(passingStudents[i].name, pasiingStudents[i].gpa));
    
    
    
    
    
    
        fclose(originalFile);
        fclose(newFile);
    
    
        remove("C:\\Users\\jurs\\Desktop\\students.txt");
        rename("C:\\Users\\jurs\\Desktop\\students_temp.txt" , "C:\\Users\\jurs\\Desktop\\students.txt");
    
    
        printf("berhasil menghapus semua failing Students.")
        return 0;
    }
    erorr in
    Code:
    for (int i = 0; i < studentCount; i++)

    please help me Master i'm newbie in C


    Post the exact error
    Code:
    C:\Users\jurs\Desktop\tugas.c||In function `main':|C:\Users\jurs\Desktop\tugas.c|22|error: 'for' loop initial declaration used outside C99 mode|
    C:\Users\jurs\Desktop\tugas.c|26|error: `nameLen' undeclared (first use in this function)|
    C:\Users\jurs\Desktop\tugas.c|26|error: (Each undeclared identifier is reported only once|
    C:\Users\jurs\Desktop\tugas.c|26|error: for each function it appears in.)|
    C:\Users\jurs\Desktop\tugas.c|45|error: redefinition of 'i'|
    C:\Users\jurs\Desktop\tugas.c|22|error: previous definition of 'i' was here|
    C:\Users\jurs\Desktop\tugas.c|45|error: 'for' loop initial declaration used outside C99 mode|
    C:\Users\jurs\Desktop\tugas.c|46|error: `pasiingStudents' undeclared (first use in this function)|
    C:\Users\jurs\Desktop\tugas.c|46|error: too many arguments to function `strlen'|
    C:\Users\jurs\Desktop\tugas.c|57|error: syntax error before "return"|
    ||=== Build finished: 10 errors, 0 warnings (0 minutes, 1 seconds) ===|

  2. #2
    Registered User Uli's Avatar
    Join Date
    Dec 2013
    Posts
    12
    I think it is better if you initialize your variable name and type very first (before other statement). And you have to put your int i on the top (not in for stucture).

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Your first error means you have to enable C99 standard

    Code:
    C:\Users\jurs\Desktop\tugas.c||In function `main':|C:\Users\jurs\Desktop\tugas.c|22|error: 'for' loop initial declaration used outside C99 mode
    It depends on the compiler on how to do this. With gcc you add "-std=gnu99" or "-std=c99" to the compiler flags.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You are leaking memory. If the current student you are looking at does not have a passing grade, you skip assigning it to a slot in your passingStudents array. When this happens, you do nothing with the memory allocated for name and you lose track of it when moving on to processing the next student. You should be calling free on the name field whenever the student does not have a passing grade and at the end of your program calling free on both the name field for all locations within the passingStudents array with a valid student followed by a final call to free on the array itself.

    Code:
    int passingCount = 0;
    for (int i = 0; i < studentCount; i++)
    {
        ... 
        
        char *name = malloc(sizeof(char) * (nameLen + 1));
        name[nameLen] = '\0';
        
        ...
     
        if (gpa >=65.0)
        {
            ...
        }
        else
        {
            free(name);
        }
    }
    
    ...
    
    
    // At end loop through the passingStudents array and free the name member
    for(int i = 0; i < passingCount; ++i )
    {
        free(passingStudents[i].name);
    }
    // Then after that free the array itself
    free(passingStudents);



    Then you have this:
    Code:
    for (int i = 0; i < passingCount; i++)
        fprintf(newFile, "%i %s %.2f\n", strlen(passingStudents[i].name, pasiingStudents[i].gpa));
    Your call to strlen is wrong here and it looks like there is only two arguments that you are actually passing to be printed when there should be 3: strlen(passingStudents[i].name), passingStudents[i].name and passingStudents[i].gpa (notice the misspelled pasiingStudents).
    Last edited by hk_mp5kpdw; 01-03-2014 at 06:51 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. text file editing
    By Stevie_dno3 in forum C Programming
    Replies: 1
    Last Post: 02-16-2013, 07:28 PM
  2. File editing problem.
    By opat in forum C++ Programming
    Replies: 7
    Last Post: 09-09-2011, 06:38 AM
  3. Editing a text file
    By bijan311 in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2010, 12:04 PM
  4. Editing a text file
    By rehan in forum C++ Programming
    Replies: 2
    Last Post: 06-24-2007, 09:58 AM
  5. Editing a text file
    By chimpanzee in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2006, 04:47 AM