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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:25:56+00:00 2026-05-25T15:25:56+00:00

As my learning, I am trying to use c++ ifstream and its operator>> to

  • 0

As my learning, I am trying to use c++ ifstream and its operator>> to read data from a text file using code below. The text file outdummy.txt has following contents:

just dummy
Hello ofstream
555

My questions is how to read char data present in the file into a char array or string. How to do this using the ifstream::operator>> in code below.

#include <iostream>
#include <fstream>

int main()
{
    int a;
    string s;
    char buf[100];
    ifstream in("outdummy.txt",ios_base::in);


    in.operator>>(a); //How to read integer? How to read the string data.??

    cout << a;

    in.close();
    getchar();
    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-25T15:25:56+00:00Added an answer on May 25, 2026 at 3:25 pm

    If you want to use formatted input, you have to know in advance what data to expect and read it into variables of the according data type. For example, if you know that the number is always the fifth token, as in your example, you could do this:

    std::string s1, s2, s3, s4;
    int n;
    
    std::ifstream in("outdummy.txt");
    
    if (in >> s1 >> s2 >> s3 >> s4 >> n)
    {
      std::cout << "We read the number " << n << std::endl;
    }
    

    On the other hand, if you know that the number is always on the third line, by itself:

    std::string line;
    
    std::getline(in, line);  // have line 1
    std::getline(in, line);  // have line 2
    std::getline(in, line);  // have line 3
    
    std::istringstream iss(line);
    
    if (iss >> n)
    {
      std::cout << "We read the number " << n << std::endl;
    }
    

    As you can see, to read a token as a string, you just stream it into a std::string. It’s important to remember that the formatted input operator works token by token, and tokens are separated by whitespace (spaces, tabs, newlines). The usual fundamental choice to make is whether you process a file entirely in tokens (first version), or line by line (second version). For line-by-line processing, you use getline first to read one line into a string, and then use a string stream to tokenize the string.


    A word about validation: You cannot know whether a formatted extraction will actually succeed, because that depends on the input data. Therefore, you should always check whether an input operation succeeded, and abort parsing if it doesn’t, because in case of a failure your variables won’t contain the correct data, but you have no way of knowing that later. So always say it like this:

    if (in >> v) { /* ... */ }            // v is some suitable variable
    else { /* could not read into v */ }
    
    if (std::getline(in, line)) { /* process line */ }
    else { /* error, no line! */ }
    

    The latter construction is usually used in a while loop, to read an entire file line by line:

    while (std::getline(in, line)) { /* process line */ }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Scala and trying to use javax.cache in Scala code and can't find
I am learning php, trying to use the fopen() function. The php file I
I'm just learning ruby and trying to understand the scope of code executed in
I've been learning C#, and I'm trying to understand lambdas. In this sample below,
I'm learning C++ and i'm getting some troubles when i'm trying to use a
I am learning RoR and trying to use accepts_nested_attributes_for and has_and_belongs_to_many to submit information
I'm just starting learning WPF and I'm trying to use a GridViewRowPresenter inside of
I am learning java. I am trying to use composite design pattern. I am
I just started learning Prism and trying to use it with MEF in a
So I just started learning haskell and I'm trying to use this if statement:

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.