所以這是我用過一些自我創建的錯誤檢查,但由於某種原因,當我編譯這個使用運行它的第一方案之一:檢測文件不存在
./file test1.txt test2.txt 10
我得到絕對是一個錯誤,表明輸出文件存在,我檢查過這個文件,它甚至沒有改變輸出文件的名字(第二個參數),我什麼都沒有收到。任何人都可以幫忙?現在我已經把我的大腦折騰了好幾年了。這是我在Gentoo中編譯和運行的UNIX作業任務。我有它在VB中運行,並有我的Windows和Linux操作系統之間的鏈接文件夾。
#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;
}
的就是你得到實際的錯誤? – John3136
你不應該在自己的錯誤消息中使用'perror',因爲該函數使用'errno'來打印一條消息,'errno'只在系統函數失敗(或者你自己設置)時纔有效。在「打開」,「讀取」和「寫入」之後,您可以正確使用它。 –