这个目录下有eddy的文件吗?如果没有这个文件肯定是打不开的。
注意下文件路径和文件名,特别是扩展名(你的电脑可能隐藏了扩展名)
1.fgetc判断是否为EOF
2.fgets返回的是指针 所以我们判断是否为NULL
根据问题描述,第二个文件是一个文件夹,而不是一个文件。因此,需要使用文件夹的操作函数来打开它,并对其中的文件进行操作。
可以使用Windows.h头文件中的FindFirstFile和FindNextFile函数来遍历文件夹中的文件,并对它们进行压缩处理。具体做法可以参考以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#include <zlib.h>
// 压缩文件函数
int compress_file(char *src_path, char *dst_path)
{
FILE *src_file = fopen(src_path, "rb");
if (src_file == NULL)
{
printf("Failed to open source file!\n");
return -1;
}
FILE *dst_file = fopen(dst_path, "wb");
if (dst_file == NULL)
{
printf("Failed to create destination file!\n");
fclose(src_file);
return -1;
}
// 使用zlib库进行压缩
int ret, flush;
unsigned have;
z_stream strm;
unsigned char in[4096];
unsigned char out[4096];
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
if (ret != Z_OK)
{
printf("Failed to initialize zlib!\n");
fclose(src_file);
fclose(dst_file);
return -1;
}
do
{
strm.avail_in = fread(in, 1, 4096, src_file);
if (ferror(src_file))
{
printf("Failed to read source file!\n");
deflateEnd(&strm);
fclose(src_file);
fclose(dst_file);
return -1;
}
flush = feof(src_file) ? Z_FINISH : Z_NO_FLUSH;
strm.next_in = in;
do
{
strm.avail_out = 4096;
strm.next_out = out;
ret = deflate(&strm, flush);
if (ret == Z_STREAM_ERROR)
{
printf("Failed to compress data!\n");
deflateEnd(&strm);
fclose(src_file);
fclose(dst_file);
return -1;
}
have = 4096 - strm.avail_out;
if (fwrite(out, 1, have, dst_file) != have || ferror(dst_file))
{
printf("Failed to write compressed data!\n");
deflateEnd(&strm);
fclose(src_file);
fclose(dst_file);
return -1;
}
} while (strm.avail_out == 0);
} while (flush != Z_FINISH);
deflateEnd(&strm);
fclose(src_file);
fclose(dst_file);
return 0;
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
printf("Usage: %s [source folder] [destination folder]\n", argv[0]);
return -1;
}
char src_path[MAX_PATH];
char dst_path[MAX_PATH];
WIN32_FIND_DATA find_data;
HANDLE find_handle;
// 构造源文件路径和目标文件路径
sprintf(src_path, "%s\\*", argv[1]);
sprintf(dst_path, "%s\\", argv[2]);
find_handle = FindFirstFile(src_path, &find_data);
if (find_handle == INVALID_HANDLE_VALUE)
{
printf("Failed to open source folder!\n");
return -1;
}
do
{
// 忽略.和..文件夹
if (strcmp(find_data.cFileName, ".") == 0 || strcmp(find_data.cFileName, "..") == 0)
continue;
// 构造源文件路径和目标文件路径
sprintf(src_path, "%s\\%s", argv[1], find_data.cFileName);
sprintf(dst_path, "%s\\%s.z", argv[2], find_data.cFileName);
// 压缩文件
if (compress_file(src_path, dst_path) != 0)
{
printf("Failed to compress file: %s\n", src_path);
}
} while (FindNextFile(find_handle, &find_data));
FindClose(find_handle);
return 0;
}
该程序的命令行参数为源文件夹路径和目标文件夹路径,它会遍历源文件夹中的所有文件(不包括.和..文件夹),对它们进行压缩处理,并将结果保存在目标文件夹中,文件名后缀为.z。