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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:49:15+00:00 2026-05-11T14:49:15+00:00

I have the following data that looks like this for example: 34 foo 34

  • 0

I have the following data that looks like this for example:

34 foo
34 bar
34 qux
62 foo1
62 qux
78 qux

These are sorted based on the first column.

What I want to do is to process lines that starts with 34, but I also want the file iteration to quit after it finds no more 34s, without having have to scan through whole file. How would I do this?

The reason is because the number of lines to be processed is very large (~ 10^7). And those that start with 34 are only around 1-10% of it.

I am aware that I can grep the lines and output it into another file, but this is too tedious and creates more disk space consumption.

This code illustrates my failed attempt using ‘continue’:

#include <iostream> #include <vector> #include <fstream>        #include <sstream>        using namespace std;       int main () {     string line;     ifstream myfile ('mydata.txt');     vector<vector<string> > dataTable;     if (myfile.is_open())     {         while (! myfile.eof() )            {                 stringstream ss(line);                     int FirstCol;                 string SecondCol;                  if (FirstCol != 34) {                    continue;                 }                  // This will skip those other than 34                 // but will still iterate through all the file                 // until the end.                  // Some processing to FirstCol and SecondCol                  ss >> FirstCol >> SecondCol;                 cout << FirstCol << '\t << SecondCol << endl;              }         myfile.close();     }      else cout << 'Unable to open file';        return 0; } 
  • 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. 2026-05-11T14:49:16+00:00Added an answer on May 11, 2026 at 2:49 pm

    Based on the assumption that the file is sorted by FirstCol, use a state variable that indicates whether or not you have found the first one. Once you have found the first one, as soon as you find a column that is != 34, you can break out of the loop.

    For example, suppose your data is now:

    15 boo 32 not 34 foo 34 bar 34 qux 62 foo1 62 qux 78 qux  

    …this code will do what you want:

    #include 'stdafx.h' #include <iostream> #include <vector> #include <fstream>        #include <sstream>        using namespace std;       int main () {     string line;     ifstream myfile ('mydata.txt');     vector<vector<string> > dataTable;     if (myfile.is_open())     {         bool found34 = false;          while ( myfile )            {                 stringstream ss(line);                     int FirstCol;                 string SecondCol;                // This will skip those other than 34                 // but will still iterate through all the file                 // until the end.                  // Some processing to FirstCol and SecondCol                  myfile >> FirstCol >> SecondCol;                 cout << FirstCol << '\t' << SecondCol << endl;                     switch( FirstCol )                 {                 case 34 :                     found34 = true;                     cout << 'Processing a 34';                     continue;   // keep looping                 default :                     if( found34 )                     {                         // we found all the 34's and now we're on to the next value, so we're done                         cout << 'We're done.';                         break;                     }                     else                     {                         // we haven't found the first 34 yet, so keep scanning until we do                         cout << 'Keep on looking for a 34...';                         continue;                     }                 }         }         myfile.close();     }      else cout << 'Unable to open file';        return 0; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer newline can take an optional argument of a port, on… May 12, 2026 at 12:17 am
  • Editorial Team
    Editorial Team added an answer You can treat the bit as a string ('0', '1')… May 12, 2026 at 12:17 am
  • Editorial Team
    Editorial Team added an answer use the tutorials at w3schools.com. They indicate how to work… May 12, 2026 at 12:17 am

Related Questions

Suppose i have the following class. public class Location { public Id { get;
I need to undo the following ASP.Net processes in PHP so I can get
I have an interesting problem, and I can't seem to figure out the lambda
I'm learning ASP.NET MVC (and MVC in general) and all the help online I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.