0

Hi friends,


I'm new to this community. i just want to read string from a file excluding punctuations like ',' ';' or newline character. Is there any way to read the string by specifying the terminate character.

2
Contributors
8
Replies
10
Views
6 Years
Discussion Span
Last Post by Martin B
Featured Replies
0

There isn't a built in way of doing this. You would need to write a parser.

1. Read each line of the file into a buffer using fgets (assuming you know the maximum line length).
2. Walk through the string until you come to the next separator, and copy what you've read into a buffer.

0

There isn't a built in way of doing this. You would need to write a parser.

1. Read each line of the file into a buffer using fgets (assuming you know the maximum line length).
2. Walk through the string until you come to the next separator, and copy what you've read into a buffer.

Thanks 4 ur reply. Yeah, thats i know.Is it possible to use fscanf to read the string using format specifier like ^[a-z] or ^[A-Z]?? i've come across the net about this fscanf thats why i'm asking. Is there anyway to read the string using fscanf for my requirment??

1
unsigned int eof = 0;
while(fscanf(fptr, "%[^;]", str) && !eof) {
    printf("%s\n", str);
    fgetc(fptr);
    eof = feof(fptr);
}

Edited by Martin B: n/a

Comments
Good
0
unsigned int eof = 0;
while(fscanf(fptr, "%[^;]", str) && !eof) {
    printf("%s\n", str);
    fgetc(fptr);
    eof = feof(fptr);
}

Hi Martin,

How can i specify more than one character in %[^;]? I want to include the character like '{' and '\n'.

0

You just add them inside the square brackets after the ^.

[^;{\n]

Thanks martin, Could u please explain elaborate about this ^[...] , [^..] function using some more special character and program. I tried [^;}\n] but it terminated when it find '\n' but i mentioned '}' before '\n' and the '}' is present last of my file.So i don't understand.

0

The ^ means "none of these", so [^;}\n] should cause it to stop reading the string when it encounters a ;, } or \n.

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.