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

Related Questions

I would like to read the text and binary attachments in a saved Outlook
I have a byte array in memory, read from a file. I would like
In java i would like to read a file line by line and print
I've written a Brainfuck implementation (C++) that works like this: Read input brainfuck file
I would like to perform validation in some of my input components such as
I would like to read/write encrypted XML files using LINQ to XML. Does anyone
I have found C++ FQA Lite very edificatory and would like to read more
I would like to test a string containing a path to a file for
I would like emacs to mark files that are generated as read-only when they're
I would like to remove/delete a migration file. How would I go about doing

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.