Pregunta

I am trying to do something that I imagine is quite straightforward but I am new to C/C++ so it is proving a little bit tricky. Essentially I am trying to remove a single whitespace from data contained within a .txt. Each piece of data is on a separate line:

01011 0
11100 1
00001 0

and so on. I have been able to count the number of lines, and the size of each string (including the whitespace) however I want to lose the whitespace located within the data. My code for reading the data in (including whitespace is as follows):

std::ifstream myfile ("random.txt");
std::string str; 
if(myfile.is_open())
                {
                while (std::getline(myfile, str))
                    {
                    i++;
                    Size = str.size();
                    data_input[i] = str;
                    line_num = i;
                    array_count = line_num * Size;
                    } 
                i = 0;
                }

I have looked at various other posts but can't seem to find one that fits what I am trying to achieve. Any help would be appreciated.

¿Fue útil?

Solución

str.erase(str.find(' '), 1);

Explanation:

  1. The call to str.find returns the position (index) of the space.
  2. The call to str.erase removes one character, starting at that position.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top