how to read a string from a \n delimited file
Asked Answered
E

5

6

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

Elation answered 27/4, 2010 at 3:52 Comment(0)
D
2

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.

Danilodanio answered 27/4, 2010 at 3:59 Comment(2)
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.Asthenopia
@Adam Good points. The accepted answer above is also safer for buffer overflow errors.Danilodanio
N
6
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.

Null answered 27/4, 2010 at 3:58 Comment(0)
C
3

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.

Crossruff answered 27/4, 2010 at 3:54 Comment(2)
This works weirdly. it doesn't read every line. and each string instead of ending in \0 ends in \nElation
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.Amie
S
3

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.)

Stronghold answered 27/4, 2010 at 4:2 Comment(0)
D
2

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.

Danilodanio answered 27/4, 2010 at 3:59 Comment(2)
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.Asthenopia
@Adam Good points. The accepted answer above is also safer for buffer overflow errors.Danilodanio
H
-1

intially send data using Payload_ID 00 01 02 03 ....10

use

char *pChar="" ; //for string capturing.

fprintf(fp1, "%s", strtok(pChar,"Payload_ID"));

Haematopoiesis answered 4/5, 2017 at 6:19 Comment(1)
here Payload_ID is the delimiter you can specifyHaematopoiesis

© 2022 - 2024 — McMap. All rights reserved.