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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:39:50+00:00 2026-06-06T12:39:50+00:00

In my assignment i have a problem with reading a file. See the following

  • 0

In my assignment i have a problem with reading a file. See the following code segment.

    std::string data;
    std::ifstream fileRead;

    fileRead.open("a.txt");

    while (fileRead >> data) 
    {
        long a = fileRead.tellg();
        fileRead.seekg (a+1, ios::beg);

        std::string check;
        //some code here

        while (fileRead >> check)
        {
            //some code here
        }
        fileRead.seekg (a+1, ios::beg);
    }

I have to check how many same words are in the file. My logic is, i read a word and keep it in data. Then i continue reading after that word using fileRead.seekg (a+1, ios::beg); Then i compare each of the words in the file.

After checking whole file i again put my fileobject to next word using this line fileRead.seekg (a+1, ios::beg); But this line is not working. I mean first while loop just work once. Someone please help me out here.

By the way, i am totally new in file operation. So my logic or concept may not be correct in that case tell me what is the right way to do this? But i need to know why first while loop ends just after reading one word?

  • 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-06-06T12:39:51+00:00Added an answer on June 6, 2026 at 12:39 pm

    When you read file as while(fileRead >> data), the a failure flag is set for the stream object, before exiting the loop. In fact, it is set that is why it exits the loop. You need to clear this failure flagas:

    fileRead.clear(); //clear the failure flag
    fileRead.seekg (0, ios::beg);
    //now read
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a comma separated file named foo.csv containing the following data: scale, serial,
I am using Tkinter in a programming assignment and have the following problem. I
I usually loop through lines in a file using the following code: open my
I have assignment to work on producer and consumer problem by use thread and
This is a follow up question from Problem with array assignment I now have
I am hacking away at a uni assignment and have come across a problem
I have a problem with some C code I am writing as part of
Continuing on this problem , but I'll reiterate: For a homework assignment I have
I have an assignment problem with cost matrix C, eg: 21 30 26 16
I have an assignment that has asked me to copy a file using buffered

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.