Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3485270
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:53:15+00:00 2026-05-18T10:53:15+00:00

guys can you help me with my code.. i want to edit a specific

  • 0

guys can you help me with my code.. i want to edit a specific line in a text file using c i have this code…

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


struct studentinfo{

       char id[8];
       char name[30];
       char course[5];
}s1;

int main(void){

     FILE *stream = NULL;
     FILE *stream2 = NULL;
     stream = fopen("studentinfo.txt", "rt");
     stream2 = fopen("studentinfo2.txt", "w+");

     char arr [100];
     char arr2[100];
     char arr3[100];
     int i=0;
     int count=0;

     printf("enter details: ");
     gets(arr2);
     printf("enter new student id: ");
     gets(arr3);

    while(!feof(stream)){ 
     fgets(arr, 6, stream);
        if(strcmp(arr, arr2)!=0){
        fprintf(stream2, "%s", arr);
        }else printf("student id found!");
    }
     fclose(stream);
     fclose(stream2);
     getch();
}

The program successfully deletes the student id w/c was inputed by the user if it matches to the data in the text file.

but i still don’t know how to replace the student id or any fields related with it.

this program only copies data which is not equivalent to the user’s input and store it to another text file(i have 2 text files) this is the output if the user entered 12345

the way it stores data to the other file:

, name1, bsba

12346, name2, bsba

12347, name3, bsba

12350, name4, bsba

12390, name5, bs

AND THIS IS THE ORIGINAL FILE:

12345, name1, bsba

12346, name2, bsba

12347, name3, bsba

12350, name4, bsba

12390, name5, bs

any better solutions? thanks 🙂
anyway thanks again to aix, coz i’v got this idea from him… unfortunately i cant finish it… hope you can help me…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:53:16+00:00Added an answer on May 18, 2026 at 10:53 am

    You are reading only 5 characters at a a time. While this will work (because fgets will stop at the end of a line), it’s very inefficient and means you are comparing the users input to every 6 characters of a file, even when those file contents are not the student id.

    If you do want to continue with the approach of your program, when you do get a match with the user input, you need to read (and discard) the rest of the line before continuing examining further lines.

    For lines that don’t match, you should read (and copy into the new file) the remainder of the line without comparing it to the user input (since you know it is not the student id).

    I suspect the person who wrote the assignment expected you to read an entire line in, split it (by looking for the commas) into the various fields and put the information into your studentinfo structures. Then process the studentinfo in whatever way the assignment requested, and finally write the new file with the modified data.

    Although you can make your approach work for deleting a record of a specified student id, it is very inflexible. Searching for a record, or adding a record would require a complete rewrite of your program. If you had code that could read the information into an array of studentinfo structs, and write that info out again, any processing you needed to do would just work on those structs and the changes would be much smaller.

    So, in pseudo code, you want something like this

    allocate space for one line of the file
    allocate space for an array of struct studentinfos
    
    readinfo function:
    
    open the student info file for reading
    set the count of student records to 0
    while not at eof
        read in a line
        split the line on commas
            copy the bit before the first comma to the 'id' field of the newly allocated studentinfo record 
            copy the bit between first and second commas to the name field
            copy the bit from the second comma to the course field
        add one to the count of student records
    go back to read another line
    close the file
    
    writeinfo function:
    open the studentinfo file for writing
    loop over the studentinfo structs in order
        writeout the id, name and course strings of the current record, separated by comma and followed by new line
    close the file
    deletestudent function:
    read a course id from the user (or read it in your main program and pass it here as a parameter)
    loop over the studentinfo array
        compare the id to the one of the current record
        if a match
            shift all records after this down one by copying them over the top of the record before
           subtract one from the number of student records (since we've deleted one)
           return from the function indicating found and delete
    repeat for next record
    if you complete looking at all records,
        return from the function indicating no match found
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope some of you guys can help me with this problem.... I have
Can you guys help me figure this out? I have the following JavaScript snippet:
I'm hoping you guys can help me figure out why this is happening. I've
Maybe you guys can help: I have a variable called $bio with bio data.
can you guys help to convert a JPG File into an 2-dimensional int []
Hey guys i had an idea yesterday. Can you help me with this. Here
Hi guys I have this code that checks linkback: $reciprocal_linkback = reciprocal_linkback($contenturl,http://www.mydomain.com,1); if ($reciprocal_linkback==0)
i have a problem so hope you guys can help! I have a string
I am working on WPF with text. Now I want to edit text using
I am hitting a wall and have come here hoping you guys can help

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.