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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:45:59+00:00 2026-05-10T19:45:59+00:00

I have a file with data listed as follows: 0, 2, 10 10, 8,

  • 0

I have a file with data listed as follows:

0,       2,    10 10,       8,    10 10,       10,   10 10,       16,   10 15,       10,   16 17,       10,   16 

I want to be able to input the file and split it into three arrays, in the process trimming all excess spaces and converting each element to integers.

For some reason I can’t find an easy way to do this in c++. The only success I’ve had is by inputting each line into an array, and then regexing out all the spaces and then splitting it up. This entire process took me a good 20-30 lines of code and its a pain to modify for say another separator(eg. space), etc.

This is the python equivalent of what I would like to have in C++:

f = open('input_hard.dat') lines =  f.readlines() f.close()  #declarations inint, inbase, outbase = [], [], []  #input parsing for line in lines:     bits = string.split(line, ',')     inint.append(int(bits[0].strip()))     inbase.append(int(bits[1].strip()))     outbase.append(int(bits[2].strip())) 

The ease of use of doing this in python is one of the reasons why I moved to it in the first place. However, I require to do this in C++ now and I would hate to have to use my ugly 20-30 line code.

Any help would be appreciated, thanks!

  • 1 1 Answer
  • 1 View
  • 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-10T19:45:59+00:00Added an answer on May 10, 2026 at 7:45 pm

    There is really nothing wrong with fscanf, which is probably the fastest solution in this case. And it’s as short and readable as the python code:

    FILE *fp = fopen('file.dat', 'r'); int x, y, z; std::vector<int> vx, vy, vz;  while (fscanf(fp, '%d, %d, %d', &x, &y, &z) == 3) {   vx.push_back(x);   vy.push_back(y);   vz.push_back(z); } fclose(fp); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a data file that I want to process; I want to
I have my template file which has data being populated into it. However I
I have the following data (from a text file), I would like to split
I have a file data.base which looks like: 1234 XXXX 4321 XXXX 9884 ZZZZ
I have a file containing data in a single column .. I have to
I have a data file that looks like the following example. I've added '%'
I have a data file with the # sign as delimiter, that I would
I have a data file which is comprised of rows of data, newline separated.
I have a data file, a signature file (created by the GnuPG) and a
I have Text file that contains data separated with a comma , . How

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.