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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:06:30+00:00 2026-05-11T16:06:30+00:00

I would like to read an input file in C++, for which the structure

  • 0

I would like to read an input file in C++, for which the structure (or lack of) would be something like a series of lines with text = number, such as

input1 = 10
input2 = 4
set1 = 1.2
set2 = 1.e3

I want to get the number out of the line, and throw the rest away. Numbers can be either integers or doubles, but I know when they are one or other.

I also would like to read it such as

input1 =    10
input2=4
set1   =1.2
set2= 1.e3

so as to be more robust to the user. I think this means that it shouldn’t be red in a formatted fashion.

Anyway, is there a smart way to do that?

I have already tried the following, but with minimal knowledge of what I’ve been doing, so the result was as expected… no success.

    #include <stdio.h>
    #include <stdlib.h>
    #include <float.h>
    #include <math.h>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cstdlib>
    #include <boost/lexical_cast.hpp>
    #include <string>

    using namespace std;
    using namespace boost;

    int main(){

            string tmp;
            char temp[100];

            int i,j,k;

            ifstream InFile("input.dat");

            //strtol
            InFile.getline(temp,100);
            k=strtol(temp,0,10);
            cout << k << endl;

            //lexical_cast
            InFile.getline(temp,100);
            j = lexical_cast<int>(temp);
            cout << j << endl;

            //Direct read
            InFile >> tmp >> i;
            cout << i << endl;

            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-11T16:06:30+00:00Added an answer on May 11, 2026 at 4:06 pm

    Simply read one line at a time.
    Then split each line on the ‘=’ sign. Use the stream functionality do the rest.

    #include <sstream>
    #include <fstream>
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::ifstream    data("input.dat");
        std::string      line;
    
        while(std::getline(data,line))
        {
            std::stringstream    str(line);
            std::string          text;
    
            std::getline(str,text,'=');
    
            double   value;
            str >> value;
        }
    }
    

    With error checking:

    #include <sstream>
    #include <fstream>
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::ifstream    data("input.dat");
        std::string      line;
    
        while(std::getline(data,line))
        {
            std::stringstream    str(line);
            std::string          text;
            double               value;
    
            if ((std::getline(str,text,'=')) &&  (str >> value))
            {
                // Happy Days..
                // Do processing.
                continue; // To start next iteration of loop.
            }
            // If we get here. An error occurred.
            // By doing nothing the line will be ignored.
            // Maybe just log an error.
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 85k
  • Answers 85k
  • 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 Hate to kill the idea so quickly, but it's not… May 11, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer This is perfectly good. In fact, the canonical way of… May 11, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer There were a lot of good feedback related to the… May 11, 2026 at 5:11 pm

Related Questions

Can't exactly find a way on how to do the following in C/C++. Input
Alright here's the deal, I'm taking an intro to C++ class at my university
I am working on an application that involves some gis stuff. There would be
Hello I have a chunk of memory (allocated with malloc()) that contains bits (bit
I want my web application users to download some data as an Excel file.

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.