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 thefseek
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
最满意答案
使用当前的目录设置,
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"));
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
The only default directory built into the classpath (/jar) is the
src
. We need to either put theresources
into thesrc
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
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编译(Why is Angular compiled)[2021-09-25]
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. ...
-
编译时出现图像问题(Problems with images when compiled)[2022-11-08]
使用当前的目录设置, 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 ...
-
编译时程序较慢(Program is slower when compiled)[2022-02-08]
几个月前,我配置了一个调试工具,可以在程序运行时将其自身附加到程序中。 我完全忘记了我做过这件事。 特别感谢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 ...