What's new

Closed File modifying and deleting

Status
Not open for further replies.

Jannelle345

Honorary Poster
Joined
Sep 9, 2018
Posts
250
Reaction
83
Points
160
Ilang days na lng due date na ng project ko sino marunung d2 pa tulong dito lng tlga ako na hold sa modify and delete using structure variable and pointers and FILE dapat naka save in note pad at nababasa di yung naka binary lng
Sana may maka pansin

Choses:
1.add student
2.edit student
3.delete student
4.show all student exist

Using file write and read
 
Last edited:
matik naman po diba na kapag may nilagay kang input e pwede maedit? bakit po hiwalay pa?
 
matik naman po diba na kapag may nilagay kang input e pwede maedit? bakit po hiwalay pa?
Di po example if click nya yung edit sa chooses then input nya yung name or id na modidy nya tapus matatapak dyan dyan ako parati naka hold di ma scan yung search name or id
 
Di po example if click nya yung edit sa chooses then input nya yung name or id na modidy nya tapus matatapak dyan dyan ako parati naka hold di ma scan yung search name or id
ay naka .exe na pala? cmd palang kame :)
 
Last edited by a moderator:
Di po example if click nya yung edit sa chooses then input nya yung name or id na modidy nya tapus matatapak dyan dyan ako parati naka hold di ma scan yung search name or id

Ibig mo sabihin may kasali na ang GUI sa program mo?

Bigyan kita ng clue :
  1. Use strcmp function on student name.
  2. Use strcmp function on id if its a string.
Or better yet show us your code. Mahirap manghula ng logic at algorithm na ginagamit sa code mo.
 
Ibig mo sabihin may kasali na ang GUI sa program mo?

Bigyan kita ng clue :
  1. Use strcmp function on student name.
  2. Use strcmp function on id if its a string.
Or better yet show us your code. Mahirap manghula ng logic at algorithm na ginagamit sa code mo.
Mukhang yan yung sulution tlga paps para saan nga pala yang strcmp kasi string tlga id na ginagamit namin eh
 
Ibig mo sabihin may kasali na ang GUI sa program mo?

Bigyan kita ng clue :
  1. Use strcmp function on student name.
  2. Use strcmp function on id if its a string.
Or better yet show us your code. Mahirap manghula ng logic at algorithm na ginagamit sa code mo.
Thanks sa clue mo paps may idea na ako sa edit yan tlga ako na bibiktima eh
 
Thanks paps sa delete paps paano yun?

Yan ang medyo komplikado. Kailangan mo nang mag improvise dyan. Walang standard library function para dyan.

Ang pinaka simple mong magagawa ay :
  1. Load mo lahat ng records sa "students.txt" file sa memory/buffer.
  2. Kung pwede lahat ng add, insert at delete operations doon mo gawin sa naka load sa memory/buffer.
  3. Open mo ulit ang "students.txt" pero hindi na append mode (a) dapat write mode na (w) sa fopen function mo. Para i overwrite o para blank na ang file na yan.
  4. I lagay mo pabalik ang lahat na updated data sa students record na naka load sa memory/buffer sa "students.txt" file.
Or better yet. Refer ka na lang sa example na to kasi mahaba haba na ito.

You do not have permission to view the full content of this post. Log in or register now.
 
Yan ang medyo komplikado. Kailangan mo nang mag improvise dyan. Walang standard library function para dyan.

Ang pinaka simple mong magagawa ay :
  1. Load mo lahat ng records sa "students.txt" file sa memory/buffer.
  2. Kung pwede lahat ng add, insert at delete operations doon mo gawin sa naka load sa memory/buffer.
  3. Open mo ulit ang "students.txt" pero hindi na append mode (a) dapat write mode na (w) sa fopen function mo. Para i overwrite o para blank na ang file na yan.
  4. I lagay mo pabalik ang lahat na updated data sa students record na naka load sa memory/buffer sa "students.txt" file.
Or better yet. Refer ka na lang sa example na to kasi mahaba haba na ito.

You do not have permission to view the full content of this post. Log in or register now.
Write mode lng yung ginagamit ko para mas simple
 
Yan ang medyo komplikado. Kailangan mo nang mag improvise dyan. Walang standard library function para dyan.

