how to list contents of a file?

This is a discussion on how to list contents of a file? within the C Programming forums, part of the General Programming Boards category; hi all, how do i list the contents of a file and calculate the string length (strlen) with it? thanks!...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    8

    how to list contents of a file?

    hi all,

    how do i list the contents of a file and calculate the string length (strlen) with it?

    thanks!

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    45

    solution

    u can look into the implementation of the wc command in unix . say cat <filename>|wc-l. or equivalently to count the number of characters in it. Then u can use the same in your C code for achieving the same

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    8

    wc in C

    hi,

    thanks for your tips. i know cat <filename> is to display contents of file in Unix, but what is the equivalent code in C?

    Do i have to open the file first fopen() before displaying the contents? thanks!

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    3
    of course dear, you will have to open the file, without bringing the file into the RAM how can you work on that?

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    45

    solution

    yes

    u have to open the file. u can look for the implementation of the cat command as it is open source then go ahead with your program you will get it

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157

    Re: how to list contents of a file?

    Use the cat command?? When did this become a Unix OS Command board? Sheesh!

    Originally posted by zagelle
    hi all,

    how do i list the contents of a file and calculate the string length (strlen) with it?

    thanks!
    Open the file.
    Read each line.
    Add the number of characters read to a counter for the file length
    Output the line to list the contents
    When done, close the file.

    This will work even in Windows and DOS too, whereas the other suggestion is only Unix/Linux.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    Quick code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc,char *argv[])
    {
      char c;
      FILE *fp;
      if (argc!=2) {
        puts("wrong command.\n");
        exit(1);
      } else
        if ((fp=fopen(argv[1],"r"))!=NULL) {
          while ((c=(char)getc(fp))!=EOF)
            putc(c,stdout);
          fclose(fp);
        } else {
          puts("file not found.\n");
          exit(1);
        }
      return 0;
    }
    [EDIT] Hoping this is not your homework.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    char c;
    while ((c=(char)getc(fp))!=EOF)
    FAQ (getc returns an int).
    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.*

  9. #9
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    >>(getc returns an int).

    You are right.
    It's just a quick code. And I only intend to cast it to be printable to stdout. However anybody can fix it.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >And I only intend to cast it to be printable to stdout.

    Besides being incorrect for the comparison with EOF, it is also unnecessary -- putc is already performing this conversion.
    #include <stdio.h>

    int fputc(int
    c, FILE *stream);
    int putc(int
    c, FILE *stream);

    fputc()
    writes the character c, cast to an unsigned char, to stream.

    putc() is equivalent to fputc() except that it may be implemented as a macro which evaluates stream more than once.
    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.*

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by Jaguar
    Quick code
    ...

    [EDIT] Hoping this is not your homework.
    If you are concerned about someone's ability to learn, as you implied, it's best to refrain from posting code that answers the question no matter how easy the solution. I know how it's a kick to be able to answer someone's question, but hold back on the solution so they can learn by solving it.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me debug this linked list !
    By Dark Angel in forum C Programming
    Replies: 6
    Last Post: 04-18-2008, 02:10 PM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 06:29 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21