4

Is there a way to do this in C (or C++)?

Background info: I'm going to be reading either a memory block or a large file line by line and processing those lines one at a time and I'm lazy to write the same thing twice, once for memory blocks and once for file streams. If the file stream version needs to be used, then the file won't fit into memory. Understandably, I could save the memory block to a file, and then access the same data with a file stream, but that seams like a waste of computer time.

I know about /dev/shm on linux systems. Is there something more portable that gives me the same kind of abstraction at the layer of the language (C or C++)?

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142

3 Answers3

8

In C you may use sprintf and sscanf, in C++ there is the std::stringstream class that is constructed using a string. So you may make the function take an std::istream as argument and pass either std::ifstream of std::istringstream depending on the case.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
3

You could use the function open_memstream(3) and it's associated functions if it is available on your system. (glibc and POSIX 2008) : open_memstream(3) man page,

iain
  • 5,660
  • 1
  • 30
  • 51
2

You could write your own stream buffer, like so:

#include <streambuf>
#include <istream>
#include <iomanip>
#include <iostream>

template<
    class CharT,
    class Traits = std::char_traits<CharT>
> class basic_MemoryBuffer : public std::basic_streambuf<CharT, Traits> {
public:
  basic_MemoryBuffer(CharT* ptr, std::size_t size)  {
    setg(ptr, ptr, ptr+size);
  }
};

typedef basic_MemoryBuffer<char> MemoryBuffer;

int main () {
  char data[] = "Hello, world\n42\n";

  MemoryBuffer m(data, sizeof(data)-1);
  std::istream in(&m);

  std::string line;
  while(std::getline(in, line)) {
    std::cout << line << "\n";
  }
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308