views:

137

answers:

4

I'm trying to read a return delimited file. full of phrases.

I'm trying to put each phrase into a string.

The problem is that when I try to read the file with

fscanf(file,"%50s\n",string);

the string only contains one word. when it bumps with a space it stops reading the string

+3  A: 

fscanf with %s stops reading when it finds whitespace.

Since you are reading unformatted text, you can simply use fgets, which reads until it fills the buffer you give it, it finds a newline (\n), or it reaches the end-of-file, whichever comes first.

James McNellis
This works weirdly. it doesn't read every line. and each string instead of ending in \0 ends in \n
maty_nz
Each string still ends in `'\0'`, but the last character before that is usually `'\n'`, since it reads in the line exactly as it appears in the file. If the `'\n'` is absent then it means either that your buffer wasn't big enough for the entire line, or that you just read the last characters in the file and it didn't finish with a newline.
caf
+3  A: 
fscanf(file,"%50[^\n]\n",string);
  1. Every character except \n will be consumed by [^\n]

  2. Maximum 0f 50 chars will be consumed (make sure string has space for 51 atleast)

  3. ..\n",string this makes sure that \n is also consumed so that the next call does not just return a null string.

N 1.1
this works great, Thanks
maty_nz
+1  A: 

fscanf can be modified to read past spaces. The details are a bit complicated. Here is what the man page says about %[...]

Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char, and there must be enough room for all the characters in the string, plus a terminating NUL character. The usual skip of leading white space is suppressed. The string is to be made up of char-acters in (or not in) a particular set; the set is defined by the characters between the open bracket [ character and a close bracket ] character. The set excludes those characters if the first character after the open bracket is a circumflex ^. To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set. The hyphen character - is also special; when placed between two other characters, it adds all intervening characters to the set. To include a hyphen, make it the last character before the final close bracket. For instance, `[^]0-9-]' means the set ``everything except close bracket, zero through nine, and hyphen''. The string ends with the appearance of a character not in the (or, with a circumflex, in) set or when the field width runs out.

So, %[^\n] should read everything up to the carriage return.

Nathan S.
It reads everything up to the newline but *not* the newline itself. If you loop on `fscanf(file, "%[^\n]")`, you'll just keep getting the empty string over and over again. You need to consume the newline with either a whitespace in the format string, or something like `%*c`.
Adam Rosenfield
Thanks this is it.
maty_nz
@Adam Good points. The accepted answer above is also safer for buffer overflow errors.
Nathan S.
+2  A: 

Avoid using scanf. As already mentioned, you should use fgets instead.

If you don't want to use a fixed-size buffer and to allow lines of arbitrary length, you can try using Chuck Falconer's public domain ggets function. (That link seems to be down right now, but archive.org has a copy.)

jamesdlin