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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:40:13+00:00 2026-05-20T14:40:13+00:00

hello i am trying to write to a FILE in a wanted line number

  • 0

hello i am trying to write to a FILE in a wanted line number using c programming language
and for some unknown reasons it doesnt get written
this is my checking code:

int main()
{
    int x;
    int counter = 0;
    char buffer[MAX];
    FILE* fp = fopen("sale_day.txt","w");
    fprintf(fp,"5 orange 11\n");
    fprintf(fp,"4 pelephone 222\n");
    fprintf(fp,"3 mirs 4000\n");
    fprintf(fp,"2 cellcom 302\n");
    fprintf(fp,"1 tmobile 500\n");
    fclose(fp);
    fp = fopen("sale_day.txt","r+");
    while (counter < 2)
    {// jumping two rows
        fgets(buffer,MAX,fp);// i tried using fscanf which didnt help aswell
        counter++;
    }
    fflush(fp); // i tried with and without still doesnt work
    fputs("$",fp);
    fflush(fp); // i tried with and without still doesnt work
    fclose(fp);
}

i expect to get :

5 orange 11
4 pelephone 222
$ mirs 4000
2 cellcom 302
1 tmobile 500

for some reason it stays as the following in “sale_day.txt” file

5 orange 11
4 pelephone 222
3 mirs 4000
2 cellcom 302
1 tmobile 500

even tho when i debug it it shows a “$” instead of the 3 digit

thanks in advance for your help !

  • 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-20T14:40:13+00:00Added an answer on May 20, 2026 at 2:40 pm

    What are you expecting? When I run your code, I get:

    5 orange 11
    4 pelephone 222
    $ mirs 4000
    2 cellcom 302
    1 tmobile 500
    

    This is exactly right given what you’ve coded: read two lines (leaving you positioned at the third line), and write a $ char.

    Note that file write operations overwrite existing file data, or append new data to the end of a file. They don’t insert data (which may have been the operation you’re expecting).

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

Sidebar

Related Questions

I’m trying to write a twitter program to get my tinme line. I’m using
Hello I'm trying to write some tool using code::blocks, wxWidgets and libxml2 on Windows
Hello there im trying to send files using client-server classes in java. For some
i am trying to write a boot loader(hello world sort). i am using Bochs
I am using JDK 7 and I am trying to perform a simple write-to-file
Hello I'm getting this error, while trying to save serial number to XML File.
Hello i am trying to write the following function to display 7 days of
Hello everyone I am trying to write a string on image during runtime and
Hello I'm trying something .. I was reading Jquery traversing documentations some forums, this
Hello I am trying to setup a Padrino project using DataMapper and MySQL on

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.