2012-09-21 28 views
1

所以這是我用過一些自我創建的錯誤檢查,但由於某種原因,當我編譯這個使用運行它的第一方案之一:檢測文件不存在

./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; 
} 
+0

的就是你得到實際的錯誤? – John3136

+3

你不應該在自己的錯誤消息中使用'perror',因爲該函數使用'errno'來打印一條消息,'errno'只在系統函數失敗(或者你自己設置)時纔有效。在「打開」,「讀取」和「寫入」之後,您可以正確使用它。 –

回答

1

開放呼籲HANDLE CheckFile是印刷錯誤文件存在。這是你的問題。當未找到輸出文件而且您是退出時,您正在輸出錯誤的語句,從而阻止代碼創建任何文件。

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 
} 

爲什麼你想這樣做::

writeout = write(openFD,bufsize,readin); 

當你句柄輸出文件CheckFile

+0

啊,我看到了問題。我在這裏也沒有達到我的要求,如果文件存在,我的意思是停止該程序。我需要實現一些代碼來做到這一點,現在我已經糾正了這個問題。它正在創建文件,雖然我現在正在收到文件寫入錯誤:錯誤地址。 –

+0

如果CheckFile沒有返回-1,退出代碼,否則創建輸出文件:-) – Abhineet

+0

是的,請打開輸出文件的權限和正確的訪問:: http://gd.tuwien.ac.at/languages/ c/programming-bbrown/c_075.htm – Abhineet

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

open負的返回表示該文件無法打開,很可能是因爲它不存在......這並不意味着該文件已經存在。無法打開輸入文件當然不意味着輸出文件已經存在。請更仔細地檢查你的代碼有明顯錯誤,例如,

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

在這裏,你不檢查返回代碼。

+0

好吧,我已經改爲讀寫模式,但是我仍然收到錯誤。正如指出的那樣,<0 or > 0並不一定意味着明確的失敗或成功或某事,但我正在脫離規範和一些示例代碼,這似乎被接受在這個級別。讀/寫功能依然無濟於事。 –

1

看看你的代碼的這個片段:

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

你的意思是試圖READ打開一個文件只模式。根據我在你的問題中讀到的內容,如果該文件不存在,那麼這不是錯誤,但是在你正在驗證相反的代碼中,如果該文件不存在,則拋出一個錯誤(事實上,你的消息錯誤在這裏是不正確的)。

仔細檢查你的邏輯,你會發現你的解決方案。