Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read and write a STL C++ string?

Tags:

c++

string

io

stl

#include<string>
...
string in;

//How do I store a string from stdin to in?
//
//gets(in) - 16 cannot convert `std::string' to `char*' for argument `1' to 
//char* gets (char*)' 
//
//scanf("%s",in) also gives some weird error

Similarly, how do I write out in to stdout or to a file??

like image 327
Moeb Avatar asked Apr 11 '10 12:04

Moeb


2 Answers

You are trying to mix C style I/O with C++ types. When using C++ you should use the std::cin and std::cout streams for console input and output.

#include <string>
#include <iostream>
...
std::string in;
std::string out("hello world");

std::cin >> in;
std::cout << out;

But when reading a string std::cin stops reading as soon as it encounters a space or new line. You may want to use getline to get a entire line of input from the console.

std::getline(std::cin, in);

You use the same methods with a file (when dealing with non binary data).

std::ofstream ofs("myfile.txt");

ofs << myString;
like image 181
Yacoby Avatar answered Sep 30 '22 04:09

Yacoby


There are many way to read text from stdin into a std::string. The thing about std::strings though is that they grow as needed, which in turn means they reallocate. Internally a std::string has a pointer to a fixed-length buffer. When the buffer is full and you request to add one or more character onto it, the std::string object will create a new, larger buffer instead of the old one and move all the text to the new buffer.

All this to say that if you know the length of text you are about to read beforehand then you can improve performance by avoiding these reallocations.

#include <iostream>
#include <string>
#include <streambuf>
using namespace std;

// ...
    // if you don't know the length of string ahead of time:
    string in(istreambuf_iterator<char>(cin), istreambuf_iterator<char>());

    // if you do know the length of string:
    in.reserve(TEXT_LENGTH);
    in.assign(istreambuf_iterator<char>(cin), istreambuf_iterator<char>());

    // alternatively (include <algorithm> for this):
    copy(istreambuf_iterator<char>(cin), istreambuf_iterator<char>(),
         back_inserter(in));

All of the above will copy all text found in stdin, untill end-of-file. If you only want a single line, use std::getline():

#include <string>
#include <iostream>

// ...
    string in;
    while( getline(cin, in) ) {
        // ...
    }

If you want a single character, use std::istream::get():

#include <iostream>

// ...
    char ch;
    while( cin.get(ch) ) {
        // ...
    }
like image 39
wilhelmtell Avatar answered Sep 30 '22 04:09

wilhelmtell