0

Any of my C programmes utilizing fopen() run perfectly from IDE or Windows environment, but fail when opened from Command Line (cmd.exe), as fopen("r") keeps returning NULL pointer. The files do exist and have no permission restrictions. So the programms DO run from Command Line, but with error (null pointer) as soon as they encounter fopen() in the code.

int main(int argc, char *argv[])
 {
  FILE *fr, *fw;
  int j;

  if(argc==1)
   {
     vypis_navodu(argv[0]); // function call
   }

  if(argc==2)
   {     
      if ((fr=fopen(argv[1], "r")) == NULL) // returns NULL from CMD 
          {
           printf("\nunable to open %s\n", argv[1]); 
           return 1;    
          }

SOLUTION USING THE FULL PATH TO FILE:

int main(int argc, char *argv[])
{
FILE *fr, *fw;
char path[100] = "C:\\folder\\"; // path to the folder
int j;

if(argc==1)
 {
  vypis_navodu(argv[0]);
 }


if(argc==2)
 {   
    if ((fr=fopen(strcat(path, argv[1]), "r")) == NULL) // works o.k.
      {
        printf("\nunable to open %s\n", argv[1]);
        return 1;   
      }
8
  • 3
    Post your code. Commented Jan 6, 2023 at 9:40
  • Try to use a full pathname with fopen() Commented Jan 6, 2023 at 9:41
  • Use strerror (manpagesfr.free.fr/man/man3/strerror.3.html) to display the error message in order to know what's wrong. You can use perror too, or simply errno value. Commented Jan 6, 2023 at 9:49
  • There is no problem in the snippet of code you have posted. You should also share the argument set in IDE to pass when program run and argument that you are passing in command line. Commented Jan 6, 2023 at 11:02
  • @H.S. the path i was passing in command line was not full. It works with the full path. Thanks. Commented Jan 6, 2023 at 11:18

1 Answer 1

0

From the man page:

The fopen(), fdopen(), and freopen() functions may also fail and set errno for any of the errors specified for the routine malloc(3).

The fopen() function may also fail and set errno for any of the errors specified for the routine open(2).

You may use perror or strerrror to print out an error message.

The perror() function produces a message on standard error describing the last error encountered during a call to a system or library function.

Here's an example:

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[]) 
{
    /* argument checking here... */

    FILE *fp;

    errno = 0;
    fp = fopen(argv[1]), "r");
    if (!fp) {
        perror("fopen");
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}


Sign up to request clarification or add additional context in comments.

2 Comments

The fopen() function is not required to set errno: "The fopen function returns a pointer to the object controlling the stream. If the open operation fails, fopen returns a null pointer." That means fopen() is free to modify errno to any non-zero value whether there's any error or not: "The value of errno may be set to nonzero by a library function call whether or not there is an error. ..." The question does not state the C implementation used, so the man page may not be relevant.
Note that the POSIX specification for fopen() stipulates: Otherwise, a null pointer shall be returned, [CX] ⌦ and errno shall be set to indicate the error. ⌫ where the material in "[CX] ⌦ … ⌫" indicates an extension over standard C that does require errno to be set on POSIX systems. Check the man page for fopen() on your system, but if it is Unix-based (Linux, etc), it will set errno.

Your Answer

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.