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

  • Home
  • SEARCH
  • 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 8276215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:18:44+00:00 2026-06-08T08:18:44+00:00

I am trying to read in a file line by line, parse it based

  • 0

I am trying to read in a file line by line, parse it based on spaces and place it into a vector called input. However, I cannot do a cin >> x1 >> x2 >> x3 … because the line doesn’t have a set number of terms. Below is what I was thinking, but it could be wrong.

vector <double> input;
fstream inputFile("../Sphere.in", fstream::in);
inputFile.open("../Sphere.in", fstream::in);

while (inputFile.good())
{
    getline(inputFile, line);

then something in here that says for each space in line put in input[i]

 }

I am very new at C++ and would appreciate any help that someone could throw my way.

Thanks,

John

  • 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-08T08:18:45+00:00Added an answer on June 8, 2026 at 8:18 am

    while (inputFile.good()) is wrong, as it only evaluates to false after a read fails. Without additional checks inside the loop, the last iteration will work on invalid input. The correct, idiomatic way is to put the read in loop’s condition:

    while (getline(inputFile, line)) { ... }
    

    This way you only enter the loop if getline succeeds. Now you can construct a string stream from line and read doubles from there, using similar approach with read inside loop condition:

    while (getline(inputFile, line)) {
        istringstream iss(line);
        double d;
        while (iss >> d) input.push_back(d);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read and parse a file line by line, but there
I'm trying to read a file line by line starting from a specific line
I'm trying to read a file line by line to a string type variable
I am trying to read a file line-by-line, and storing that line of data
I am trying to read a text file line by line, and the text
I am trying to read file and split its line to get some context(Computer
I'm trying to read the last line from a text file. Each line starts
I am trying to read the number of line in a binary file using
What I am trying to do is, Open the word file, read a line,
I am trying to read a xml file from the web and parse it

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.