我正在使用C++对多个大文件进行排序。我有一个文本文件,其中包含所有输入文件的名称,每行一个。我想一次读取一个文件名,将它们存储在一个数组中,然后用这些名称创建一个文件。现在,我正在使用fopen和fread,它们需要字符数组(我试图优化速度),所以我的文件名被读入字符数组数组中。但是,这些数组需要事先确定最大大小,所以如果文件名小于最大值,其余的就会被垃圾填满。然后,当我尝试在fopen()中使用该数组作为文件名时,它无法识别该文件,因为它在该字符串的末尾有垃圾。我怎么解决这个问题?这里是我的代码:从另一个文件中的文件名创建文件C++
#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