Home > database >  what does std::string str(size, '\0'); mean in c ?
what does std::string str(size, '\0'); mean in c ?

Time:01-24

https://en.cppreference.com/w/cpp/io/basic_istream/read

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdint>
 
int main()
{
    // read() is often used for binary I/O
    std::string bin = {'\x12', '\x12', '\x12', '\x12'};
    std::istringstream raw(bin);
    std::uint32_t n;
    if(raw.read(reinterpret_cast<char*>(&n), sizeof n))
        std::cout << std::hex << std::showbase << n << '\n';
 
    // prepare file for next snippet
    std::ofstream("test.txt", std::ios::binary) << "abcd1\nabcd2\nabcd3";
 
    // read entire file into string
    if(std::ifstream is{"test.txt", std::ios::binary | std::ios::ate}) {
        auto size = is.tellg();
        std::string str(size, '\0'); // construct string to stream size
        is.seekg(0);
        if(is.read(&str[0], size))
            std::cout << str << '\n';
    }
}

for example: I can read https://en.cppreference.com/w/cpp/io/basic_istream/tellg to get what does tellg do. so I also understand ate and seekg
but I don't understand std::string str(size, '\0'); // construct string to stream size
even i read: https://en.cppreference.com/w/cpp/string/basic_string

CodePudding user response:

Just a string of '\0' with the length is 'size'. std::string is not C-Style string, it could contain any data.

See this example:

#include <iostream>
#include <string>

void print_the_content(size_t size) {
  std::string str(size, '\0'); // construct string to stream size

  for (auto it = str.begin(); it != str.end();   it) {
    std::cout << static_cast<uint32_t>(*it) << ' ';
  }
  std::cout << std::endl;
}

#define TEST_CNT 5
int main(int argc, char **argv) {
  for (auto i = 0; i < TEST_CNT;   i) {
    std::cout << std::endl << "Now test counter: " << i << std::endl;
    print_the_content(i);
  }

  return 0;
}
g   test.cpp
./a.out

The output:


Now test counter: 0


Now test counter: 1
0

Now test counter: 2
0 0

Now test counter: 3
0 0 0

Now test counter: 4
0 0 0 0

CodePudding user response:

https://en.wikipedia.org/wiki/Null-terminated_string

NUL-terminated string is stored in an array, and the string itself consists of all the characters in an array prior to the first '\0' character (called a NUL in ASCII) https://www.tutorialspoint.com/ascii-nul-ascii-0-0-and-numeric-literal-0

Here we will see the ASCII NUL, ASCII 0 and the Numeric Literal 0. The ASCII null is represented as 0x00, and zero is represented as 0x30. The ASCII NUL character is used to denote the end of the string in C or C .

for example:

 string a1= {'H','e','l','l','o','\0','i','d','1','0','t'};
        cout<<a1;

will return Hello id10t. So

string a(5,'\0');
    cout<<a.size()<<endl;

will construct a string with 5 characters, ended by null-terminated string. based on

Constructs the string with count copies of character ch. This constructor is not used for class template argument deduction if the Allocator type that would be deduced does not qualify as an allocator https://en.cppreference.com/w/cpp/string/basic_string/basic_string

  •  Tags:  
  • Related