简体   繁体   中英

how to convert ifstream to istringstream

I have a function that reads through an istringstream and does some operations on it so ... I am trying to read in a file using fstream and convert the fstream object to an istringstream in order to pass it to my function. I don't know how to do this. Any help would be much appreciated. I am still pretty much a beginner so please keep it simple if you can.

string inputFile(argv[1]);    
ifstream inFile(inputFile.c_str());    
istringstream is;

is >> inFile.rdbuf(); //*******This is wrong and is what I'm having trouble with

function(is);

What you want is std::istream_iterator :

std::string myString ( std::istream_iterator(inputFile) , std::istream_iterator() ) ;
std::istringstream is ( myString ) ;

This creates a string with the contents of inputFile by using a begin iterator ( std::istream_iterator(inputFile) ) and an end iterator ( std::istream_iterator() ). It then creates an istringstream from that string.

This solves your immediate problem, but there is almost certainly a better way to do what you are trying to do. For example:

  • Pass an istream instead of an istringstream (because then you don't have to copy it).

  • Change your algorithm to not need an istringstream .

You cannot convert a std::ifstream directly to a std::istringstream .

However, your function that uses a std::istringstream should simply be rewritten to use a std::istream instead, so that it can be used both with std::istringstream and std::ifstream .

If you really want to take a std::ifstream , and get a std::istringstream , you should use std::istreambuf_iterator to construct a temporary std::string , which is the only parameter type that a std::istringstream accepts. But that's bad design. Use a std::istream , and pass the std::ifstream directly.

Stringstream cant take from a file, it can only take from a string and give out to a string. Make a string to hold the information from the file using getline/etc then feed that into the stringstream. Also make sure to clear the stringstream after using it ex. Stringstreamname.clear().

Not sure exactly what you need to do, but going beyond your direct question, I will bet that you want to read in a line at at time, even if you don't know that yet.

In C++, if you read in anything that does not fit, the input gets "stuck." For example, suppose input is mistakenly:

4 4x

The x is a typo.

If you read it:

int a,b;
cin >> a >> b;

a and b will both be 4. But the next time you read, it will fail because of the x.

Instead, read in each line into a string:

#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;

int main() {
    ifstream f("input.dat");
    string line;

    getline(f, line);
    istringstream s(line);
    int a,b;
    s >> a >> b;
    cout << (a + b) << '\n';
}

And of course, as others have said, you should write your code in terms of reading from istream, independent of where it is coming from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM