我是c ++的新手并且是我自己学习的,所以我有一个应该从文件路径读取指定文件的程序,理论上 做一个精确的副本。我的问题是我总是关闭(如字节数,例如,一个173 kb的文件是177kb),从我看到的文件越大,我越关闭。那么为什么我错了,有更好的方法吗?
int main()
{
//I was monitering memory usage, some reason if i wrote to console to fast, I couldn't moniter memory
system("pause");
ifstream inputFile;
inputFile.open("C:\\Users\\Tallennar\\Documents\\Tables.docx", ios::in | ios::binary);
ofstream outputFile;
outputFile.open("word.docx", ios::binary);
char buffer[257] = { ' ' };//one extra for \0
if (!inputFile)
printf("failed to open input file");
if (!outputFile)
printf("failed to open outputfile \n");
//gets my file size
inputFile.seekg(0, ios::end);
size_t fileSize = inputFile.tellg();
inputFile.seekg(0, ios::beg);
//some math to see how many times I need to loop
int leftOverFromIterations = fileSize % 256;
int fileSizeIterations = (fileSize - leftOverFromIterations) / 256;
int bufferSize = sizeof(buffer);
//loops through to print to output file
for (int i = 0; i <= fileSizeIterations; i++)
{
inputFile.read(buffer, bufferSize);
//so i dont get funny chars
buffer[256] = '\0';
outputFile.write(buffer, bufferSize);
//for me to see what is getting printed
std::cout << buffer;
}
//since not every file is divisible by 256, get the
// leftovers from the above for loop
inputFile.read(buffer, leftOverFromIterations);
//close files
inputFile.close();
outputFile.close();
system("pause");
return 0;
}
答案 0 :(得分:1)
几个问题:
for
循环运行次数过多,应使用i < fileSizeIterations
。
您正在使用\0
覆盖缓冲区的最后一个字符。您应该将bufferSize
设置为小于数组大小的1,因此您不会读入null所需的字符。或者您应该使用std::string
而不是C风格的字符串。
您没有将剩菜复制到输出文件。
您还应避免在整个代码中使用256
进行硬编码,并在那里使用bufferSize
。
int main()
{
//I was monitering memory usage, some reason if i wrote to console to fast, I couldn't moniter memory
system("pause");
ifstream inputFile;
inputFile.open("C:\\Users\\Tallennar\\Documents\\Tables.docx", ios::in | ios::binary);
ofstream outputFile;
outputFile.open("word.docx", ios::binary);
char buffer[257] = { ' ' };//one extra for \0
if (!inputFile)
printf("failed to open input file");
if (!outputFile)
printf("failed to open outputfile \n");
//gets my file size
inputFile.seekg(0, ios::end);
size_t fileSize = inputFile.tellg();
inputFile.seekg(0, ios::beg);
int bufferSize = sizeof(buffer)-1;
//some math to see how many times I need to loop
int leftOverFromIterations = fileSize % bufferSize;
int fileSizeIterations = (fileSize - leftOverFromIterations) / bufferSize;
//loops through to print to output file
for (int i = 0; i < fileSizeIterations; i++)
{
inputFile.read(buffer, bufferSize);
//so i dont get funny chars
buffer[bufferSize] = '\0';
outputFile.write(buffer, bufferSize);
//for me to see what is getting printed
std::cout << buffer;
}
//since not every file is divisible by bufferSize, get the
// leftovers from the above for loop
inputFile.read(buffer, leftOverFromIterations);
//so i dont get funny chars
buffer[leftOverFromIterations] = '\0';
outputFile.write(buffer, leftOverFromIterations);
//for me to see what is getting printed
std::cout << buffer;
//close files
inputFile.close();
outputFile.close();
system("pause");
return 0;
}
答案 1 :(得分:0)
为什么不使用getline()
呢?更容易混淆。
int main()
{
//I was monitering memory usage, some reason if i wrote to console to fast, I couldn't moniter memory
system("pause");
ifstream inputFile;
inputFile.open("C:\\Users\\Tallennar\\Documents\\Tables.docx", ios::in | ios::binary);
ofstream outputFile;
outputFile.open("word.docx", ios::binary);
if (!inputFile)
{
printf("failed to open input file");
return 0; //if you didn't open the file don't continue
}
if (!outputFile)
{
printf("failed to open outputfile \n");
return 0; //if you didn't open the file don't continue
}
string line;
while(getline(inputFile, line ))
{
outputFile << line;
}
inputFile.close();
outputFile.close();
system("pause");
return 0;
}
注意:我在您的if语句中添加了return 0;
,因此如果它无法打开文件就会停止。
免责声明:我没有运行此代码,因为我没有可以处理手头文件的编译器。