我是一名初學者程式員(目前正在學習如何使用哈希表和嘗試之類的東西),因此資訊不靈通,并且會重視您的建議。
我想寫一個程式:
- 接收目錄地址作為 argv。
- 逐個遍歷該目錄中的每個檔案(它們都將是 BMP)并在讀取到緩沖區后...
- 對該緩沖區中的 RGB 值執行函式,沒什么特別的——想象一下像框模糊或灰度函式。
- 將緩沖區保存到新檔案夾中的檔案中,關閉當前正在訪問的原始目錄中的檔案,然后移動到下一個目錄,直到到達最終檔案。
我正在盡我所能嘗試 dirent,但無論我如何表達這個問題,我最終都會得到一些東西,告訴我如何讀取檔案名并列出它們,以及如何將它們讀入一個本身不包含的 dirent 結構檔案資料;我對專門訪問目錄并在其中查找檔案一無所知,其明確目的是對它們進行 fopen() 操作。
我的代碼摘錄,給你一個我(可能很糟糕)邏輯的例子:
DIR *folder;
folder = opendir(argv[3]);
if (folder == NULL);
{
printf("Unable to read folder");
return 2;
}
struct dirent *input;
FILE *fileinput;
int files = 0;
// Use this file loop to go through each
while( (input = readdir(folder)) != NULL )
{
fileinput = fopen(input->d_name, "r");
if (filepointer != NULL)
{
// checks for file headers, open another FILE for writing, my actual function etc.
}
但同樣,似乎 FOPEN 正在訪問一個名稱的副本,而不是檔案本身表明如此。而且我根本沒有詞匯可以在 SO 或其他地方找到類似的問題來回答這個問題。
有人介意指出我正確的方向嗎?為任何麻煩道歉,因為我確信這是一個非常基本的問題......
ーーーー編輯:要求發布更新的代碼以供審查:
#include <dirent.h> //必要
#include <sys/types.h>
#include "helpers.h" //bmp.h declared within
#include <getopt.h> //parse argvs
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <string.h>
const int PATH_MAX = 260;
int main(int argc, char *argv[])
{
char *filters = "rbg";
char filter = getopt(argc, argv, filters);
if (filter == '?') {
printf("Invalid filter.\nUsage: ./colourfilter [flag]\n r = red\t b = blue\t g = green\n");
return 2;
}
if (getopt(argc, argv, filters) != -1) {
printf("Only one filter may be used.\n");
return 3;
}
// OPEN INPUT FOLDER
const char *inputs = "inputs";
DIR *infolder = opendir(inputs);
if (infolder == NULL) {
//fprintf(stderr,"Unable to read folder %s\n", infolder);
printf("Unable to read folder.\n");
return 4;
}
// Declare variables
struct dirent *input;
int counter = 0;
char name[8];
FILE *imgout;
while((input = readdir(infolder)) != NULL)
{
char path[PATH_MAX];
if (!strcmp(input->d_name, ".") || !strcmp(input->d_name, "..")) {
continue;
}
if ((size_t)snprintf(path, sizeof(path), "%s/%s", infolder, input->d_name) >= sizeof(path)) {
printf("Filename too long: %s/%s\n", infolder, input->d_name);
continue;
}
// FOPEN THINGS
// "Also make sure you open the BMP files as binary with "rb" and "wb".:" (see: https://stackoverflow.com/questions/71321367/)
sprintf(name, "i.bmp", counter);
FILE *imgin = fopen(path, "rb");
imgout = fopen(name, "wb");
if (imgin == NULL) {
printf("Could not open %s.\n", path);
return 7;
}
if (imgout == NULL) {
fclose(imgin);
printf("Could not create images.\n");
return 8;
}
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, imgin);
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, imgin);
// Ensure infile is (likely) a 24-bit uncompressed BMP 4.0
if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
bi.biBitCount != 24 || bi.biCompression != 0)
{
fclose(imgout);
fclose(imgin);
printf("Unsupported file format.\n");
return 8;
} // ... other stuff after this for implementing functions etc.
uj5u.com熱心網友回復:
問題是fopen(input->d_name, "r");嘗試在當前目錄中打開檔案,而不是在argv[3]. 您必須在單獨的字串中構造檔案的路徑。
還要確保使用"rb"和將 BMP 檔案作為二進制檔案打開"wb"。
char *foldername = argv[3];
DIR *folder = opendir(foldername);
if (folder == NULL) {
fprintf(stderr, "Unable to read folder %s\n", foldername);
return 2;
}
struct dirent *input;
FILE *fileinput;
int files = 0;
// Use this file loop to go through each
while ((input = readdir(folder)) != NULL) {
char path[PATH_MAX];
if (!strcmp(input->d_name, ".") || !strcmp(input->d_name, "..")) }
continue;
}
if ((size_t)snprintf(path, sizeof path, "%s/%s", foldername, input->d_name) >= sizeof path) {
fprintf(stderr, "filename too long: %s/%s\n", foldername, input->d_name);
continue;
}
fileinput = fopen(path, "rb");
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/440155.html
