我正在使用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