打开Windows目录下的文件进行读取/ C来写 [英] open a windows file directory for reading/writing in c

查看:476
本文介绍了打开Windows目录下的文件进行读取/ C来写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个Windows目录的内容写入用c文件。举例来说,如果我有JPEG文件的目录(即包含多个JPEG文件的目录),并希望将其转换为一个.raw文件,我有这样的事情:

I'm trying to write the contents of a windows directory to a file using c. For example, if I had a directory of jpegs (i.e. a directory that contains multiple jpegs) and wanted to convert them to a .raw file, I have something like this:

#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>

typedef uint8_t BYTE;

#define BLOCK 512*sizeof(BYTE);

int main(void)
{
    FILE * fd = fopen("C:\\jpegs", "r");
    if (fd == NULL) {
        fprintf(stderr, "Error opening device file.\n");
        return EXIT_FAILURE;
    }
    int block = BLOCK;
    FILE * fn = fopen("new.raw", "w+");
    void * buff = malloc(block);
    while(feof(fd) == 0) {
        fread(buff,block,1,fd);
        fwrite(buff,block,1,fn);
    }
    free(buff);
    fclose(fd);
    fclose(fn);
    return 0;
}

问题是,我不认为窗口目录终止EOF。有没有人有关于如何解决这个任何想法?

The problem is I don't think windows directories are terminated with EOF. Does anyone have any ideas about how to solve this?

推荐答案

在Unix系统中,虽然可以打开一个目录的阅读,你不能真正从中读取,除非您使用执行opendir( ) READDIR() closedir()家庭电话。你不能写在Unix上的一个目录;即使是超级用户(root)不能做到这一点。 (主要原因与打开一个目录,更通常的open() fopen()函数,是为了让你可以使用 CHDIR()然后按 fchdir()要回你开始的地方,或使用各种 *在()的功能,如 openat(),来引用该目录。)

On Unix systems, although you can open a directory for reading, you can't really read from it unless you use the opendir(), readdir(), closedir() family of calls. You can't write to a directory on Unix; even superuser (root) can't do that. (The main reason for opening a directory, more usually with open() than fopen(), is so that you can use chdir() followed by fchdir() to get back to where you started, or use the various *at() functions, such as openat(), to reference the directory.)

在Windows中,你以最低需要使用RB模式,但坦率地说,我不希望你能够做多少吧。有可能是类似物Unix的执行opendir()功能在Windows API中,你应该使用这些替代。

On Windows, you'd at minimum need to use "rb" mode, but frankly, I'd not expect you to be able to do much with it. There are probably analogues to the Unix opendir() functions in the Windows API, and you should use those instead.

这篇关于打开Windows目录下的文件进行读取/ C来写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