Handling text with C?

T

Tatu Portin

Is there any convenient way to program text-handling routines in C?

For example, I have a text file and I want to remove every other line, and then
write the output.

Or should I read tutorials about string fuctions and exercise to become an
expert in:
for (i = 0 ; i < sz ; i++) printf ("%c", buf);
kind of lines?
 
E

Eric Sosman

Tatu said:
Is there any convenient way to program text-handling routines in C?

It depends on what you consider convenient. Quite a
lot of text has been "handled" by C, with greater and
lesser degrees of difficulty.
For example, I have a text file and I want to remove every other line, and then
write the output.

Error checking omitted for brevity:

#include <stdio.h>
int main(void) {
int copy;
int ch;
for (copy = 1; ; copy ^= 1) {
do {
if ((ch = getchar()) == EOF)
return 0;
if (copy)
putchar(ch);
} while (ch != '\n');
}
}
Or should I read tutorials about string fuctions and exercise to become an
expert in:
for (i = 0 ; i < sz ; i++) printf ("%c", buf);
kind of lines?


Learn all you can about the language and its libraries,
because it sometimes happens that what you need is already
there, already tested, already debugged. For example, the
above loop can (most likely) be replaced by

printf ("%.*s", sz, buf);

This particular instance isn't an overwhelming improvement
in convenience, but it serves to show that the library may
be capable of more than you know. Learn what it can do,
and you may save yourself some work.
 
M

Malcolm

Tatu Portin said:
Is there any convenient way to program text-handling routines in C?

For example, I have a text file and I want to remove every other line, and
then write the output.
The C string library isn't ideal. Everyman and his dog has produced a new C
string library, storing the length of the string along with the character
array, so that strlen() is an O(constant) function. However none have caught
on.

If you are serious about text processing then you will find a library
somewhere that does what you want. However if you are just casual, then
you'll have to accept that functions like the above are achievable, but not
too easy, in C.

For instance you can write a routine that reads an arbitrary-sized line into
a buffer allocated with malloc(), and then it is easy to iterate over the
file removing every other line.

However in perl the same job is much simpler, and that may well be the tool
to use. C isn't ideal for everything.
 
P

Peter Shaggy Haywood

Groovy hepcat Tatu Portin was jivin' on Tue, 16 Nov 2004 18:56:09 GMT
in comp.lang.c.
Handling text with C?'s a cool scene! Dig it!
Is there any convenient way to program text-handling routines in C?

For example, I have a text file and I want to remove every other line, and then
write the output.

If that's all you want to do, then it's very easy. All you have to
do is read the file, one line at a time, perhaps using fgets(), output
the lines you want and discard those you don't.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
D

Dave Thompson

Tatu Portin wrote:
Or should I read tutorials about string fuctions and exercise to become an
expert in:
for (i = 0 ; i < sz ; i++) printf ("%c", buf);
kind of lines?


Learn all you can about the language and its libraries,
because it sometimes happens that what you need is already
there, already tested, already debugged. For example, the
above loop can (most likely) be replaced by

printf ("%.*s", sz, buf);

As long as none of buf [ 0 .. sz -1 ] is a null character. And that sz
is of type int or a type that default-promotes to int, or is or
promotes to unsigned int with a value in range of signed int;
or failing that its value is in range and you add a cast.

Alternatively it can be replaced by
fwrite (buf, /* can swap these two: */ 1, sz, stdout)
on only the condition that sz "fits in" size_t, and if it doesn't any
decent compiler can and will give you a warning.
This particular instance isn't an overwhelming improvement
in convenience, but it serves to show that the library may
be capable of more than you know. Learn what it can do,
and you may save yourself some work.

Agree there. :)


- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,154
Messages
2,570,870
Members
47,400
Latest member
FloridaFvt

Latest Threads

Top