嘿。我在使用 ofstream 将 char 写入文件时遇到一些问题。这就是代码的样子(只是为了展示它是如何工作的。这不是真正的代码)。
char buffer[5001];
char secondbuffer[5001];
char temp;
ifstream in(Filename here);
int i = 0;
while(in.get(secondbuffer) && !in.eof[])
{
i++;
}
for(int j = 0; j < i; j++)
{
secondbuffer[j] = buffer[j];
}
ofstream fout(somefile);
fout << secondbuffer;
// end of program
问题是它可以很好地读取第一个文件的字符,但是当它写入第二个文件时,它会添加第一个文件中的所有字符,就像它应该做的那样,但是当没有更多字符时,它会添加很多文件末尾的“Ì”字符。
外汇:
文件 1: abc
文件2: abcÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ...
如何防止程序在文件中保存“Ì”?
编辑2:
int i = 0;
lenghtofFile++;
while(fin.get(firstfileBuffer[i]) && !fin.eof())
{
i++;
lenghtofFile++;
}
firstfileBuffer[i] = '\0';
for(int j = 0; j < lenghtofFile; j++)
{
if(secondfileBuffer[j] != ' ' && secondfileBuffer[j] != '\0')
{
secondfileBuffer[j] = function(key, firstfileBuffer[j]);
}
}
secondfileBuffer[lenghtofFile]='\0';
fout << secondfileBuffer;