Can't erase newlines from content file (text)

Viewed 45

I have this code:

struct Config parse_config(const std::string &str)
{
    struct Config config;
    std::ifstream file_stream;

    try {
        file_stream.open(str.c_str(), std::ifstream::in);
    } catch (const std::ifstream::failure &e) {
        std::cerr << e.what();
    }

    std::stringstream stream;
    stream << file_stream.rdbuf();
    std::string text(stream.str().c_str());

    text.erase(std::remove(text.begin(), text.end(), '\n'), text.end());
    std::cout << text << std::endl;
    return config;
}

This is supposed to erase all newline chars in my string which is the content of my config file so then I can parse it properly.

My config file is this

user       www;

http {
  index    index.html index.htm index.php;
  client_max_body_size 100M;

  server { # php/fastcgi
    listen       80;
    server_name  domain1.com www.domain1.com;

    location ~ \.php$ {
      fastcgi_pass   127.0.0.1:1025;
    }
  }

  server { # simple reverse-proxy
    listen       80;
    server_name  domain2.com www.domain2.com;

    # serve static files
    location ~ ^/(images|javascript|js|css|flash|media|static)/  {
      root    /var/www/virtual/big.server.com/htdocs;
      expires 30d;
    }

    # pass requests for dynamic content to rails/turbogears/zope, et al
    location / {
      proxy_pass      http://127.0.0.1:8080;
    }
  }

  server { # simple load balancing
    listen          80;
    server_name     big.server.com;

    location / {
      proxy_pass      http://big_server_com;
    }
  }
}

But anytime I try a possible way to erase the newlines this is the result

~$ g++ ./srcs/parser/parser.cpp ./srcs/server.cpp -o parser
} } } proxy_pass      http://big_server_com;ails/turbogears/zope, et al

I've already tried this

int i;
while ((i = text.find_first_of('\n')) != std::string::npos)
{
    std::cout << i << std::endl;
    text.erase(i, 1);
}

And this

for (std::string::iterator i = text.begin(); i < text.end(); i++) if (*i == '\n')
    i = text.erase(i);

And I always get the same result

My hypothesis is that the string.erase() method does not do the same as the vector.erase() one. So I'm putting '\0' characters in each '\n'. But I've searched all along stackoverflow and everyone does it the way I do. So maybe the problem is not with the erase method but with the ifstream or stream itself. Actually I don't know

0 Answers
Related