Thread: entire file

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    entire file

    I need to read an entire file. Past the \n all the wy to EOF. I am thinking about using unbuffered file reading(open) Is there another way.

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    What's in the file? How about using fgetc in a loop testing for EOF?
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Using open() and read() are your best bet.
    How about using fgetc in a loop testing for EOF?
    Oh god that would be horribly slow. Having to make a disk access call each time /shudder

    Just to play around I wrote a sample program for it
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    #define MEG 1048576
    
    int main(void)
    {
      char buffer[MEG];
      int fd, total=0, got=0;
    
      fd = open("file.txt", O_RDONLY);
      if ( fd == -1)
      {
        perror("Couldn't open file");
        return 1;
      }
    
      while (total < sizeof(buffer) )
      {
        got = read(fd, &buffer[total], sizeof(buffer) - total);
        if ( got == -1 )
        {
          perror("Problem with read()");
          break;
        }
        if ( got == 0)
          break;
        total += got;
      }
      printf("Got %d characters\n", total);
      printf("%s\n", buffer);
      return 0;
    }
    It will read a meg - 1 worth of data. -1 is the error code for read and 0 is the EOF code.

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Here's an idea (using standard C) for reading in an entire file,
    note that error checking has been omitted.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char** argv)
    {
            FILE* fp = fopen(argv[1], "rb");
    
            char* buf;
    
            long nbytes;
    
            
            fseek(fp, 0L, SEEK_END);
    
            nbytes = ftell(fp);
    
            fseek(fp, 0L, SEEK_SET);    /* restore file pointer indicator */
    
            
            buf = malloc(nbytes * sizeof(char));
    
            fread(buf, sizeof(char), nbytes, fp);
    
            fclose(fp);
    
    
            free(buf);
    
            return 0;
    }
    Last edited by c99; 02-23-2004 at 03:34 PM.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Originally posted by Thantos
    Using open() and read() are your best bet.
    How about using fgetc in a loop testing for EOF?
    Oh god that would be horribly slow. Having to make a disk access call each time /shudder
    Or maybe not.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Good to know.

    Of course reading one character at a time will still be slower then reading the entire block at once. For example I made a one meg file filled with random characters from ASCII value of 0 to 127. Ran the following two programs on it
    entirefile.c
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    #define MEG 1048576
    
    int main(void)
    {
      char buffer[MEG];
      int fd, total=0, got=0;
    
      fd = open("file.txt", O_RDONLY);
      if ( fd == -1)
      {
        perror("Couldn't open file");
        return 1;
      }
    
      while (total < sizeof(buffer) )
      {
        got = read(fd, &buffer[total], sizeof(buffer) - total);
        if ( got <= 0)
        {
          if ( got == -1 )
          {
            perror("Problem with read()");
            break;
          }
          if ( got == 0)
            break;
        }
        total += got;
      }
      printf("Got %d characters\n", total);
      return 0;
    }
    entirefile2.c
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    #define MEG 1048576
    
    int main(void)
    {
      char buffer[MEG];
      int total=0;
      FILE *fp;
    
      fp = fopen("file.txt", "r");
      if ( fp == NULL)
      {
        perror("Couldn't open file");
        return 1;
      }
    
      while (total < sizeof(buffer) )
      {
        buffer[total]=fgetc(fp);
        if ( buffer[total] == EOF)
        {
          buffer[total]='\0';
          break;
        }
        total++;
      }
      printf("Got %d characters\n", total);
      return 0;
    }
    Times for entirefile.c
    real 0m0.013s
    user 0m0.000s
    sys 0m0.010s

    Times for entirefile2.c
    real 0m0.115s
    user 0m0.110s
    sys 0m0.010s


    Granted they are both under 1 second but stil fgetc was a little under 9 times slower.

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Thantos, heh, I immediately went and did something simular.

    I used time.h clock_t and clock() to get cpu ticks.

    Results reading a 1MB file of random junk into a buffer:

    fgetc took approx 731 cpu ticks.
    fread took approx 20 cpu ticks.

    That's almost 40X faster using fread to read in the entire file vs
    reading in each byte separately.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
            buf = malloc(nbytes * sizeof(char));
    
            fread(buf, sizeof(char), nbytes, fp);
    
            fclose(fp);
    
            printf("%s", buf)
    ... and you don't see a bug in this code?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    ... and you don't see a bug in this code?
    Which one? The bug that assumes nbytes is really the size you want, the bug that results in a null pointer access if malloc fails or the bug that results in a null pointer access if fopen fails?

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Edward
    Which one? The bug that assumes nbytes is really the size you want, the bug that results in a null pointer access if malloc fails or the bug that results in a null pointer access if fopen fails?
    nbyes will be the size of the file, as the file was opened in binary mode, so that isn't a bug. But still, you missed another one, keep guessing
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Edward, well done, you found three bugs that don't exist.

    applause.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by c99
    Edward, well done, you found three bugs that don't exist.

    applause.
    >>the bug that results in a null pointer access if malloc fails<<
    That's valid.

    >> or the bug that results in a null pointer access if fopen fails?<<
    That's also valid.

    OK, so we have to assume you omitted error checking for clarity purposes.

    But, there's still another bug (a real bug, not an error checking problem)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    nbyes will be the size of the file
    True. But will the size of the file be the size that you expected?
    But still, you missed another one
    Not in your snippet unless I'm going blind (that I wear glasses suggests that this could very well be the case). However, in the original code there appears to be a & hiding where it shouldn't be that I missed in my first glance.
    Edward, well done, you found three bugs that don't exist.
    Really. Look again until you see at least two of them. I can tolerate the first because it isn't likely to be a problem.

  14. #14
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Ok, so we have to assume you omitted error checking for clarity purposes.
    Am I the only one that doesn't wear glasses here? I could swear
    the second line of my post says "note that error checking has
    been omitted" or is this a case of selective blindness?

    Which leaves the one bug which both Edward and I have yet to find.

  15. #15
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Cat got your tongue?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM