使用C语言进行文件检查

美智

我需要问您一个更好的方法来检查文件是否存在,因为我真的不知道哪种方法是正确的。我有一个运行良好的程序,但是当我必须处理不同类型的文件时,而不仅仅是说txt文件,就会出现问题我是Linux用户,如果尝试在Library文件中使用该程序,我会尝试查看该程序的工作方式。

这是问题所在,因为我使用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;
}

谢谢你们。

美智

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

这是Linux解决方案,因为我不是Windows用户,并且对于我而言,使用即可检查C中是否存在文件就足够了access

这是代码:

#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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章