Thread: reading a file of unknown length

  1. #1
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51

    reading a file of unknown length

    hey people!!

    Need a little help if someone has five minutes.
    I'm trying to write some code to read from a file, without
    knowing the exact length of the file, if you know what i mean.

    This is what i've come up with.
    Code:
    
    
    #include<stdio.h>
    #include<stdlib.h>
    FILE*fptr;
    
    main()
    {
        int i;
        char *m[5];
        char why[16];
       
        fptr=fopen("c:\\MYTEST ", "w+");
        fprintf(fptr,"hello to whole world...whats goin down in happyville??\n");
        fseek(fptr, 0, SEEK_SET);
        while(!feof(fptr))
        {
        gets(why);
             if(!feof(fptr))
             {
                 for(i=0;i<5;i++)
                 {
                     m[i]= (char*) malloc(10*1);
                 }
             }
         }
         
             printf("%s %s %s %s %s %s", why,m[0],m[1],m[2],m[3],m[4]);
             
                 fclose(fptr);
                 
                     getchar();
                 }





    this code doesnt actually work, hence my call for help. The creation of
    the file works however when it gets to the 'while' something must go wrong
    because my command window just shows a blank.


    i'm sure its obvious that i'm a little new to this and
    i'm probably trying to run before i can walk but if someone can give
    me a little insight into how this sort of thing is done
    i'd be real greatful

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It doesn't actually matter how big a file is if you work line-by-line. Lots of people recommend trying it this way first, before doing anything dramatic, if you have a more complex file to read.

    The easiest way to read a line from a file is actually to put fgets in a loop. feof() shouldn't be used in a while loop.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    
    int main (void) {
       FILE * txtin;
       txtin = fopen("junk.txt", "a+");
       if (txtin != NULL) {
          fputs("SAGE - set a good example", txtin);
          char buf[BUFSIZ] = {'\0'}; 
          /* one of your biggest responsibilities is just supplying big
           * enough storage space 
           */
           while (fgets(buf, BUFSIZ, txtin) != NULL) {
              /* buf now contains a line... do your processing, 
               * whatever it may be.
               */
              fputs(buf, stdout);
           }
       } else {
          perror("fopen");
       }
    
       fclose(txtin);
       return 0;
    }
    For the sake of example this just outputs the contents of the file onto the screen, but this snippet is particularly common I believe.

    Reference:
    http://www.cprogramming.com/tutorial/cfileio.html
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Last edited by whiteflags; 07-12-2006 at 03:48 PM.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Have a look at the FAQ for a few items that you might want to clear up in your code:

    Why gets() is bad / Buffer Overflows
    Casting malloc
    Why it's bad to use feof() to control a loop

    Also, when you call malloc, you ought to make an attempt to free the allocated memory when you are done with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Reading indeterminate length line from file
    By Yasir_Malik in forum C Programming
    Replies: 32
    Last Post: 12-11-2004, 03:49 PM
  4. Reading from file into structs..
    By dankas in forum C Programming
    Replies: 14
    Last Post: 10-16-2002, 10:33 PM