Thread: updating a specific data in the file

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    40

    updating a specific data in the file

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    #define MAX 200
    
    
    
    
    void getinfoStudent();
    void prompt();
    void printstudentInfo();
    void addstudentInfo();
    void printpartial();
    int compare();
    void update();
    
    
    struct Student
    {
        char firstname[30];
        char lastname[30];
        int studentnumber;
        int markFinal;
        int markMidterm;
        int markAssignment[5];
        int markLab[5];
    
    
    }student[MAX];
    
    
    
    
    
    
    int main()
    {
    prompt();
    return 0;
    }
    
    
    
    
    void prompt()
    {
        int choice, flag = 1 ;
    
    
    printstudentInfo();
    printf("\n");
    
    
        printf("Please choose an option:\n");
        printf("  1. View the information for a specific student\n");
        printf("  2. Add a new student\n");
        printf("  3. Exit\n");
        printf("What is your choice? :");
        scanf("%d",&choice);
    
    
        while(flag)
        {
            switch(choice)
            {
            case 1:
                getinfoStudent();
                break;
            case 2:
                system("cls");
                addstudentInfo();
                break;
            case 3:
                exit(0);
                break;
    
    
            default:
            printf("Invalid choice. Please try again.\n");
            printf("Press any key to continue.\n");
            getch();
            system("cls");
            prompt();
            }
        }
    
    
    }
    
    
    
    
    void getinfoStudent()
    {
        system("cls");
    printpartial();
    }
    
    
    
    
    void printstudentInfo()
    {
    int i;
    FILE *fp;
       printf("F Name  L Name  Student# A1  A2  A3  A4  A5  L1  L2  L3  L4  L5  Midterm  Final\n");
       printf("________________________________________________________________________________\n");
    fp = fopen("studentinfo.txt","r");
    
    
     if (fp == NULL)
        {
             printf("I couldn't open studentinfo.txt for printing.\n");
             exit(0);
        }
    
    
        for(i = 0; i< MAX; i++)
        {
    while (fscanf(fp, "%s %s %d %d %d %d %d %d %d %d %d %d %d %d %d\n", student[i].firstname, student[i].lastname,&student[i].studentnumber,
                  &student[i].markAssignment[0],&student[i].markAssignment[1],&student[i].markAssignment[2],&student[i].markAssignment[3],
                  &student[i].markAssignment[4],&student[i].markLab[0],&student[i].markLab[1],&student[i].markLab[2],&student[i].markLab[3],
                  &student[i].markLab[4],&student[i].markMidterm,&student[i].markFinal)== 15)
    
    
        printf("%s %s  %d  %d %d %d %d %d %d %d %d %d %d %d %d\n",student[i].firstname, student[i].lastname,student[i].studentnumber,
               student[i].markAssignment[0],student[i].markAssignment[1],student[i].markAssignment[2],student[i].markAssignment[3],
               student[i].markAssignment[4],student[i].markLab[0],student[i].markLab[1],student[i].markLab[2],student[i].markLab[3],
               student[i].markLab[4],student[i].markMidterm,student[i].markFinal);
        }
    
    
    fclose(fp);
    
    
    }
    
    
    
    
    void printpartial()
    {
    int i,studentnum,flag = 1;
    FILE *fp;
    
    
    fp = fopen("studentinfo.txt","r");
    
    
     if (fp == NULL) {
             printf("I couldn't open studentinfo.txt for printing.\n");
             exit(0);
          }
    
    
    while (flag)
    {
    
    
    printf("Please type the student number to look at the student's information: ");
    scanf("%d",&studentnum);
    
    
    
    
     for (i = 0;i<MAX;i++)
        {
            while (fscanf(fp, "%s %s %d %d %d %d %d %d %d %d %d %d %d %d %d\n", student[i].firstname, student[i].lastname,&student[i].studentnumber,
                  &student[i].markAssignment[0],&student[i].markAssignment[1],&student[i].markAssignment[2],&student[i].markAssignment[3],
                  &student[i].markAssignment[4],&student[i].markLab[0],&student[i].markLab[1],&student[i].markLab[2],&student[i].markLab[3],
                  &student[i].markLab[4],&student[i].markMidterm,&student[i].markFinal)== 15)
        if (studentnum == student[i].studentnumber) // finding whether the colour is invalid or not
        {
          printf("\nF Name  L Name  Student# A1  A2  A3  A4  A5  L1  L2  L3  L4  L5  Midterm  Final\n");
       printf("________________________________________________________________________________\n");
                printf("%s %s %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
                       student[i].firstname,student[i].lastname,student[i].studentnumber,student[i].markAssignment[0],
                       student[i].markAssignment[1],student[i].markAssignment[2],student[i].markAssignment[3],student[i].markAssignment[4],
                       student[i].markLab[0],student[i].markLab[1],student[i].markLab[2],student[i].markLab[3],student[i].markLab[4],student[i].markMidterm, student[i].markFinal);
    
    
         fclose(fp);
    
    
         printf("\n Please press key to continue. ");
         getch();
         system("cls");
         prompt();
        }
        else
        {
            flag = 1;
        }
        }
    
    
    if(flag == 1)
        {
                 printf("Invalid student number. Please type it again.\n");
                getch();
                getinfoStudent();
        }
    }
    
    
    }
    
    
    
    
    
    
    void addstudentInfo()
    {
    char first[30],last[30];
    int stunum,a1,a2,a3,a4,a5,l1,l2,l3,l4,l5,m,f,ch,ln;
    FILE *fp;
    
    
    fp = fopen("studentinfo.txt","a+");
    
    
     if (fp == NULL)
        {
             printf("I couldn't open studentinfo.txt for printing.\n");
             exit(0);
        }
    
    
    ch = 1;
    while(ch)
    {
        printf("What is the student's first name? :");
        scanf("%s",first);
    
    
        printf("What is the student's last name? :");
        scanf("%s",last);
    
    
        printf("What is the student number? :");
        scanf("%d",&stunum);
    
    
    char s1[50] = "What is the student's Assignment 1 mark? :";
    a1 = compare(s1);
    char s2[50] = "What is the student's Assignment 2 mark? :";
    a2 = compare(s2);
    char s3[50] = "What is the student's Assignment 3 mark? :";
    a3 = compare(s3);
    char s4[50] = "What is the student's Assignment 4 mark? :";
    a4 = compare(s4);
    char s5[50] = "What is the student's Assignment 5 mark? :";
    a5 = compare(s5);
    char s6[50] = "       What is the student's Lab 1 mark? :";
    l1 = compare(s6);
    char s7[50] = "       What is the student's Lab 2 mark? :";
    l2 = compare(s7);
    char s8[50] = "       What is the student's Lab 3 mark? :";
    l3 = compare(s8);
    char s9[50] = "       What is the student's Lab 4 mark? :";
    l4 = compare(s9);
    char s10[50] = "       What is the student's Lab 5 mark? :";
    l5 = compare(s10);
    char s11[50] = "     What is the student's Midterm mark? :";
    m = compare(s11);
    char s12[50] = "       What is the student's Final mark? :";
    f = compare(s12);
    
    
        fprintf(fp,"\n%s %s %d %d %d %d %d %d %d %d %d %d %d %d %d\n",first,last,stunum,a1,a2,a3,a4,a5,l1,l2,l3,l4,l5,m,f);
        printf("\nStudent information is succesfully added.\n\n");
    
    
        printf("Added Student\n");
        printf("          Name: %s %s\nStudent Number: %d\n",first,last,stunum);
    
    
        printf("\npress 1 to continue adding student info or 0 to exit: ");
        scanf("%d",&ln);
        ch = ln;
        system("cls");
        }
    fclose(fp);
    prompt();
    }
    
    
    
    
    
    
    int compare(char st[50])
    {
        int a;
        int flag = 1;
    
    
    while(flag)
    {
        printf("%s",st);
        scanf("%d",&a);
    
    
        if(a > 120 || a < 0)
    {
        printf("Invalid mark. Please enter again\n");
        flag = 1;
    }
    else
    {
            flag = 0;
    }
    }
        return a;
    }
    
    
    
    
    void update()
    {
    
    
    }
    Inside of my studentinfo.txt file
    Code:
    Jnh Has 2353325 23 56 72 34 67 22 46 42 5 23 56 23
    Daniel Laurent 6744590 10 20 30 40 50 60 70 80 90 100 40 50
    This code is working totally fine. Right now, I'm trying to update a specific data from the student info.
    Let's say I'm trying to update my student number of Daniel.
    So, in this case, I want to replace 6744590 to some other student number.
    I think I have to use fseek() or rewind() and use fputs() but, I don't know how I should decide where to look.
    Could anyone suggest any ideas?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You are writing the student info as text to a file. this way the length of the records are different.
    There is no way to modify a single value in a text file.
    You will have to rewrite the whole file.

    Another way would be to use fwrite/fread to write/read the records in binary format. That way each record would have the same size and you could use fseek to position into the file and write a single record or even value.
    Kurt

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    40
    hmm... which file type should i use?
    and could you tell me how to rewrite the file?
    Last edited by tosihiro2007; 03-17-2013 at 03:24 PM.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    40
    Oh yeah I've got an another problem.
    Let's forget about updating. Now, I'm thinking to delete the studentinfo.
    Let's say I want to delete student[0] info, which is Jnh Has' and after that, I want to move my student[1] to student[0].
    Could anyone give me the idea for this?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by tosihiro2007 View Post
    Oh yeah I've got an another problem.
    Let's forget about updating. Now, I'm thinking to delete the studentinfo.
    Let's say I want to delete student[0] info, which is Jnh Has' and after that, I want to move my student[1] to student[0].
    Could anyone give me the idea for this?
    First, you should know that in a program handling more records, you wouldn't "delete" a record. You would zero out the record member values, and add a bit of code so your program would :

    1) not print them out or show them anywhere as part of the data it held

    2) would put new records, into those zeroed out record's place.

    With the idea of not moving data around (shuffling it, etc.)

    Let's say you have 10 records, and you want to delete #4:

    1, 2, 3, 4, 5, 6, 7, 8, 9, 10

    Delete #4
    1, 2, 3, X, 5, 6, 7, 8, 9, 10

    Shuffle the higher records, down by one, beginning 1 record higher higher than the record you deleted:

    Code:
    .........<  <  <  <  <  <   <== shuffled records
         
    1, 2, 3, 4, 5, 6, 7, 8, 9
    Give that a try.
    Last edited by Adak; 03-17-2013 at 04:03 PM.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    40
    hmm... I understand what you are talking about but, I don't know what type of code I should use...

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by tosihiro2007 View Post
    hmm... I understand what you are talking about but, I don't know what type of code I should use...
    Why not assign the entire student struct (as one object), down to the array[index], that is one less?

    Start with 5 (in the example above, and move it to 4, then 6 to 5, then 7 to 6, etc., using a for loop?

    This is VERY simple, so be careful NOT to over think it.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    40
    oh yeah I tried that. However, when I ran this program, it prints
    Jhn Has 5543535 44 34 43 43 523 23 23 424 .......
    0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0
    .
    .
    .
    .
    199 times.
    because I defined the max memory to 200.
    Does anyone know how to stop this?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have an int variable "count"=0, which you increment every time you add a record. That tells you how many actual good data items that you start out with. When you loop in your code, you only loop from 0 to <count, not <max.

    If you delete a record then you --count, so it's still good to use count.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating the Data on a Datagrid from the Datasource
    By AntV in forum C++ Programming
    Replies: 0
    Last Post: 07-22-2009, 12:15 PM
  2. Getting Specific Data from a file
    By darknite135 in forum C++ Programming
    Replies: 4
    Last Post: 12-31-2007, 11:41 PM
  3. Parsing specific data from one text file to another..
    By chops11 in forum C Programming
    Replies: 2
    Last Post: 09-13-2005, 10:50 AM
  4. Self-Updating Data Structures...
    By alvifarooq in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2005, 06:12 AM
  5. saving and updating data
    By izimmer in forum C Programming
    Replies: 1
    Last Post: 04-26-2002, 05:08 AM