hello for all
i read lines from file as : getline(infile,word);
but i want put condition :don't read the spaces that in begin of lines

this problem appear with me now when i end my project :'(
please help me

You can add this to your code.

word.substr(word.find('  ');

This will read until the whitespace so you can do what you want. Hope that helps

You use it after the getline. I dont quite get your question but after find the whitespace then maybe you can read the next line and so on.

i read the file line by line
then store the lines in strings
but the space that is in the begin of line i want remove it before read the line and store it in the strings

and when add this statment
(word.substr(word.find(' '))
,appear problem in the run (abnormal program termnation)

i read the file line by line
then store the lines in strings
but the space that is in the begin of line i want remove it before read the line and store it in the strings

Does the following make things clearer ?

std::string line;

// read a whole line ...
while(getline(infile, line))
{
    // the variable [B]line[/B] contains the whole line at this point

    // insert code here to trim the leading whitespace
    
    // insert code here to save the trimmed line
}

and when add this statment
(word.substr(word.find(' '))
,appear problem in the run (abnormal program termnation)

Here is one way to trim leading whitespace

string test_string = "    test_string";
    string ws = " \t";

    // lookup the position of the first non-whitespace char, if any
    std::string::size_type pos = test_string.find_first_not_of(ws);

    if(std::string::npos == pos)
    {
        // string contains only whitespace
    }
    else
    {
        // drop the leading whitespace
        test_string.erase(0, pos);
    }

    cout << "[" << test_string << "]" << endl;
Comments
nice & clean example
This article has been dead for over six months. Start a new discussion instead.