1

I'm trying to implement a command called displaycontent that takes a text file name as argument and display its contents. I am to use open(), read(), write(), and close() system calls in Linux to do this. It should act somewhat like the UNIX cat command for displaying file content.

Here is what I have so far:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    int fd;
    char content[fd];   

    errno = 0;
    fd = open(argv[1], O_RDONLY);


    if(fd < 0)
    {
        printf("File could not be opened.\n");
        perror("open");
        return 1;
    }
    else 
    {

        read(fd, content, sizeof(content)-1);
        write(1, content, sizeof(content)-1);
    }

return 0;
}

I have a file named hello2.txt and in it, there's the text: hellooooooooooooooo

When I do ./displaycontent hello2.txt, I get:

user@user-VirtualBox:~/Desktop/Csc332/csc332lab$ ./displaycontent hello2.txt
hellooooooooooooooo
����>k���[`�s�b��user@user-VirtualBox:~/Desktop/Csc332/csc332lab$ 

There are strange symbols and things following the content of the file. I am not sure what is wrong, any help would be appreciated. Thank you.

6
  • 5
    char content[fd]; What?
    – EOF
    Sep 12, 2015 at 21:41
  • @devbrs, it looks like you forgot to use close().
    – donjuedo
    Sep 12, 2015 at 21:49
  • @donjuedo: file-handles are automatically closed on program termination.
    – EOF
    Sep 12, 2015 at 21:56
  • Thank you user3121023 !!
    – page4
    Sep 12, 2015 at 22:06
  • @EOF, true, but he did say his assignment is to use all 4 functions.
    – donjuedo
    Sep 12, 2015 at 22:37

2 Answers 2

5

fd is not initialized, so the size of content is not determined.

You should not use the fd for that, anyway. You could use a large fixed number, if this is just an exercise. Otherwise, you'd want to get the file size and use that.

To get the file length, you can follow this example:

#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>

int main()
{
    int fd = open( "testfile.txt", O_RDONLY );
    if ( fd < 0 )
        return 1;

    off_t fileLength = lseek( fd, 0, SEEK_END );  // goes to end of file
    if ( fileLength < 0 )
        return 1;

    //  Use lseek() again (with SEEK_SET) to go to beginning for read() call to follow.
    close( fd );
    return 0;
}

(I have not compiled this today, and am only going from memory. If there are typos, they should be minor)

2
  • 2
    Even if that is fixed there is likely to still be junk output. The write outputs the whole buffer rather than just what was read.
    – kaylum
    Sep 12, 2015 at 21:47
  • You're right. I was adding to my answer as you commented. :-)
    – donjuedo
    Sep 12, 2015 at 21:47
3

use bytes = read (fd,content,sizeof(content)-1); to capture the number of bytes read. Then use bytes in write(1,content,bytes); to only write the bytes that were read. – user3121023

1
  • 1
    And bytes should be of type ssize_t.
    – alk
    Sep 13, 2015 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.