用C语言检查文件

时间:2015-05-30 15:22:48

标签: c

我需要问你一个更好的方法来检查一个文件是否存在,因为我真的不知道哪个是正确的方法。 我有一个工作正常的程序,但问题出现在我必须处理不同类型的文件,而不仅仅是,让我们说txt文件。 我是Linux用户,如果我尝试在库文件中使用它,我试着看看程序是如何工作的。

这是问题,程序无法读取该文件,因为我使用READ MODE "r",并且库文件不是txt文件。 如何检查该文件是否存在?

#include <stdio.h>
#include <stdlib.h>

int main (void){
    FILE * fp;
    char *fileNAme = "secret.txt";
    size_t fileLong;
    char * buffer;
    size_t result;

    /*Check if the file exists, if not Exit*/
    fp = fopen (fileNAme , "r");
    if (fp==NULL){
        printf("\n\n\t\t\tThe file %s does not Exists\n\n\n\n\n", fileNAme);
        exit(1);
    }

    /* Read the Number of Chars present and store them in FileLong*/
    fseek (fp , 0 , SEEK_END);
    fileLong = (size_t)ftell (fp);

    /* Print the size of the FileLong */
    if(fileLong == 0){
        printf("The Size of the File is \t= %ld\n",fileLong);
    }else{
        printf("The Size of the File is \t= %ld\n",fileLong-1);
    }

    /* Moving the pointer back to the begining of the file */
    rewind (fp);

    /* allocate memory to the buffer to which is needed for holding the whole File content */
    buffer = malloc (sizeof(char)*fileLong);

    /* Check if the memory was aloccated, if not print the Error and Exit */
    if (buffer == NULL){
        fputs ("Memory error",stderr);
        exit (1);
    }

    /* copy the file into the buffer:*/
    result = fread (buffer,1,fileLong,fp);

    /*Print the size of the Buffer*/
    if(result == 0){
        printf("The size of the Buffer is \t= %ld\n",result);
    }else{
        printf("The size of the Buffer is \t= %ld\n",result-1);
    }

    /* Check if the Buffer have the same size as lsize */
    if (result != fileLong){
        fputs ("Reading error",stderr);
        exit (1);
    }else{
        printf("\nThe Content of the File(%ld)\tis the same as the content of the Buffer(%ld)\n", fileLong-1, result-1);
    }

    /* Now we can print the Content of the File */
    printf("\n\n\n\n\t\tThe Content of the File is:\n");

    /* Compare result with FileLong to see if there are some information inside that File or not */
    if(result != 0 && fileLong != 0){
        printf("%s", buffer);
    }else{
        printf("\n\n\nThe File is empty\n\n\n");
    }

    /* Close that File */
    fclose (fp);

    /* Free the needed memory*/
    free (buffer);
    return 0;
}

谢谢大家。

3 个答案:

答案 0 :(得分:3)

您可以使用access() API和F_OK来检查文件是否存在。

您需要包含头文件unistd.h

FWIW,您还可以使用R_OKW_OKX_OK来检查文件是否具有读取,写入和执行权限。

以下是使用access的方法:

if( !access( filename, F_OK )) 
{
    // file is present
}
else 
{
    // file is not there
}

答案 1 :(得分:3)

它的文件类型并不重要,fopen如果可以打开则仍会返回有效的FILE指针。因此,一种简单的方法就是使用fopen并查看它是否返回NULL

另一种更多的Linux(或更确切地说是POSIX)方式可能是使用access函数,或者可能使用stat函数。

答案 2 :(得分:0)

我将发布我的解决方案的一部分。

这是Linux的工作,因为我不是Windows用户,根据我的需要,使用access检查C中是否存在文件就足够了。

以下是代码:

#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>

void fileCheck(const char *fileName){

    if(!access(fileName, F_OK )){
        printf("The File %s\t was Found\n",fileName);
    }else{
        printf("The File %s\t not Found\n",fileName);
    }

    if(!access(fileName, R_OK )){
        printf("The File %s\t can be readed\n",fileName);
    }else{
        printf("The File %s\t cannot be readed\n",fileName);
    }

    if(!access( fileName, W_OK )){
        printf("The File %s\t it can be Edited\n",fileName);
    }else{
        printf("The File %s\t it cannot be Edited\n",fileName);
    }

    if(!access( fileName, X_OK )){
        printf("The File %s\t is an Executable\n",fileName);
    }else{
        printf("The File %s\t is not an Executable\n",fileName);
    }
}

int main (void) {
    char *fileName = "/boot/grub/grub.cfg";

    fileCheck(fileName);
    return 0;
}

输出:

The File /boot/grub/grub.cfg     was Found
The File /boot/grub/grub.cfg     can be readed
The File /boot/grub/grub.cfg     it cannot be Edited
The File /boot/grub/grub.cfg     is not an Executable