开发者

How to read text file into deque

开发者 https://www.devze.com 2023-03-19 11:01 出处:网络
I am trying to construct a deque of strings (in C++) from a txt file by putting a new entry in the deque for each line in the file. Below is my attempt at the function - I know that the while loop is

I am trying to construct a deque of strings (in C++) from a txt file by putting a new entry in the deque for each line in the file. Below is my attempt at the function - I know that the while loop is being executed the proper number of times, however after calling this function the queue is always empty. I'm sure I'm missing something small (very new to C++ syntax and workings...), and any help is greatly appreciated.

void read_file(string file_name, deq开发者_高级运维ue<string> str_queue) {
    ifstream filestr;
    filestr.open(file_name);
    if (!filestr.is_open()){
        perror ("Error opening file.");
    }
    else {
        while (filestr) {
            string s;
            getline(filestr,s);
            str_queue.push_back(s);
        }
    }
}        


You are passing the queue by value, not by reference. Try this:

void read_file(const string &file_name, deque<string> &str_queue) {


Pass the deque<string> either by reference or pointer. You are creating a local deque which goes out of scope at end of call.


I suggest using STL to work for you (see working demo on codepad[1]); this program will replicate itself on stdout:

#include <iostream>
#include <fstream>
#include <iterator>
#include <deque>
#include <vector>

using namespace std;

struct newlinesep: ctype<char>
{
    newlinesep(): ctype<char>(get_table()) {}

    static ctype_base::mask const* get_table()
    {
        static vector<ctype_base::mask> rc(ctype<char>::table_size,ctype_base::mask());
        rc['\r'] = rc['\n'] = ctype_base::space;
        return &rc[0];
    }
};

int main()
{
    deque<string> str_queue;

    ifstream ifs("t.cpp");
    ifs.imbue(locale(locale(), new newlinesep));
    copy(istream_iterator<string>(ifs), istream_iterator<string>(), back_inserter(str_queue));
    copy(str_queue.begin(), str_queue.end(), ostream_iterator<string>(cout, "\n"));
    return 0;
}

The idea of imbuing the custom locale (with newlinesep) was borrowed from this answer: Reading formatted data with C++'s stream operator >> when data has spaces


[1] interestingly this tells us quite a bit about the implementation details of codepad.org; not only have I guess the source filename used (t.cpp) but also can we see that the source code gets modified slightly (what is prelude.h? - Perhaps it is a giant pre-compiled header to reduce the server load)


I would start with one of the answers to a previous question. In my answer there, I gave one example using a std::set and one using a std::vector, but the code should continue to work fine if you use a std::deque instead:

std::deque<std::string> lines;

std::copy(std::istream_iterator<line>(std::cin), 
          std::istream_iterator<line>(),
          std::back_inserter(lines));
0

精彩评论

暂无评论...
验证码 换一张
取 消