"\"%S\"" - It dosen't work!

Started by
3 comments, last by MichaelNolan 21 years ago
Hey, I have a text file, e.g. 123 125 127 "Hello" World I want to read in the "Hello" part of it after reading in the numbers, char *MySrting ; fscanf(TheFile, "\"%s\"", &MyString); Result: MyString = "" Why?
Mike
Advertisement
Reading strings with scanf works like this:
char buf[256];scanf("%s", buf); 
in case you didn''t understand .. the problem with your code is ONE TOO MANY levels of indirection on your character buffer ...

a char * is already the right type to pass to scanf and fscanf ... so you DO NOT want to use the & operator as well ..
There''s another problem. You need to allocate some space for MyString to hold the string that''s scanned in. fscanf won''t modify MyString or allocate any space for you, it just modifies whatever MyString points to (and right now it doesn''t point at anything useful).
make sure you read in the \n part too (or \r\n on windows)

This topic is closed to new replies.

Advertisement