【问题标题】:fseek() and ftell() fail in a loopfseek() 和 ftell() 循环失败
【发布时间】:2017-04-14 13:21:44
【问题描述】:

我需要遍历一个目录 data 并读取每个满足特定条件的文件,在一个字符串中并对其进行处理。由于某种原因,它在fseek 调用后失败(输出只是目录中第一个文件的名称)。

知道我做错了什么吗?

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

void doAlgorithm(char *input) {
    printf("%s\n", input);
}

int main(int argc, char** argv) {
    struct dirent *dir;
    DIR *d = opendir("data");
    FILE *file;
    while ((dir = readdir(d)) != NULL) {
        if (strlen(dir->d_name) > 6 && dir->d_name[6] == 'i') {
            printf("Filename: %s\n", dir->d_name);
            file = fopen(dir->d_name, "r");
            fseek(file, 0, SEEK_END);
            long length = ftell(file);
            fseek(file, 0, SEEK_SET);
            printf(", Filesize: %ld\n", length);

            char *buffer = malloc(length + 1);
            fread(buffer, 1, length, file);
            buffer[length] = '\0';

            fclose(file);
            doAlgorithm(buffer);
        }
    }
    closedir(d);
    return (EXIT_SUCCESS);
}

【问题讨论】:

  • ftell 返回一个long int 太小
  • 您也未能在各处检查返回值。仅仅因为你要求 length 字节并不意味着你得到了它们。
  • 好的,我解决了这个问题,但它仍然没有改变任何东西,在 fseek() 调用之后程序仍然无法正常工作。顺便说一句,如果重要的话,我会在 Windows 上运行它。
  • 问题是 fseek 调用出现问题,如果我尝试打印 fseek 或 ftell 返回值,它不会打印任何内容

标签: c file fread fseek ftell


【解决方案1】:

您的问题是您file = fopen(dir-&gt;d_name, "r"); 不知道该文件在目录中的位置。你需要给它完整的路径。你可以这样做;

    struct dirent *dir;
   // put the directory path here. on windows is \ instead of /
    char *path = "/Users/adnis/CLion/Stackoverflow/testdir";
    char *slash = "";
    DIR *d = opendir(path);
    FILE *file;
        while ((dir = readdir(d)) != NULL) {
       if (strlen(dir->d_name) > 6 && dir->d_name[6] == 'i') {
                printf("Filename: %s\n", dir->d_name);
                int length = strlen(path);
        /*check if the path already contains a '/' at 
          the end before joining the filename to the directory*/

                if(path[strlen(path)-1] != '/'){ //on windows is '\'
                   slash = "/";
                }

                length += strlen(dir->d_name)+2;
     // allocate memory for the new path
     // and make sure we have enough memory.
                char *newpath = malloc(length);

                assert(newpath != NULL);

                snprintf(newpath,length,"%s%s%s",path,slash,dir->d_name);

                file = fopen(newpath, "r");
                if(file == NULL){
                    fprintf(stderr, "fopen: %s\n", strerror(errno));
                    break;
                }
                fseek(file, 0, SEEK_END);
                long len = ftell(file);
                fseek(file, SEEK_SET, 0);

                char *buffer = malloc(len + 1);
                fread(buffer, 1, len, file);
                buffer[strlen(buffer)] = '\0';

                printf("%s \n",buffer);
                fclose(file);
            }
        }
        closedir(d);
        return (EXIT_SUCCESS);

我建议在阅读目录时你也必须尽量避免阅读“。”和“..”,因为它们只是当前目录和上一个目录。这样的事情会有所帮助。在你的 while 循环中

if(strcmp(dir->d_name,".") == 0 || strcmp(dir->d_name,"..") == 0)
            continue;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    相关资源
    最近更新 更多