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 3452160
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:13:29+00:00 2026-05-18T09:13:29+00:00

Can you help me code a program that deletes a certain line in a

  • 0

Can you help me code a program that deletes a certain line in a text file.

I’m planning to use:

  1. fgets to capture a line from text file and store it in an array, but i was confused how does fgets store the line in an array(does it capture the line and store it in the first index and the next time it captures a line will this be stored to the next index or just overwrite the first index of an array?)

  2. if the line was stored in different indices I’ll then have a condition that compares the user’s input to the captured line of fgets and if it is equal, it will skip the line and do the rule till it reaches the end of file.

  3. then I’ll close the text file like this fclose(stream) and reopen it as “wt” to overwrite everything written at the file.

Am I having the right logic… or you can suggest better solutions…
hope you can help me understand how fgets store the line in an array…

btw this is the code I’m trying for my testing:

#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;
     stream = fopen("studentinfo.txt", "rt");

     char arr[100];
     int i=0;

    while(!feof(stream)){  
     fgets(arr, 100, stream);
     printf("%s", arr);
     }
     fclose(stream);
/*planning to reopen the stream but will change "rt" to "wt"*/

     getch();
}
  • 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-18T09:13:30+00:00Added an answer on May 18, 2026 at 9:13 am

    Well, disk space permitting, I would do the following:

    • open input file for reading
    • open a temporary file (in the same directory) for writing
    • while not at the end of input file:
      • read line from input file
      • if it does not equal the line you’re looking for, write it out to the output file
    • close both files
    • remove the input file
    • rename the temporary file to give it the input file’s name

    This avoids the need to keep the (almost) entire file in memory.

    It also does not suffer from the problem that if your program dies (or gets killed) in the middle of writing the file, a part of your input file may be lost for good.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe you can help me... I am writing a program in Windows Mobile that
HI i did this code with help of Marc Gravell in Why can't I
Are there any algorithms that can help with hierarchical clustering? Google's map-reduce has only
I would like information on algorithms that can help identify commonality and differences between
Is there anything which can help with msmq monitoring? I'd like to get some
Wondering if anyone can help me with this annoying but trivial (in terms of
I have heard using PDB files can help diagnose where a crash occurred. My
Hopefully some Custom Control Designers/Builders can help I'm attempting to build my first custom
I was wondering if anyone can help me get started with creating a room
I am wondering if someone can help me figure out the best approach to

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.