-1

I am scanning strings as input , i am using getline to do so e.g

char *lajna=NULL;
size_t dlzka=0;
getline(&lajna,&dlzka,stdin);

and i want to read first char using fgetc , i tried to do

test=fgetc(lajna);

but it throws error

cannot convert ‘char**’ to ‘FILE* {aka _IO_FILE*}’ for argument ‘1’ to ‘int fgetc(FILE*)’ ciarka=fgetc(&lajna);

i checked it up and found nothing how to read chars from buffer like this, what is the right way?

Johnyb
  • 980
  • 1
  • 12
  • 27

1 Answers1

2

See the prototype of fgetc():

   int fgetc(FILE *stream);

It takes a FILE* as argument but you are passing char*. Hence, the error. (The error message suggests you actually have it like: test=fgetc(&lajna);)

To read characters from lajna, you don't need to use any function or special mechanism. You can simply index into it:

char ch = lajna[0]; // first char 

and so on.

Or you can use a loop to read all chars.

for(i=0; lajna[i]; i++) {  //until the null terminator '\0'
   char ch = lajna[i];
}
P.P
  • 117,907
  • 20
  • 175
  • 238
  • i would like to use sscanf on that buffer , and i need to get rid of first character. – Johnyb Jan 22 '16 at 21:11
  • 1
    Not sure why you would need to use `sscanf()`. If you simply to ignore the first char then do: `char *str = lajna +1;` and use `str`. If you want to make a *copy* then use: `char buf[dlzka]; strcpy(buf, lajna+1);` – P.P Jan 22 '16 at 21:15
  • If you want to use `sscanf` and ignore the first character one option is: `sscanf(&lajna[1], ...)`. This assumes `lajna` has at least one character (and the NUL terminator). Or `sscanf(lajna, "%*c%s", ..)`, the `*` format specifier means parse but ignore that input. – kaylum Jan 22 '16 at 21:19
  • this does not help my code at all , is there any way how to use fgetc on that buffer? the "%*c" mess up it pretty badly. – Johnyb Jan 22 '16 at 21:33
  • "is there any way how to use fgetc on that buffer?" - No. It's *not* possible. You are asking something that doesn't make sense. `fgetc()` reads from a *stream*, not from a buffer. `sscanf()` can be used to read a char but that's pointless to a single character from a buffer. – P.P Jan 22 '16 at 21:35