Ang pinaka simple mong magagawa ay :
  1. Load mo lahat ng records sa "students.txt" file sa memory/buffer.
  2. Kung pwede lahat ng add, insert at delete operations doon mo gawin sa naka load sa memory/buffer.
  3. Open mo ulit ang "students.txt" pero hindi na append mode (a) dapat write mode na (w) sa fopen function mo. Para i overwrite o para blank na ang file na yan.
  4. I lagay mo pabalik ang lahat na updated data sa students record na naka load sa memory/buffer sa "students.txt" file.
Or better yet. Refer ka na lang sa example na to kasi mahaba haba na ito.

You do not have permission to view the full content of this post. Log in or register now.
Paps ito yung code ko ayaw gumana yung strcmp sa akin eh
 

Attachments

Paps ito yung code ko ayaw gumana yung strcmp sa akin eh

Anong compiler ginagamit nyo?
Bakit ang daming "main();" ganito? Hindi ka dapat mag call ng "main" function. Ngayon lang ako nakakita ng ganito.
Haaays! Hindi ko alam kung saan magsisimula.

Ewan ko kung tapos ka na ba nito pero eto na at binago ko ang dl8 function mo.

At tsaka masyadong nakadepende ang code sa ibang functions mo sa fixed size na number ng student records at pag nag delete ka na mababawasan na yan at hindi na gagana ng tama yong ibang mga functions mo.

Code:
void dl8()
{
    struct student studBuffer[5];
    char studName[15];
    int foundFlag, studentIndex, i;

    f = fopen("Record.txt","r");

    // If f is not NULL then load all the contents to buffer
    if (f != NULL)
    {
        printf("Opening Student File Successful...\n");

        // Fill our buffer of zeroes to clear out leftover garbage data.
        memset(studBuffer,0,sizeof(studBuffer) * 5);

        for(i=0;i<5;i++)
        {
            fscanf(f,"%s%s%s%d%lf%lf%lf%lf",
                    studBuffer[i].id,
                    studBuffer[i].name,
                    studBuffer[i].course,
                    &studBuffer[i].year,
                    &studBuffer[i].grade[0],
                    &studBuffer[i].grade[1],
                    &studBuffer[i].grade[2],
                    &studBuffer[i].fgrade);
        }
        // Now that we all the necessary data in the buffer
        // we can close the file for now.
        fclose(f);
    }
    else
    {
        printf("Opening Student File Failed...");
        // Exit out of the function
        return;
    }

    // Time to find out which student record to delete.
    // But first we need to ask for the name of the student

    printf("Enter the name of the student to remove out of the record: \n");
    scanf("%s",studName);

    // Now we have a name. Time to find out if that name
    // is in our student records.

    // Initialize our name is found flag.
    // Set to 0 to signify as false.

    foundFlag = 0;

    // Loop counter
    i = 0;
    while (foundFlag != 1 && i < 5)
    {
        // If function strcmp returns 0 then we found a match.
        if (strcmp(studName,studBuffer[i].name) == 0)
        {
            // Get the array index of the matching student name.
            studentIndex = i;
            foundFlag = 1;
        }
        i++;
    }

    // If no match is found. Return to the caller of the this function
    // or exit out of this funtion.
    if (foundFlag != 1)
    {
        printf("No found match on students name. \n");
        // Exit out of function.
        return;
    }

    // If we reached here it means we have found a match.
    // Now time to write the changes in the file.

    // Open the file in write mode.
    f = fopen("Record.txt","w");

    // If f is not NULL then write all the contents the buffer
    // into the file except for the student to be removed.
    if (f != NULL)
    {
        for(i=0;i<5;i++)
        {
            // Write into the file except for the array index
            // of student to removed.
            if (i != studentIndex)
            {
                fprintf(f,"%s\n%s\n%s\n%d\n%.2lf\n%.2lf\n%.2lf\n%.2lf\n",
                        studBuffer[i].id,
                        studBuffer[i].name,
                        studBuffer[i].course,
                        studBuffer[i].year,
                        studBuffer[i].grade[0],
                        studBuffer[i].grade[1],
                        studBuffer[i].grade[2],
                        studBuffer[i].fgrade);
            }
        }
        fclose(f);

        printf("Student record deletion is successful.\n");
    }
    else
    {
        printf("Student record deletion is failed.\n");
    }
}
 
