Hello I need a better explanation of the fscanf parameter. I have the following text file
this is line 1
this is line 2
this is line 3
...
read out i do with
for(int i=0;i<2;++i){
test[255];
fscanf(fp,"%[\n]",test);
printf("%s\n",test);
}
now I get:
this is line 1
this is line 1
with "%[^\n]\n"
I get
this is line 1
this is line 2
now I break my statement apart as far as I understand it: % means read it unformated (%s would give me a string %c single character...) [^\n] until you get something that does not match in this case newline
Could you explain me the function of the square brackets better and the termination. I read the official explanations but don't understand them fully.
extension 1: of my question. I am aware that there are more easy to use options to achieve my goal. But I just try to understand the syntax of fscanf.
extension 2: when I understand it right
fscanf(fp,"%[^\n]%*c",test)
reads until newline and skips the next character which IS the newline. Following this logic %[^\n] would be every character except newline. I could write
for(int i=0;i<2;++i){
test[255];
fscanf(fp,"%[a-z]",test);
printf("%s\n",test);
}
and I would expect to get
this
is
But I get
ٷ�
ٷ�
extension 3 question is not duplicate to scanf() leaves the new line char in the buffer as i want to read a complete line