0

I've made a simple program. I'm reading a text file in the same folder as the program. The file only has one line: " v 1.0 2.0 3.0".

Problem:

When I initialize a stringstream instance ss with a string line, and I use the erase() function to try to remove the character 'v' from the string, it is not working. The MSVC consoles shows the same line of string:

image

If I remove the .erase(0,1) function, the output is the same.

How could this happen? It should remove the character v. I learn this through the OpenGL's obj loader tutorial, and they can remove it.

Code

#include <sstream>
#include <fstream>
#include <iostream>

int main()
{
    std::ifstream filestream("textfile.txt", std::ios::in);
    std::string line = "";

    while (!filestream.eof())
    {
        std::getline(filestream, line);
        std::cout <<"getline std::string "<< line.c_str()<<std::endl;
        std::stringstream ss(line.erase(0,1));
        std::cout << "stringstream: " << ss.str() << std::endl;
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
pg daszzz
  • 49
  • 5
  • 8
    To me it looks like there is a leading whitespace character before the `v` which does get removed – UnholySheep Feb 15 '23 at 15:35
  • I tried, will put the test below – pg daszzz Feb 15 '23 at 15:37
  • 2
    Don't miss https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – 273K Feb 15 '23 at 15:37
  • 3
    It is a good idea to surround strings with visible delimiters when you want to inspect them visually. Try `std::cout << std::quoted(line) << std::endl;` – molbdnilo Feb 15 '23 at 15:38
  • 1
    Seems fine here when `v` is the first character of the string. I suspect in your case it is not. Add delimiters around what you print to accurately visualize whitespace. https://godbolt.org/z/6jdzsqacG As a side note, `.c_str()` is not necessary when printing a string using iostreams. – Retired Ninja Feb 15 '23 at 15:40
  • Please, do not answer in the question section. Rollbacked edit. – YSC Feb 15 '23 at 16:46

1 Answers1

0

After some testing, I found the reason. I made a low level mistake. I should use a text editor that can show all the hiden blank spaces.

It looks like that there is an empty character since the initializtion of stringstream ss. And if I remove one more characters, then it is okay.

int main()
{
    std::ifstream filestream("textfile.txt", std::ios::in);
    std::string line;

    while (!filestream.eof())
    {
        std::getline(filestream, line);
        std::cout <<"getline std::string"<< line.c_str()<<std::endl;
        std::stringstream ss(line.erase(0,2));
        
        std::cout << "stringstream:" << ss.str() << std::endl;
    }
}

Is it possible that book writer of the opengl book made a typo? They use erase(0,1) for "v" for vertices data, and then use erase(0,2) for "f" for the face data

That is the mistake for not using a good text editor

Joshua Ooi
  • 1,139
  • 7
  • 18
pg daszzz
  • 49
  • 5
  • 1
    Maybe it's your file that is off, and not the book. The author of the book has no idea what you may have done to the text file. – PaulMcKenzie Feb 15 '23 at 15:45
  • 1
    Replace `while (!filestream.eof())` with `while (std::getline(filestream, line))` and read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). When you find a good editor, I recommend Notepad++, check if your file has a BOM. – Retired Ninja Feb 15 '23 at 16:22