Thread: Read Text File with System Call

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    14

    Read Text File with System Call

    Hi All,

    I'm new to C-programming, and I'm trying to read a text file.

    Here's my code so far.

    Code:
     #include <stdio.h>
          2 #include <stdlib.h>
          3 #include <string.h>
          4 #include <sys/types.h>
          5 #include <sys/stat.h>
          6 #include <fcntl.h>
          7 #include <unistd.h>
          8 #include <errno.h> // external int errno
          9
         10 #define ACC_RIGHT 0644 /* User: Read+Write, Group and Other: Read */
         11
         12 int main(int argc, char *argv[])
         13 {
         14   /*int cmd_count;
         15
         16   printf("input= &#37;s\n", argv[1]); // program name
         17   printf("output= %s\n", argv[2]);
         18
         19   int fd; // file descriptor
         20
         21   if ((fd = open(argv[1], O_WRONLY, ACC_RIGHT)) < 0) {
         22     //perror("Fault by open file "); exit(1);
         23     printf("Cannot open file!\n");
         24   } else {
         25     printf("File has been openend!\n");
         26   */
         27
         28   //#define BUFSIZE 128
         29
         30   //char buffer;
         31
         32   char c;
         33   //char *pc;
         34   char pc[512];
         35   int nread;
         36
         37   //pc = &c;
         38
         39   //char buffer[512];
         40
         41   int  filedes;
         42   //ssize_t nread;
         43   //long total = 0;
         44   //int count = 0;
         45
         46   /* open the test file */
         47   if ((filedes = open("test.txt", O_RDONLY)) == -1)
         48   {
         49         printf("Couldn't open test.txt \n");
         50   } else {
         51         printf("file is open!\n");
         52   }
         53
         54   /* read in loop until EOF */
         55   while (nread = read(filedes, pc, 1) > 0)
         56   {
         57         //printf("Read %d bytes this time \n", nread);
         58         //count++;
         59         //*pc ^= 31;
         60
         61         printf("char=%s\n", pc);
         62
         63         //total += nread;
         64   }
         65
         66   //printf("char1=%s\n",pc);
         67
         68   //printf("char1=%s\n",c[0]);
         69
         70   //printf("Read %d bytes in total \n", total);
         71   //}
         72
         73   return 0;
         74 }
    Here's my problem.

    I do not want a fixed buffer size when reading text files, and I need to use the 'read' function.

    I'm not new to programming, but C is new for me.

    If I use a pointer, which is commented out in my code above (*pc / pc = &c; ), I'm getting strange characters at the end of the characters from my text file, e.g.:

    char=R&#171;&#223;..&#191;

    instead of

    char=R, which I get when using the above code, without commenting out the pointer code.

    Please, Help! This is frustrating me.

    Best Regards,

    Kevin
    Last edited by kevinv2u; 02-13-2008 at 07:27 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why do you want to use open/read instead of the better fopen/fgets?
    Also, read wants to know the maximum number of bytes you can read into your buffer, which should be sizeof(buffer).
    And just reading into an uninitialized pointer is very, very bad. I suggest you read a little about pointers and dynamic memory management if you need to use it.

    And note that pointers are not "unlimited" or "non-fixed" buffers. They are just pointers to a fixed memory block. Only you can define the size of that memory block yourself. It's not a solution to fixed buffers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    14
    I need to use read beause of an assignment. We need to use direct system calls for handling files.

    The static buffer size is not a problem for my school assignment, but I'm personally not happy with the fixed buffer size. I think this can be done better.
    What if the file is larger than the specified amount in my buffer?

    I'm just starting with C, so I still need to read a lot. ( also about pointers. )

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All buffers are fixed size. However, it's possible to reallocate and thus change the buffer size if you need a bigger size later. This is dynamic memory management, done using malloc/realloc/free.
    For your code, you might just get away with a fixed buffer on the stack.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    14
    What about using filelength functions, and using this to allocate the buffer?
    Is that an option? Or would this be a dirty solution?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure, you could do that.
    Just don't allocate 500 MB memory to read a file, because that would just be bad.
    Being a programmer, I take it that you know a little of how operating systems work?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    14
    Yes, I do...

    I've got experience with various languages (.NET, Java, VB6, PHP) and operating systems.

    C is quite a bit different though...

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it's much lower level
    Though I prefer C++...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kevinv2u View Post
    What about using filelength functions, and using this to allocate the buffer?
    Is that an option? Or would this be a dirty solution?
    Is it part of your assignment to actually allocate a buffer, or is this your personal choice to do it that way?

    I would think that a better solution is to use fixed size buffer of say 4KB - 32KB. Reading the file in it's entirety will ultimately fail, because it's quite easy to create a file that is significantly larger than the addressable space of most machines - e.g. you can easily create a 1TB file, which is the addressable space of a Athlon64 processor - never mind finding a machine with even nearly 1TB of memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    14
    It's not part of my assignment to allocate a dynamic buffer. It's a personal choice of mine.

    I can declare the buffer as such: 'char buffer[80]' and read a text file into this buffer. That would do for the assignment.

    I'm just experimenting with different text files of different sizes, and want to make the program as flexible as possible, so that it can also read larger text files, with a limit of course.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think the normal is to define an array with BUFSIZE size (or BUF_SIZE?) when reading and reading/writing from/to files.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    14
    You mean that I have to define BUFSIZE in my code as such?:

    Code:
    #define BUFSIZE 80

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, it's a default macro defined in some header.
    It's called BUFSIZ and is declared inside stdio.h.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    14
    Thanks! I've modified my code...

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, BUFSIZ is good for stdio, e.g. fread, fwrite and friends, but I wouldn't recommend using that for a read/write operation type.

    Also, it's probably best to use an even power of 2, such as 512, 1024. Multiples thereof [and yes, technically 80 is 5 * 2 ^ 4, but 2 ^ 4 isn't big enough, and 5 isn't a good multiple].

    Remember that disk sectors are usually 512, sometimes 1024 or 2048 bytes long, so it's to your advantage if the size is a "nice" multiple of this size.

    The bigger the buffer, the less overhead you get from the read/write call in itself.

    [If you want to make it really fancy, allow a switch -bN[k,m] that allows you to modify the blocksize, and if nothing is specified use a default of, say, 4KB].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help is needed to read text from a file
    By yuzhangoscar in forum C Programming
    Replies: 12
    Last Post: 09-12-2008, 12:10 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. getline function to read 1 line from a text file
    By kes103 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 06:21 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM