Ben
Ben

Reputation: 95

C can't open file using a variable

I need to open a file located on Desktop(Linux). If i write the location as a string inside the fopen() function it works, but if i pass it as a variable, it doesn't work. Here is my code :

fp = fopen(readPathToFile, "r");
if (!fp){
       printf("Failed to open text file\n");
       exit(1);
}
else{
      fscanf(fp,"%s",line);
      printf("File read: %s",line);
}

If i write it like this, it shows me the content of file :

fp = fopen("home/user/Desktop/test.txt", "r");
    if (!fp){
           printf("Failed to open text file\n");
           exit(1);
    }
    else{
          fscanf(fp,"%s",line);
          printf("File read: %s",line);
    }

The child process opens the file. Here is my full code

   #include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#define READ  0
#define WRITE 1
int main ()
{
  pid_t pid;

  int mypipefd[2];
 id_t child_pid;
 char line[100];
 char *pathToFile[100];
 FILE *fp;
 char buff[255];
 /* create the pipe */
  if (pipe(mypipefd) == -1) {
    fprintf(stderr,"Pipe failed");
    return 1;
  }

 child_pid = fork () ;

    if (child_pid > 0) {
        printf("Introduceti locatia catre fisier:");
        fgets(pathToFile, 100, stdin);
        close(mypipefd[READ]);
        write(mypipefd[WRITE], &pathToFile, sizeof(pathToFile));
        close(mypipefd[WRITE]);
        printf("parent: write value : %s",pathToFile);
    }
    else if (child_pid < 0) {
        fprintf(stderr, "Fork failed");
        return 1;
    }
    else{
        char *readPathToFile[100];
        close(mypipefd[WRITE]);
        read(mypipefd[READ], &readPathToFile, sizeof(readPathToFile));
        close(mypipefd[READ]);
        printf("child: read value : %s",readPathToFile);
        fp = fopen(readPathToFile, "r");
        if (!fp)
        {
            printf("Failed to open text file\n");
            exit(1);
        }
        else{
            fscanf(fp,"%s",line);
            printf("File read: %s",line);
        }
    }
return 0;
}

Upvotes: 0

Views: 530

Answers (2)

John Bode
John Bode

Reputation: 123558

Okay, so this is the root of your problem:

char *pathToFile[100];

This declares pathToFile as a 100-element array of pointers to char, not a 100-element array of char. The first thing you need to do is change that declaration to

char pathToFile[100];

Secondly, fgets will save the trailing newline from your input to the target buffer if there's room, so you'll need to remove that newline from the input:

char *newline = strchr( pathToFile, '\n' );
if ( newline )
  *newline = 0;

Upvotes: 1

Jens
Jens

Reputation: 72707

Your compiler did not warn you about the type mismatch in

char *pathToFile[100];
fgets(pathToFile, 100, stdin);

(array of 100 pointers-to-char versus array of 100 chars)? Did you turn warnings off?

Also note that fgets retains the newline. Your file name probably does not end with a newline. You should replace it with a NUL (zero) byte.

Typically you don't need a debugger to track these down. A little bit of printf debugging can do wonders. :-)

Upvotes: 1

Related Questions