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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:49:08+00:00 2026-06-18T01:49:08+00:00

i wrote a code in C++ where it opens a .txt file and reads

  • 0

i wrote a code in C++ where it opens a .txt file and reads its contents, think of it as a (MAC address database), each mac address is delimited by a (.), my problem is after i search the file for total number of lines , iam unable to return the pointer to the initial position of the file in here i use seekg() and tellg() to manipulate the pointer to the file.

here is the code:

#include <iostream>
#include <fstream>
#include <conio.h>


using namespace std;

int main ()
{
 int i = 0;
string str1;

ifstream file;
file.open ("C:\\Users\\...\\Desktop\\MAC.txt");  


 //this section calculates the no. of lines

while (!file.eof() )
{
  getline (file,str1); 
 for (int z =0 ; z<=15; z++)
 if (str1[z] == '.')
 i++;   
}


file.seekg(0,ios::beg);
getline(file,str2);

cout << "the number of lines are " << i << endl; 
cout << str2 << endl;

file.close();


      getchar();
      return 0;
      }

and here is the contents of the MAC.txt file:

0090-d0f5-723a.

0090-d0f2-87hf.

b048-7aae-t5t5.

000e-f4e1-xxx2.

1c1d-678c-9db3.

0090-d0db-f923.

d85d-4cd3-a238.

1c1d-678c-235d.

here the the output of the code is supposed to be the first MAC address but it returns the last one .

  • 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-18T01:49:09+00:00Added an answer on June 18, 2026 at 1:49 am
    file.seekg(0,ios::end);
    

    I believe you wanted file.seekg(0,ios::beg); here.

    Zero offset from the end (ios::end) is the end of the file. The read fails and you’re left with the last value you read in the buffer.

    Also, once you’ve reached eof, you should manually reset it with file.clear(); before you seek:

    file.clear();
    file.seekg(0,ios::beg);
    getline(file,str2);
    

    The error would have been easier to catch if you checked for errors when you perform file operations. See Kerrek SB’s answer for examples.

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

Sidebar

Related Questions

So, here's the problem. I wrote a code that opens a div based on
I wrote X = [[1,2,3,4,5,6],[6,5,4,3,2,1]|...] to a file by the following code: ?-tell('test.txt'),maplist(format('~d ~d
I have a txt file with 16 lines of integers. Each line contains 5
Would appreciate some assistance i have a txt file witht he following contents: 1234|dog|apartment|two
When I try to open a file in write mode with the following code:
I wrote code to get name of printer which is installed in my system.but
I wrote code first without using functions to prototype, and of course, it worked
I recently wrote code that didnt work as i would expect, it was: message
i added sharekitkit my project. And wrote code like here : SHKItem *item =
I wrote some code in VHDL that is expected to look at a rotory

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.