Thanks d2 paps dami ko nakuha tulong at alam kuna para mag reduce ng size dahil dito
Gumamit ako ng call "main" para bumalik sa main function
Anong compiler ginagamit nyo?
Bakit ang daming "main();" ganito? Hindi ka dapat mag call ng "main" function. Ngayon lang ako nakakita ng ganito.
Haaays! Hindi ko alam kung saan magsisimula.

Ewan ko kung tapos ka na ba nito pero eto na at binago ko ang dl8 function mo.

At tsaka masyadong nakadepende ang code sa ibang functions mo sa fixed size na number ng student records at pag nag delete ka na mababawasan na yan at hindi na gagana ng tama yong ibang mga functions mo.

Code:
void dl8()
{
    struct student studBuffer[5];
    char studName[15];
    int foundFlag, studentIndex, i;

    f = fopen("Record.txt","r");

    // If f is not NULL then load all the contents to buffer
    if (f != NULL)
    {
        printf("Opening Student File Successful...\n");

        // Fill our buffer of zeroes to clear out leftover garbage data.
        memset(studBuffer,0,sizeof(studBuffer) * 5);

        for(i=0;i<5;i++)
        {
            fscanf(f,"%s%s%s%d%lf%lf%lf%lf",
                    studBuffer[i].id,
                    studBuffer[i].name,
                    studBuffer[i].course,
                    &studBuffer[i].year,
                    &studBuffer[i].grade[0],
                    &studBuffer[i].grade[1],
                    &studBuffer[i].grade[2],
                    &studBuffer[i].fgrade);
        }
        // Now that we all the necessary data in the buffer
        // we can close the file for now.
        fclose(f);
    }
    else
    {
        printf("Opening Student File Failed...");
        // Exit out of the function
        return;
    }

    // Time to find out which student record to delete.
    // But first we need to ask for the name of the student

    printf("Enter the name of the student to remove out of the record: \n");
    scanf("%s",studName);

    // Now we have a name. Time to find out if that name
    // is in our student records.

    // Initialize our name is found flag.
    // Set to 0 to signify as false.

    foundFlag = 0;

    // Loop counter
    i = 0;
    while (foundFlag != 1 && i < 5)
    {
        // If function strcmp returns 0 then we found a match.
        if (strcmp(studName,studBuffer[i].name) == 0)
        {
            // Get the array index of the matching student name.
            studentIndex = i;
            foundFlag = 1;
        }
        i++;
    }

    // If no match is found. Return to the caller of the this function
    // or exit out of this funtion.
    if (foundFlag != 1)
    {
        printf("No found match on students name. \n");
        // Exit out of function.
        return;
    }

    // If we reached here it means we have found a match.
    // Now time to write the changes in the file.

    // Open the file in write mode.
    f = fopen("Record.txt","w");

    // If f is not NULL then write all the contents the buffer
    // into the file except for the student to be removed.
    if (f != NULL)
    {
        for(i=0;i<5;i++)
        {
            // Write into the file except for the array index
            // of student to removed.
            if (i != studentIndex)
            {
                fprintf(f,"%s\n%s\n%s\n%d\n%.2lf\n%.2lf\n%.2lf\n%.2lf\n",
                        studBuffer[i].id,
                        studBuffer[i].name,
                        studBuffer[i].course,
                        studBuffer[i].year,
                        studBuffer[i].grade[0],
                        studBuffer[i].grade[1],
                        studBuffer[i].grade[2],
                        studBuffer[i].fgrade);
            }
        }
        fclose(f);

        printf("Student record deletion is successful.\n");
    }
    else
    {
        printf("Student record deletion is failed.\n");
    }
}
 
Thanks d2 paps dami ko nakuha tulong at alam kuna para mag reduce ng size dahil dito
Gumamit ako ng call "main" para bumalik sa main function

I would strongly advice to never use that method of calling "main" function from inside the "main" function or even anywhere else. You are doing recursion on the whole program itself. It can further complicate code and introduce more bugs. The effects are more evident when your programs get larger. It can easily eat all memory available.

What you should do instead is something like this.

Code:
#include <stdio.h>

void main()
{
    int exitFlag, input;

    exitFlag = 0;

    // This is your main program loop.
    while (exitFlag == 0)
    {

        // Put code here that needs to loop.
        printf("Give me any int value. If the value is 69 I will quit.\n");
        scanf("%d",&input);

        // To get out of this loop.
        // Just set exitFlag to 1.
        if (input == 69)
        {
            exitFlag = 1;
        }
    }
}
 
Status
Not open for further replies.
Back
Top