Detecting file that isn't there

2019-08-05 09:16发布

问题:

So this is one of the first programs I've ever used some self created error checks however for some reason when I compile this and run it using:

./file test1.txt test2.txt 10

I get absolutely an error suggesting that the output file exists and I've checked the file and it doesn't even when I change the name of the output file (second argument) I get nothing. Anyone who can help? I've been racking my brain for ages now. This is a UNIX homework assignment I'm compiling and running in Gentoo. I have it running in a VB and have a linked folder between my windows and linux OS's.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

#define BUFFT 25

int main (int argc, char *argv[])
{
  int count;
  int readin;
  int writeout;

  printf ("This program was called  \"%s\".\n",argv[0]);

  if (argc > 1)
    {
      for (count = 1; count < argc; count++)
    {
      printf("argv[%d] = %s\n", count, argv[count]);
    }
    }
  else
    {
      perror("The command had no arguments.\n");
      exit(-1);

    }
    // check correct number of arguments parsed //


    if  (argc == 4)
    {
        printf("There are the correct number of arguments(4)\n");

        }
        else
        {
            perror("Not enough arguments! please try again \n");
            exit(-1);
            }
    //Check original file is there//

    int openFD = open(argv[1], O_RDWR);
    if (openFD <0)
    {
        perror("Error unable to read file \n");
        exit(-1);
    }

    //Check existence of output file, if it doesn't exist create it//

    int CheckFile = open(argv[2], O_RDONLY);
    if (CheckFile < 0)
    {
        perror("Error output file already exists \n");
        exit(-1);
        }
     else
     {
     int CheckFile = open(argv[2], O_CREAT);
     printf("The file has successfully been created \n");
     }

    //Create buffer

    int bufsize = atoi(argv[3]);
    char *calbuf;
    calbuf = calloc(bufsize, sizeof(char));

    //Read text from original file and print to output//

    readin = read(openFD, calbuf, BUFFT);
    if (readin < 0){
        perror("File read error");
        exit(-1);
    }
    writeout = write(openFD,bufsize,readin);
    if (writeout <0){
        perror("File write error");
        exit(-1);
    }


  return 0;
}

回答1:

The open call for HANDLE CheckFile is printing Error File Exists. This is your problem. You are printing out the wrong statement when Output File Is Not Found and moreover you are exiting which prevents the code to create any.

int CheckFile = open(argv[2], O_RDONLY);
if (CheckFile < 0)
{
    //Means the file doesn't exist
    int CheckFile = open(argv[2], O_CREAT);
    // Check for errors  here
}

And why are you trying to do this::

writeout = write(openFD,bufsize,readin);

when your HANDLE TO OUTPUT FILE IS CheckFile



回答2:

    int CheckFile = open(argv[2], O_RDONLY);
    if (CheckFile < 0)
    {
        perror("Error output file already exists \n");

A negative return from open means that the file could not be opened, most likely because it does not exist ... it does not mean that the file already exists. Failing to open the input file certainly does not mean that the output file already exists. Please check your code more carefully for obvious errors, e.g.,

int CheckFile = open(argv[2], O_CREAT);
printf("The file has successfully been created \n");

Here you don't check the return code.



回答3:

Take a look at this fragment of your code:

int CheckFile = open(argv[2], O_RDONLY);
if (CheckFile < 0)
{
    perror("Error output file already exists \n");
    exit(-1);
}

You are saying trying to open a file in READ ONLY MODE. From what I read in your question, it's not an error if the file doesn't exist, but in the code you are validating the opposite, if the file doesn't exists, throw an error (in fact your message error is incorrect here).

Double check your logic and you will find your solution.



标签: c filesystems