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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:25:55+00:00 2026-05-24T07:25:55+00:00

I have numerical text data lines ranging between 1mb – 150 mb in size,

  • 0

I have numerical text data lines ranging between 1mb – 150 mb in size, i need to write lines of numbers related to heights, for example: heights=4 , new text must include lines: 1,5,9,13,17,21…. consequentially.

i have been trying to find a way to do this for a while now, tried using a list instead of vector which ended up with compilation errors.

I have cleaned up the code as advised. It now writes all lines sample2 text, all done here. Thank you all

I am open to method change as long as it delivers what i need, Thank you for you time and help.

following is what i have so far:

#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <vector>

using namespace std;

int h,n,m;
int c=1;

int main () {

cout<< "Enter Number Of Heights: ";
cin>>h;

ifstream myfile_in ("C:\\sample.txt");
ofstream myfile_out ("C:\\sample2.txt");
string line;
std::string str;
vector <string> v;
if (myfile_in.is_open()) {
myfile_in >> noskipws;
int i=0;
int j=0;
while (std::getline(myfile_in, line)) {
v.push_back( line );
++n;
if (n-1==i) {
myfile_out<<v[i]<<endl;
i=i+h;
++j;
}
    }
cout<<"Number of lines in text file: "<<n<<endl;
}

else cout << "Unable to open file(s) ";

cout<< "Reaching here, Writing one line"<<endl;

system("PAUSE");
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. Editorial Team
    Editorial Team
    2026-05-24T07:25:56+00:00Added an answer on May 24, 2026 at 7:25 am

    You need to use seekg to set the position at the beginning of the file, once you have read it (you have read it once, to count the lines (which I don’t think you actually need, as this size is never used, at least in this piece of code)

    And what is the point if the inner while? On each loop, you have

    int i=1;
    myfile_out<<v[i]; //Not writing to text
    i=i+h;
    

    So on each loop, i gets 1, so you output the element with index 1 all the time. Which is not the first element, as indices start from 0. So, once you put seekg or remove the first while, your program will start to crash.

    So, make i start from 0. And get it out of the two while loops, right at the beginning of the if-statement.

    Ah, the second while is also unnecessary. Leave just the first one.


    EDIT:
    Add

    myfile_in.clear();
    

    before seekg to clear the flags.

    Also, your algorithm is wrong. You’ll get seg fault, if h > 1, because you’ll get out of range (of the vector). I’d advise to do it like this: read the file in the while, that counts the lines. And store each line in the vector. This way you’ll be able to remove the second reading, seekg, clear, etc. Also, as you already store the content of the file into a vector, you’ll NOT lose anything. Then just use for loop with step h.


    Again edit, regarding your edit: no, it has nothing to do with any flags. The if, where you compare i==j is outside the while. Add it inside. Also, increment j outside the if. Or just remove j and use n-1 instead. Like

    if ( n-1 == i )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a text file which contains some numerical data. Of that text
I have an order form with about 30 text fields that contain numerical values.
I have a couple of numerical datasets that I need to create a concept
I have a need to display many numerical values in columns. These values need
I have data frame with some numerical variables and some categorical factor variables. The
So I have a large number of very large ASCII files of numerical data
I have a series of large, flat text files that I need to parse
I have some text data in this format: MI 00 3 MD 1 0.0000
I have an experiment streaming up 1Mb/s of numeric data which needs to be
I have two sets of data (3 columns: x=categorical, y = numerical, l =

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.