在文件中保存字符问题

发布于 2024-10-16 14:30:27 字数 1153 浏览 0 评论 0原文

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

问题是它可以很好地读取第一个文件的字符,但是当它写入第二个文件时,它会添加第一个文件中的所有字符,正如它应该做的那样,但是当没有更多字符时,它会添加很多字符文件末尾的“Ì”字符。

fx:

文件 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;

Hey.
I have some problems writing char to a file with ofstream.
this is how the code looks (Just to show how it works. This is NOT the real code).

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 

The problem is that it reads the characters of the first file fine, but when it writes to the second file, it adds all characters from the first file, as its supposed to do, but when there are no more characters, it adds a lot of "Ì" characters in the end of file.

fx:

file 1:
abc

file 2:
abcÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ...

How can I prevent the program save "Ì" in the file?

EDIT2:

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你穿错了嫁妆 2024-10-23 14:30:27

您需要将第二个缓冲区设置为空终止。您将添加从流中读取的所有字符,其中不包括尾随的 NULL。

fout 之前的行中添加

secondbuffer[j]='\0\';

You need to null-terminate secondbuffer. You are adding all the characters read from the stream, which do not include the trailing NULL.

on the line before fout, add

secondbuffer[j]='\0\';
中性美 2024-10-23 14:30:27

问题是您的文件中没有终止空字符。当你读入文件时,你会得到“abc”,但是声明它时位于 Secondbuffer 中的垃圾仍然存在,因此在它的开头写入“abc”意味着你有一个 5001 长度的数组以“abc”开头的垃圾。

添加secondbuffer[i] = '\0';

尝试在 for 循环后

The problem is that there is no terminating null character in your file. When you read the file in, you get "abc" just fine, but the garbage that was sitting in secondbuffer when it was declared is still there, so writing "abc" to the beginning of it means that you have a 5001-length array of garbage that starts with "abc."

Try adding

secondbuffer[i] = '\0'; after your for loop.

超可爱的懒熊 2024-10-23 14:30:27

这应该可以正常工作:

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
  char buffer[5001];
  char secondbuffer[5001];
  ifstream in("foo.txt", ifstream::in);
  ofstream fout("blah_copy.txt");
  do
    {
      in.getline(buffer,5001);
      fout<<buffer;
    }
  while(!in.eof());
  in.close();
  fout.close();
  return 0;
}

This should work fine:

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
  char buffer[5001];
  char secondbuffer[5001];
  ifstream in("foo.txt", ifstream::in);
  ofstream fout("blah_copy.txt");
  do
    {
      in.getline(buffer,5001);
      fout<<buffer;
    }
  while(!in.eof());
  in.close();
  fout.close();
  return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文