0

I want to pass an argument of a specific file location using realpath() (example: /var/log/message) and by using fprintf print the content of this file on the terminal. This is the code I have so far:

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
 if (argc < 2) {
 printf("Usage: %s <path>\n", argv[0]);
 return 1;
 }
 char *fullpath = realpath(argv[1], NULL);
 FILE *fptr;
 fptr = fopen(fullpath, "r");
 fprintf(fptr, "%s");
 return 0;
}

It doesn't throw errors, but it also doesn't do what I want it to do. When I run it e.g. ./test /var/log/message it will show me this on the terminal:

Segmentation fault (core dumped)

OS version

NAME="Fedora Linux"
VERSION="36

Compiler

gcc
asked Jul 10, 2022 at 12:06
7
  • 2
    With fprintf(fptr, "%s"); you attempt to write to the file, not read from it. There should be plenty of tutorials on how to read files all over the Internet, not to mention that any decent text-book should have chapters about it. Commented Jul 10, 2022 at 12:09
  • 2
    As for the problems, have you checked what the functions you call actually returns? Both realpath ad fopen might fail, you need to check for that. Commented Jul 10, 2022 at 12:26
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Jul 10, 2022 at 12:51
  • 1
    Not only does fprintf(fptr, "%s"); write, it's undefined behavior (That can but doesn't have to segfault) because you don't have a string argument for the %s format. Commented Jul 10, 2022 at 12:51
  • 1
    You don't normally need to use realpath() to open files specified on the command line. Simply pass argv[1] to fopen(). If you are not going to loop over all the command-line arguments, you should check for if (argc != 2). You should report errors on standard error, not standard output. Commented Jul 10, 2022 at 12:56

1 Answer 1

0

Got back to my function and added some more functionality and now it's working by adding argument for it. What I had to do is to declare that fptr is FILE and c is character type, running a while loop over the file and print its content and with using fgetc stream that content to the terminal.

Current code:

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
 FILE *fptr;
 char c;
 if (argc < 2) {
 printf("You need to grant an arg to %s <path>\n", argv[0]);
 return 1;
 }
 char *fullpath = realpath(argv[1], NULL);
 fptr = fopen(fullpath, "r");
 if (fptr == NULL)
 {
 printf("Cannot open file \n");
 free(fptr);
 exit(0);
 }
 c = fgetc(fptr);
 while (c != EOF)
 {
 printf ("%c", c);
 c = fgetc(fptr);
 }
 fclose(fptr);
 return 0;
}

Thanks all for your assistance.

answered Aug 12, 2022 at 20:32
Sign up to request clarification or add additional context in comments.

3 Comments

You still have a memory leak. If you pass NULL to realpath as second argument it does a malloc() for the return value. So, you have to free it, once you are done with it.
Like so? Make sense
fgetc() returns int, not char. The value of EOF does not fit into a char.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.