C++ でいくつかの大きなファイルの並べ替えに取り組んでいます。各行に 1 つずつ、すべての入力ファイルの名前を含むテキスト ファイルがあります。ファイル名を一度に 1 つずつ読み取り、それらを配列に格納してから、それらの名前ごとにファイルを作成したいと思います。現在、文字配列を必要とする fopen と fread を使用しているため (速度を最適化しようとしています)、ファイル名は文字配列の配列に読み込まれます。ただし、これらの配列には事前に最大サイズを固定する必要があるため、ファイル名が最大サイズよりも小さい場合、残りはゴミでいっぱいになります。次に、その配列を fopen() でファイル名として使用しようとすると、文字列の末尾にゴミがあるため、ファイルが認識されません。どうすればこの問題を解決できますか? これが私のコードです:
#include <iostream>
#include <fstream>
#include <string>
#include "stdafx.h"
#define NUM_INPUT_FILES 4
using namespace std;
FILE *fp;
unsigned char *buff;
FILE *inputFiles[NUM_INPUT_FILES];
int _tmain(int argc, _TCHAR* argv[])
{
buff = (unsigned char *) malloc(2048);
char j[8];
char outputstring[] = "Feelings are not supposed to be logical. Dangerous is the man who has rationalized his emotions. (David Borenstein)";
fp = fopen("hello.txt", "r");
string tempfname[NUM_INPUT_FILES];
//fp = fopen("hello.txt", "r");
for(int i=0;i<NUM_INPUT_FILES;i++)
{
fgets(tempfname[i], 20, fp);
cout << tempfname[i];
}
fclose(fp);
for(int i=0; i<NUM_INPUT_FILES;i++)
{
fp = fopen(tempfname[i], "w");
//fwrite(outputstring, sizeof(char), sizeof outputstring/sizeof(char), fp);
if(fp)
{
fclose(fp);}
else
cout << "sorry" << endl;
}
return 0;
}
また、fwrite() で書き出すバッファのサイズを調べるにはどうすればよいですか?
どうもありがとう、bsg