You may be finding it difficult
to write C programs to read in strings from files.
I certainly find it difficult!
One reason for difficulty is that C
- when compiled using plain "gcc" - allows you
to do all sorts of silly things that you don't really
want to do.
However, you can, if you want, ask gcc
to "Warn" you whenever your program does things
that look silly.
The easiest way to request these warnings
is to use a Makefile to handle your compilation
command.
See
~mackay/c/Makefile.
The top entry in the Makefile,
CFLAGS = $(INCDIRS) \
-g \
-Wall -Wconversion -Wstrict-prototypes\
-Wformat -Wmissing-prototypes -Wshadow\
-Wpointer-arith -Wcast-qual -Wwrite-strings
lists all of the flags I like gcc to receive.
All the "-W..." flags are requests for warnings.
[Makefiles allow you to compile a program such as readstrings
by typing
$ make readstrings
instead of typing
$ gcc readstrings.c -lm -o readstrings -g etc etc etc.
I recommend using Makefiles whenever you have repetitive commands
to type. For example, latex Makefile.]
It is a very good habit to write C
programs that generate no warnings at all
when compiled.
Most programming errors can be
caught by using these warnings.
You can see an example program that
compiles with no warning messages in this file:
~mackay/c/readstrings.c
This is a program that reads in the
Huffman code found in /home/mackay/compress/octave/symbolcode
and prints the code back to the screen, to prove
that it worked.
In order to avoid warning messages,
we have to replace main() by
main( int argc , char *argv[] )
and add
return(0);
to the end of the main routine.
I strongly recommend using all these
warning options when you compile C programs.
It helped me make this example program work!
The example program
~mackay/c/readstrings.c
illustrates several advanced
methods:
1) using pointers and pointers to pointers;
2) allocating memory as the program runs,
in response to the inputted information.
It's still not a perfect program, but it seems
to work.