简体   繁体   中英

storing text to string from a file that contains spaces

So I've been doing algorithms in C++ for about 3 months now as a hobby. I've never had a problem I couldn't solve by googleing up until now. I'm trying to read from a text file that will be converted into a hash table, but when i try and capture the data from a file it ends at a space. here's the code

#include <iostream>
#include <fstream>

int main()
{
    using namespace std;
    ifstream file("this.hash");
    file >> noskipws;

    string thing;
    file >> thing;
    cout << thing << endl;

    return 0;
}

I'm aware of the noskipws flag i just don't know how to properly implement it

When using the formatted input operator for std::string it always stops at what the stream considers to be whitespace. Using the std::locale 's character classification facet std::ctype<char> you can redefine what space means. It's a bit involved, though.

If you want to read up to a specific separator, you can use std::getline() , possibly specifying the separator you are interested in, eg:

std::string value;
if (std::getline(in, value, ',')) { ... }

reads character until it finds a comma or the end of the file is reached and stores the characters up to the separator in value .

If you just want to read the entire file, one way to do is to use

std::ifstream in(file.c_str());
std::string   all((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());

I think the best tool for what you're trying to do is get , getline or read . Now those all use char buffers rather than std::string s, so need a bit more thought, but they're quite straightforward really. (edit: std::getline( file, string ) , as pointed out by Dietmar Kühl, uses c++ strings rather than character buffers, so I would actually recommend that. Then you won't need to worry about maximum line lengths)

Here's an example which will loop through the entire file:

#include <iostream>

int main () {
    char buffer[1024]; // line length is limited to 1023 bytes

    std::ifstream file( "this.hash" );

    while( file.good( ) ) {
        file.getline( buffer, sizeof( buffer ) );
        std::string line( buffer ); // convert to c++ string for convenience
        // do something with the line
    }

    return 0;
}

(note that line length is limited to 1023 bytes, and if a line is longer it will be broken into 2 reads. When it's a true newline, you'll see a \\n character at the end of the string)

Of course, if you a maximum length for your file in advance, you can just set the buffer accordingly and do away with the loop. If the buffer needs to be very big (more than a couple of kilobytes), you should probably use new char[size] and delete[] instead of the static array, to avoid stack overflows.

and here's a reference page: http://www.cplusplus.com/reference/fstream/ifstream/

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