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.

Recommended Answers

All 8 Replies

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.

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??

fscanf(fptr, "%[^;]", str);
unsigned int eof = 0;
while(fscanf(fptr, "%[^;]", str) && !eof) {
    printf("%s\n", str);
    fgetc(fptr);
    eof = feof(fptr);
}
commented: 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'.

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

[^;{\n]

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.