Help with Strings 
Author Message
 Help with Strings

A beginner in C needs help,

I want to write a program which takes a file an input containing a list
of items, prints the contents of that file as an array of strings. If
the file contains more than 150 items, the program should inform the
user, takes the first 150 while discarding the rest. So far this is what
I come up with, can anyone help?

#include <stdio.h>

#define  MAXX 80
#define  MAXY 150

char  item[MAXY][MAXX];

int main(int argc, char *argv[])
  FILE *fp;
  int  t, i, j;

  if(argc!=2) {
    printf("You forgot to enter the filename.");
    return 1;

  if((fp=fopen(argv[1], "r"))==NULL) {
    printf("Cannot open file.\n");
    return 1;

  while(!feof(fp)) {
    for(t=0; t < MAXY; t++)  {
      fgets(item[t], MAXX, fp);
      if(!*item[t]) break; /* quit on blank line */


  for(i=0; i < t; i++) {
    for(j=0; item[i][j]; j++) printf("%c", item[i][j]);


  return 0;



Fri, 10 Jan 2003 03:00:00 GMT  
 Help with Strings


[ ... ]

>   while(!feof(fp)) {

This line is common among beginners, but it's _almost_ always a
mistake.  You usually want to test for EOF using the function that
reads the data. In your case, you're reading the data with fgets,
which returns NULL to signal the end of the file or an error having
occurred.  You generally restrict use of feof to sorting out why
fgets (or some other reading function) quit reading input.

>     for(t=0; t < MAXY; t++)  {
>       fgets(item[t], MAXX, fp);
>       if(!*item[t]) break; /* quit on blank line */

>   }

This has a serious problem: you're trying to do multiple calls to
fgets, but not checking for it returning NULL in any of them.  If
your input isn't properly terminated, you'll end up with a problem.  
And it's pretty much considered a given that you'll receive bad input
on a fairly regular basis, so you'd better deal with it at least
somewhat reasonably -- at least printing out an error message, not
just accepting it and producing garbage output.

>   for(i=0; i < t; i++) {
>     for(j=0; item[i][j]; j++) printf("%c", item[i][j]);
>     }

This looks to me like a long, slow way of handling the job.  Worse
yet, except under a rather obscure set of circumstances, it's
probably not doing what you want.

IMO, rather than storing all the data (a waste of memory if there
ever was one) you should simply read in a line, print it out, and
then do the same for a maximum of 150 times.  If you don't want to
print more than a specified width, you can limit it pretty easily
when you read the data in.  One approach would look like this:

#define MAXX 80
#define MAXY 150

char buffer[MAXX+1];

// The following pair of macros turn a defined constant into a
// string.  In case you care, the first one causes the macro argument
// (e.g. MAXX) to be expanded, and then passed to the second one.
// The second then turns the expanded argument (80) into a string
// ("80").  We simply embed that into the middle of the format string
// so all of them get concatenated into one long format string.
#define S(x) #x
#define STR(x) S(x)

for (i=0; i<MAXY; i++) {
        scanf("%" STR(MAXX) "[^\n]%*[^\n]", buffer);


I've separated this into two calls to scanf instead of one because we
more or less expect the second conversion in the first call to fail
on a regular basis (when the input fits into the buffer).  If a
format conversion fails, all the rest of the conversions are ignored,
and we don't want to skip reading and discarding the new-line when
all the input fits into the buffer we provided.


The universe is a figment of its own imagination.

Sat, 11 Jan 2003 03:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. newbie needs help to strings, strings, strings

2. Help about string & sub-string

3. Help: Convert string to decimal (or double)

4. S-Language,help with strings,char arrays?

5. need urgent help for string processing

6. Help with strings and parsing

7. Beginner help with strings

8. Beginner needs help with strings, pointers, arrays

9. Help - reverse string

10. Need help with String Array

11. please help newbie string replacement in big file

12. need some help with strings and files.


Powered by phpBB® Forum Software