Hi, again. I'm trying to read in data from a file that I've opened, so it is sitting in FILE* fp. Now I want to put it into a format that I can do things with it (seperate it into words). My question is: how can I do this dynamically? I found code (below) for putting files into a string but if I open a file of more than a certain length (in the example below, 128) then I will encounter problems, if I choose a huge fixed length then I am being very inefficient. With regards to my end goal, is there a way to either determine the length of the file then choose the string from that, or not use strings at all, maybe lists or such?

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  FILE *fp;
  char str[128];

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

  while(!feof(fp)) {
    if(fgets(str, 126, fp)) 
        printf("%s", str);
  }

  fclose(fp);

  return 0;
}

Much love,
sd

That's extremely useful, Comrade, and I thank you, but unfortunately I don't even have the book in string form yet, just as a file. I'm sorry I wasn't more clear in my original post.

Much love,
sd

> while(!feof(fp)) {
> if(fgets(str, 126, fp))
You can say this more succinctly with
while ( fgets( str, sizeof str, fp ) != NULL )

A point to note, you don't have to adjust the buffer size when calling fgets.

You can say this more succinctly with
while ( fgets( str, sizeof str, fp ) != NULL )

Minor correction on that one: while ( fgets( str, sizeof(str), fp ) != NULL )

Right now the program is only reading in the first line of the text file, why would this be?

/* Code supplied by Prelude */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define DELIM   " " 
#define MAXWORD 80 
#define MAXLEN  20 

int main ( int argc, char *argv[] )
{
  char  words[MAXWORD][MAXLEN];
  char  buff[BUFSIZ];
  int   ntokens = 0;
  int   i;

  //printf("Enter a string: ");
  //fflush(stdout);


    FILE *fp;
     // if a file is specified after the program name
     // then that file is opened
    if (argc > 1 ) {
       char* file = argv[1];
       if ( ! ( fp = fopen( file, "r" ) ) ) {        // and if it doesn't work
                                                     // an error results and the
                                                     // user is notified
            printf("     Error opening file. %s\n", strerror(errno));
            return (errno);
       }
    }
    // otherwise, it assumes the file for this assignment, which is
    // christmas_carol.txt
    else {
         printf("     No file specified; using the default christmas_carol.txt\n");
         char* file = "christmas_carol.txt";
          if ( ! ( fp = fopen(file, "r"))){          // but even that isn't for
                                                     // sure, so best check as well
               printf("     Error opening file. %s\n", strerror(errno));
               return (errno);
          }
          printf("     File has been loaded.\n");
    // congratulations, the file is open is just under 25 lines!
}



  if (fgets(buff, sizeof buff, fp) != NULL)
  {
    char  *sep = strtok(buff, DELIM);

    while (sep != NULL)
    {
      strcpy(words[ntokens++], sep);
      sep = strtok(NULL, DELIM);
    }
  }

  for (i = 0; i < ntokens; i++) 
  {
    puts(words[i]);
  }

  return(0);
}

Minor correction on that one: while ( fgets( str, sizeof(str), fp ) != NULL ) regards Niek

The code seems to run the same either way -- odd this wasn't picked up by the compiler. Thanks for the correction.

The code seems to run the same either way -- odd this wasn't picked up by the compiler. Thanks for the correction.

It's allowed both ways by compilers, (because sizeof is an operator, not a function) but I always put brackets around the 'argument' for readability. It's a mather of personal taste, like indenting code.
If you use sizeof to get the size of a variable-type (like int,char etc) the brackets ARE required. So to minimize my confusion I just always use the brackets :)

Right now the program is only reading in the first line of the text file, why would this be?

Your problem is here:

if (fgets(buff, sizeof buff, fp) != NULL)
  {

Change the 'if' to a 'while' and the problem should magicly disappear!

The problem did magically disappear, along with the rest of my output?

The problem did magically disappear, along with the rest of my output?

Haha :)

Perhaps you should add a getchar() before your return 0; , this keeps the window open when the program is done running.
The code you've posted works fine, I've tested it on VC8 with my own textfile. Perhaps something is wrong with your inputfile? Or maybe you've made some changes that you didn't post here yet?

Basically, I had more than eighty words in my text file so it crashed midway. i increased the max_word and it magically worked. thanks niek!

Much love,
sd

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.