Remove leading space in the line of a file

I have a file say A.txt that has this line of text
"------10 spaces-------The big brown fox".

It has to be C and not C++
I wrote this code
(fgets (oneLine, LINE_LENGHT ,fp )) where
one line is char oneLine[128}
LINE_LENGHT = 128
fp is a FILE pointer

and I got this
oneLine = "------10 spaces-------The big brown fox".

but this is not what I wanted, I wanted to get rid of the leading space and get this.
oneline = "The big brown fox"

It looks simple but I tried all variation of scanf and failed.

What am I overlooking?
Last edited on
1
2
3
size_t n = strspn( oneLine, " \t" );

if ( n != 0 ) strcpy( oneLine, oneLine + n );
Last edited on
Typically this would not be a good use case for scanf, however:
1
2
3
4
5
6
7
8
9
10
11
#include <cstdio>

int main()
{
    char buffer[128] = {} ;
    while ( buffer[0] != 'q' && buffer[0] != 'Q' )
    {
        std::scanf( " %127[^\n\r]", buffer ) ;
        std::printf( "%s\n", buffer ) ;
    }
}


Note that the line terminator is left in the input stream after the scanf call.
vlad from moscow,

I run your code and it works. I am unfamiliar with "strspn" so I went to www.cplusplus.com and tried to figure it out. I am a little lost why you chose
" \t".

Thanks,
Ed
Topic archived. No new replies allowed.