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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:56:32+00:00 2026-05-26T06:56:32+00:00

#include<stdio.h> #include<fstream.h> class Test { char name[10]; int data; public: void getData() { cin>>name;

  • 0
#include<stdio.h>
#include<fstream.h>
class Test
{
    char name[10];
    int data;
    public:
        void getData()
        {
            cin>>name;
            cin>>data;
        }
        void display()
        {
            cout<<name<<data;
        }
        void modify()
        {
            cin>>name;
            cin>>data;
        }
};
int main()
{
    Test t1,t2,t3,t4;
//  remove("FileIO.dat");
    t1.getData();
    t2.getData();
    t3.getData();
    t4.getData();
    fstream fp1("FileIO.dat",ios::out|ios::app);
    fp1.write((char*)&t1,sizeof(t1));
    fp1.write((char*)&t2,sizeof(t2));
    fp1.write((char*)&t3,sizeof(t3));
    fp1.write((char*)&t4,sizeof(t4));
    fp1.close();
    fstream fp2("FileIO.dat",ios::in|ios::out);
    fp2.read((char*)&t1,sizeof(t1));
    fp2.read((char*)&t2,sizeof(t2));
    int pos=-1*sizeof(t2); // ****** not understanding this line
    cout<<pos;
    fp2.seekp(pos,ios::cur);
    t2.modify();
    fp2.write((char*)&t2,sizeof(t2));
    fp2.read((char*)&t3,sizeof(t3));
    fp2.read((char*)&t4,sizeof(t4));
    t1.display();
    t2.display();
    t3.display();
    t4.display();
    fp2.close();
    return 0;
}

The program is written in turbo C++ and it deals with writing objects into the files and reading them back as well as updating the object that have been written into the file.

In the above code i am not understanding why -1 is multiplied by sizeof object to get the position.
Anyone please explain.

  • 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-26T06:56:32+00:00Added an answer on May 26, 2026 at 6:56 am

    Its because your program is reading the data from t1, reading the data from t2, modifying t2, and then writing over the contents of t2.

    When the fp2 is first opened, the file looks like this (^ represents the current position of the file pointer):

    +-----------+-----------+-----------+-----------+
    |  t1 data  |  t2 data  |  t3 data  |  t4 data  |
    +-----------+-----------+-----------+-----------+
    ^
    |
    

    After reading t1 and t2, the pointer is now going to be pointing to the beginning of t3:

    +-----------+-----------+-----------+-----------+
    |  t1 data  |  t2 data  |  t3 data  |  t4 data  |
    +-----------+-----------+-----------+-----------+
                            ^
                            |
    

    Now, in order to write over t2’s data, we need to move the file pointer back to the start of t2. How far back is that? -1 * sizeof(t2):

    +-----------+-----------+-----------+-----------+
    |  t1 data  |  t2 data  |  t3 data  |  t4 data  |
    +-----------+-----------+-----------+-----------+
                            ^
                            |
                <-----------+
                      |
                      This distance == sizeof(t2)
    

    From there, your program runs fp2.seekp(pos,ios::cur);. Since pos is negative, it moves the file pointer backwards, and your file is left in this state:

    +-----------+-----------+-----------+-----------+
    |  t1 data  |  t2 data  |  t3 data  |  t4 data  |
    +-----------+-----------+-----------+-----------+
                ^
                |
    

    And now you can overwrite t2’s data.

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

Sidebar

Related Questions

#include stdafx.h #include stdio.h #include math.h #include stdlib.h void test (int a,int *b, int
#include <stdio.h> #include <stdlib.h> #include <time.h> void initDeck (int deck[]); void showDeck (int deck[]);
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#include<stdio.h> #include<signal.h> #include<stdlib.h> void handler(int signo) { printf(First statement); system(date); exit(EXIT_SUCCESS); } int main()
#include<stdio.h> #include<iostream.h> #include<conio.h> #include<stdlib.h>(TOP) #include<fstream.h> #define MAX 5 int top = -1; int stack_arr[MAX];
Code : #include stdio.h #include string.h int main() { char *p = abc; printf(p
#include <stdio.h> int main() { unsigned long long int num = 285212672; //FYI: fits
#include<stdio.h> #include<time.h> int main() { clock_t start; double d; long int n,i,j; scanf(%ld,&n); n=100000;
#include <stdio.h> int main() { float a = 1234.5f; printf(%d\n, a); return 0; }
#include<stdio.h> int main() { printf(He %c llo,65); } Output: He A llo #include<stdio.h> int

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.