tags:

views:

2641

answers:

3
A: 

If you're doing lots of text processing, maybe C is not the best option... have you considered switching to AWK, Perl, Python, TCL,...? And, if your requirements are not stable as you say, using a dynamic language will be much more productive than C.

Of course, if you definitely need to do things in C, you can still embed some of those languages in C or call your C functions from them.

fortran
Thanks for your suggestion. But I prefer Compiling languages like C than Scripting languages like Perl which needs interpreter on user machine. I dont know Awk,TCL. So for the time being i fixed for C which si my favourite ( I have been using C as a parser language, text processor for my daily use from my early programming days). But this was a good suggestion. I dont know why it was downvoted. Thnks once again
Enjoy coding
Don't worry about the downvote, I don't care actually xD It's good to master a language above others, but sometimes it's very convenient to be flexible enough to change when the situation demands it...
fortran
+2  A: 
fscanf(stream, "%42[^\n]", buffer);

is an equivalant of fgets(buffer, 42, stream). You can't replace the 42 by * to specify the buffer length in the argument (as you can do in printf), its meaning is to suppress the assignment. So

fscanf(stream, "%*[^\n]%*c");

read upto (and included) the next end of line character.

Any conversion specifier other than [, c and n start by skipping whitespaces.

AProgrammer
I don't think you want the * in there, that's the suppression character. Also, you can't assign anything to sizeof(buffer). I think you meant something like:fscanf(stream, "%[^\r\n]%n%*[\r\n]", buffer, The first pattern matches all characters except CR/LF and puts them into buffer. Then %n gives the number of characters read and puts that into bufferSize. Then the last part reads all CR/LF characters and discards them. Although this doesn't account for empty lines.It's probably better to just use fgets.
CodeGoat
@CodeGoat I remembered about the suppression character just after submitting and then edited my post. I wouldn't use \r in fscanf. Either you are doing binary input then consideration about line ends would be unusual, or you are doing text input, and the CR-LF pair is mapped to \n by the runtime.
AProgrammer
Good point, I always used the \r anyway without thinking about how text input mode already handles it.
CodeGoat
+1  A: 

Kernighan and Pike in the excellent book 'The Practice of Programming' show how to use sprintf() to create an appropriate format specifier including the length (similar to the examples in the answer by AProgrammer), and then use that in the call to scanf(). That way, you can also control the separators. Concerns about the 'inefficiency' of this approach are probably misguided - the alternatives are harder to get right.

That said, I most normally do not use the scanf() family of functions for file I/O; I get the data into a string with some sort of 'get line' routine, and then use the sscanf() family of functions to split it up - or other more specialized parsing code.

Jonathan Leffler