I am trying to read in names and telephone numbers from a text file using fscanf.

The format of the data is:

9073234 Colwer, Mrs J.
3823823 Le Boeuf, Mr S.

To do this I am using the following line:

fscanf(fpp,"%d %s %[^\n]s",&a,surnames,firstnames);

This works except for "Le Boeuf" where it stores "Le" as the surname and "Boeuf, Mr S" in the first name (it should have "Le Boeuf" as the surname and "Mr S" as the firstname). Someone helped me with the above line, and so I don't really understand what the [^\n] does. The point is that %[^\n]s seems to allows spaces but %s doesn't. What I need is for anything before the comma to be included in the surname, even if there is a space in the surname. Can anyone help me with amending the line? I want to keep using fscanf if possible.

Many thanks

7 Years
Discussion Span
Last Post by milkz

The %[] specifier is a scan set. scanf will look for everything between the brackets and act accordingly. If the scan set begins with a ^ character, it means to exclude the characters in the scan set, otherwise include them. So %[^\n] means to read and store everything up to a newline in the corresponding string parameter. You can use the same trick to stop at a comma: %[^,] , but keep in mind that the delimiting character is not extracted, so you need to remove it manually by using a literal in the format string:

#include <stdio.h>

int main(void)
    unsigned long id;
    char last_name[10];
    char first_name[10];

    while (scanf("%d %[^,], %[^\n]", &id, last_name, first_name) == 3)
        printf(">%s< >%s<: %d\n", first_name, last_name, id);

    return 0;

String ends at space so it wont scan countinued name after space..

fscanf(pt,"%d %[^,]s ",&num,surname);

Narue posted just b4 me .. U scan lk tht and later remove the first char (,) or else you can scan a single char and discard that char ..


Hello, similar question over here:

trying to read this from a file:


This is my guess. Can anyone please tell me where I am going wrong and explain how to use [^;] and why ; still needs to be included?

while (fscanf (fp, "%d%[^;];%s[^;];%s, num, aa, bb) != EOF)


The number and first string are separated by ';', yet your format string doesn't take that into account. Consider "%d;%[^;];%s[^;];%s" instead.

>why ; still needs to be included?
Because %[^;] doesn't remove it.


Once again thank you Narue, you are on fire. I am trying my best to put this logic together by using different examples:


%d; - clearly starts off beginning up to ';' removes it.
%[^;];%s - start from after ; up to ; and remove ;??
[^;];%s- you didnt include % infront this time why?, start from ; till ;??

1000; - %d; works but if i use hello; - %s; wont get rid of the ;

Another example how would i extract everything but ; and ,:

once again, thanks a lot dont know what my assignments would look like without you.


My question is what does [^;], %[^;], and ; mean?

i added:

tried "%d;%[^;];%s[^;];%s[^;];%s", &num, aa, bb, cc

Followed exact same pattern and it wont work, really how come i cant grasp this its very upsetting.


Okay after spending countless hours screaming at my computer i figured this out. For guys having same problem as me:

fscanf (file, "%[^;];" , sentance); - sentance="hellodudeman"
if you're still confused %[^;] is used instead of %s.


while (4 == fscanf (file, "%d;%[^;];%[^;];%s", &num, word1, word2, word3)){
printf ("%d %s %s %s %s", num, word1, word2, word3)

output: 100 dude man sup

if file looked like this 1000;dude;man;sup;

%d; - now dude;man;sup; is left
%[^;]; - now man;sup; is left
%[^;]; - now sup; is left
%[^;]; - \0

hope that breaks it down for guys like me spending hours trying to figure it out.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.