首页 \ 问答 \ fseek()和ftell()在循环中失败(fseek() and ftell() fail in a loop)

fseek()和ftell()在循环中失败(fseek() and ftell() fail in a loop)

我需要通过一个目录, 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);
}

I need to loop trough a directory, data and read each file, that meets certain conditions, in a string and do something with it. For some reason it fails after the fseek call (the output is only the name of the first file in the directory).

Any idea what am I doing wrong?

#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);
}

原文:https://stackoverflow.com/questions/43412417
更新时间:2022-05-30 17:05

最满意答案

使用当前的目录设置, images目录甚至不会内置到jar中。 尝试提取它,你很可能会看到它不在那里。

您可以通过它在文件夹中没有小包装徽标来说明,如此处所示

在这里输入图像描述

构建到类路径(/ jar)中的唯一默认目录是src 。 我们需要将resources放入src

在这里输入图像描述

或者将构建路径配置为包含resources的文件。 一旦我们这样做,我们将看到文件夹图标内的小包图标。 这就是我们知道文件在构建路径上的方式

在这里输入图像描述


我们将使用CODE:

  • 第一张图片:不能,它不会工作(这是你目前的困境)

  • 第二张图片:

    ImageIcon icon = new ImageIcon(
             getClass().getResource("/resources/stackoverflow.png"));
    
  • 第三张图片:

    ImageIcon icon = new ImageIcon(
             getClass().getResource("/stackoverflow.png"));
    

要配置构建路径以使用第三个选项,请按照此答案中的示例2中的说明进行操作


With your current directory setup, the images dir won't even get built into the jar. Try to extract it and you will most likely see that it's not in there.

You can tell by the fact that it doesn't have the little package logo in the folder, as seen here with resources

enter image description here

The only default directory built into the classpath (/jar) is the src. We need to either put the resources into the src

enter image description here

or configure the build path to include the files that are in the resources. Once we do that, we will see the little package icon inside the folder icon. That's how we know the files are on the build path

enter image description here


CODE we would use:

  • First Image: Can't, it won't work (this your current predicament)

  • Second Image:

    ImageIcon icon = new ImageIcon(
             getClass().getResource("/resources/stackoverflow.png"));
    
  • Third Image:

    ImageIcon icon = new ImageIcon(
             getClass().getResource("/stackoverflow.png"));
    

To configure the build path to use the third option, follow the instructions in Example 2 in this answer

相关问答

更多
  • Angular构建在Typescript上,而不是Javascript。 由于Typescript只是一种语言扩展(wiki),浏览器无法解释它。 编译器(或更精确的Transpiler)将Angular-Typescipt代码转换为浏览器可以解释的Javascript-Code。 Angular builds on Typescript, not Javascript. Since Typescript is just a language Extension (wiki) Browsers can't ...
  • 基本上问题是php没有用GD选项编译,所以这意味着对imageantialias()的调用失败了。 删除该行,从而删除了该错误的功能,请参阅下面的php手册所说的内容, http://php.net/manual/en/function.imageantialias.php Note: This function is only available if PHP is compiled with the bundled version of the GD library. 虽然dos没有解决问题,但如果其 ...
  • 看看LESS选项 ,我相信你已经设置了rootpath来添加该前缀。 所以重置根rootpath它应该没问题。 你可以在这里测试它 - LESS2CSS ,只需添加该选项。 Looking at the LESS options, I believe you have rootpath set to add that prefix. So reset the rootpath and it should be fine. You can test it here - LESS2CSS, just add t ...
  • 在您的九个补丁中缺少顶线,问题出在哪里顶部应该有一些像素来告诉您要拉伸的内容,就像您在左侧所做的那样。 没有顶部和左边的九个补丁没有完成。 切换链接在此处输入链接描述 the top line is missing in your nine patch that is where the problem lies there should be some pixels on the top to tell what to stretch like u have done in the left side. ...
  • 使用当前的目录设置, images目录甚至不会内置到jar中。 尝试提取它,你很可能会看到它不在那里。 您可以通过它在文件夹中没有小包装徽标来说明,如此处所示 构建到类路径(/ jar)中的唯一默认目录是src 。 我们需要将resources放入src 或者将构建路径配置为包含resources的文件。 一旦我们这样做,我们将看到文件夹图标内的小包图标。 这就是我们知道文件在构建路径上的方式 我们将使用CODE: 第一张图片:不能,它不会工作(这是你目前的困境) 第二张图片: ImageIcon icon ...
  • 1,FFmpeg可以是app或一组库。 如果将其用作应用程序(安装了可执行二进制文件),则可以在终端中键入命令。 该应用程序功能有限,可能无法解决您的问题。 在这种情况下,您需要使用ffmpeg作为库并在程序中调用API。 2,据我所知,这些命令无法解决您的问题。 您需要调用ffmpeg API。 有许多用于视频/图像编码/解码的示例代码。 您可能还需要一个容器来打包结果,而ffmpeg库也可以这样做。 3,NDK是我的首选,因为ffmpeg是用C / C ++编写的。 ffmpeg有JAVA包装器; 如果 ...
  • 资源文件(.qrc)用于放置静态文件,如图标,声音,视频等。这些文件通过pyrcc4或pyrcc5命令转换为python代码,并加载到内存中,因此不能在线修改。 在C ++的情况下,它们被加载到可执行文件中。 在这两种情况下,它们都不能被修改。 Resource files(.qrc) are used to place static files such as icons, sounds, videos, etc. These are converted into python code with the ...
  • 目前,这是PostSharp 4.3.21中的一个已知问题,计划在4.3.22版本中发布。 同时,作为解决方法,您可以在编译的图像对话框中选择“永不问我”选项。 升级到4.3.22后,您可以在Visual Studio中的PostSharp选项对话框中再次启用编译后的图像。 Currently, it's a known issue in PostSharp 4.3.21 and the fix is planned to be released with version 4.3.22. In the me ...
  • 您应该在您的案例中使用资产管道助手,特别是因为指纹字符串会附加到资产的名称。 要做到这一点: 1.将对字体/图像文件的引用更改为 使用资产助手 <%= asset_path 'fontFileNameAndExtension' %> 使用资产形象助手 <%= image_path 'imageFileNameAndExtension' %> 2.将.js文件的扩展名更改为.js.erb 3.将您的js文件的名称包含到application.js中 //= require YourJsFileName ...
  • 几个月前,我配置了一个调试工具,可以在程序运行时将其自身附加到程序中。 我完全忘记了我做过这件事。 特别感谢Process Monitor使这一点非常明显。 关闭它使程序运行得很快。 AppVerifier ,对于那些好奇的人。 Several months ago, I configured a debugging tool to attach itself to my program whenever it ran. I totally forgot that I had done this. Spec ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)