3

Here is my code which checks if the file exists :

#include<stdio.h>
#include<zlib.h>
#include<unistd.h>
#include<string.h>


int main(int argc, char *argv[])
{
   char *path=NULL;
   FILE *file = NULL;
   char *fileSeparator = "/";
   size_t size=100;
   int index ;
   printf("\nArgument count is = %d", argc);

   if (argc <= 1)
   {
      printf("\nUsage: ./output filename1 filename2 ...");
      printf("\n The program will display human readable information about the PNG file provided");
   }
   else if (argc > 1)
   {
      for (index = 1; index < argc;index++)
      {
            path = getcwd(path, size);
            strcat(path, fileSeparator);
            printf("\n File name entered is = %s", argv[index]);
            strcat(path,argv[index]);
            printf("\n The complete path of the file name is = %s", path);
            if (access(path, F_OK) != -1)
            {
                  printf("File does exist");
            }
            else
            {
                  printf("File does not exist");
            }
            path=NULL;
      }
   }
   return 0;
}

On running the command ./output test.txt test2.txt The output is:

$ ./output test.txt test2.txt

Argument count is = 3
 File name entered is = test.txt
 The complete path of the file name is = /home/welcomeuser/test.txt
 File does not exist
 File name entered is = test2.txt
 The complete path of the file name is = /home/welcomeuser/test2.txt
 File does not exist

Now test.txt does exist on the system:

$ ls
assignment.c  output.exe  output.exe.stackdump  test.txt

and yet test.txt is shown as a file not existing.

Please help me understand the issue here. Also, please feel free to post any suggestions to improve the code/avoid a bug.

Regards, darkie

name_masked
  • 9,544
  • 41
  • 118
  • 172
  • 2
    You still have not fixed the segmentation fault problems you had in your last question. You might want to fix that, just because it doesn't crash does not mean it is working correctly. – SoapBox Apr 01 '10 at 00:19
  • Hi SoapBox, I believe the Seg fault was because size was not defined with a value. I have assigned it to 100. Please let me know if there is anything more to it. – name_masked Apr 01 '10 at 00:23
  • You should check the value in errno after the call to access to figure out why it thinks it doesn't exist. Oh and allocate some space for path too. – Craig Apr 01 '10 at 00:24
  • 1
    @darkie15, that's not your problem. You can't be calling `strcat()` on the return value from `getcwd()`. Well, at least not if you want your program to work reliably. – Carl Norum Apr 01 '10 at 00:27
  • Please tag this as homework, unless there is some other reason your file is named `assignment.c` ? – Tim Post Apr 01 '10 at 02:58

2 Answers2

2

Just because the call to access() fails does not mean that the file does not exist. The call could fail for other reasons.

Use printf("error:%s\n", strerror(errno)); to print out the text of the error message.

Also you are still incorrectly appending to "path" received from getcwd as you were in your previous question. Even though it is not crashing, it is still not correct and could cause you problems... possibly even the problem you have now.

getcwd() allocates a buffer for your path, but that buffer is only sized to fit the path. you are appending to that buffer, going past the end. That's bad, you can't do that. It will cause problems, and occasionally crashes. you need to pause and understand how this getcwd function works and how to properly use it.

SoapBox
  • 20,457
  • 3
  • 51
  • 87
  • Hi SoapBox, I have added a new char array and copied the value stored by existing variable 'path' to this char array using strcpy. Now I have used this char array for all further operations. The code is working now as required. Do you think there is anything more I should look into? Thank you for you patience. Regards, darkie – name_masked Apr 01 '10 at 00:47
0

I strongly suggest allocating enough room to store the path via malloc() and fpathconf() (hint, PATH_MAX).

A non-standard way of allocating and assembling it would be asprintf().

Just be sure to free the resulting path when its no longer needed, and check every call that could possibly fail due to user typos for failure.

If using malloc(), always check for failure (the result being NULL).

Good luck with your assignment :)

Tim Post
  • 33,371
  • 15
  • 110
  • 174