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

Recommended Answers

All 7 Replies

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

when use it ?
before or after getline(infile,word,' ');

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;
commented: nice & clean example +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.