Check if files exist (specified by command line arguments)

I need a do program using a unix environment. I already bought the book "Advancing Programming in Unix Environment", and so far it has helped a lot. However, some of my questions have not been answered, and I am looking for some help.

I am trying to write a program that can check if the first and second arguments have been entered if a copy program exists. If the first argument does not exist, then an error message should appear and exit. If the second argument exists, you must display the rewrite prompt. I'm not quite sure how to check if a file already exists or not.

I saw several people saying that you can do (! -E) or something similar to check for an existing / non existing file.

If anyone could help me, I would really appreciate it.

+4
source share
2 answers

The access () function is designed to tell you if a file exists (or is available for reading, writing, or writing).

#include <unistd.h> int result; const char *filename = "/tmp/myfile"; result = access (filename, F_OK); // F_OK tests existence also (R_OK,W_OK,X_OK). // for readable, writeable, executable if ( result == 0 ) { printf("%s exists!!\n",filename); } else { printf("ERROR: %s doesn't exist!\n",filename); } 
+7
source

in your block int main(int argc, char** argv) { .

 if (argc == 3) { // then there were 3 arguments, the program name, and two parameters } else if (argc == 2) { // then prompt for the "second" argument, as the program name and one // parameter exists } else { // just print out the usage, as we have a non-handled number of arguments } 

now if you want to check if a file exists that is different from checking for the presence of a program argument. Basically try to open the file and read it, but pay close attention to looking for whole error codes and checking for errors. This will prevent your program from switching to a bit where these critical operations are supposed to work.

There is a common, but erroneous concept among new programmers when working with files in C. In principle, you really need to make sure that a certain block of code is working (copy block in your case), so they check, check, and double check conditions before executing the block. Check if the file exists, check if it has the correct permissions, check that it is not a directory, etc. My recommendation is that you do not do this.

Your copy unit should be able to fail properly, just as it should be successful. If this fails, then usually you have all the information you need to print a meaningful error message. If you check first and then act, there will always be a short period of time between verification and action, and this period of time will eventually see that the file is deleted or modified after the checks have passed, but before it is read. In this scenario, the entire pre-validation code did not provide any benefit.

Code without benefits is just a breeding ground for future bugs and architectural problems. Do not waste time writing code that has dubious (or not) benefits. When you suspect that some of the code you wrote is of little use, you need to rebuild your code to put it in the right place. When you suspect that code written by someone else is not very profitable, you first need to doubt your suspicions. It is trivially easy not to see the motives underlying the code fragment, and even more so when you are just starting a new language.

Good luck

--- code for the tired ---

 #include <errorno.h> #include <stdio.h> #include <stdlib.h> extern int errno; int main(int argc, char** argv) { // to hold our file descriptor FILE *fp; // reset any possible previously captured errors errno = 0; // open the file for reading fp = fopen(argv[1], "r"); // check for an error condition if ( fp == 0 && errno != 0 ) { // print the error condition using the system error messages, with the // additional message "Error occurred while opening file" perror("Error occurred while opening file.\n"); // terminate the program with a non-successful status exit(1); } } 
+3
source

Source: https://habr.com/ru/post/1432730/


All Articles