提问者:小点点

使用ifstream读取.txt并使用ofstream写入新的.txt文件,但仅第一行有效


我正在尝试读取一个文本文件text.txt,其中包含表示十六进制值的文本:

0123456789abcdef 
0123456789abcdef
0123456789abcdef
0123456789abcdef
0123456789abcdef
0123456789abcdef
0123456789abcdef 

我应该读取这个文件,并使用ofstream写入一个新文件output.txt来写入这些十六进制值的二进制等价值,其中-表示0,而#表示1。

示例:

0 = ----
1 = ---#
2 = --#-
...
F = ####

output.txt的输出是

---#--#---##-#---#-#-##--####---#--##-#-#-####--##-####-####

在应该的时候

---#--#---##-#---#-#-##--####---#--##-#-#-####--##-####-####
---#--#---##-#---#-#-##--####---#--##-#-#-####--##-####-####
---#--#---##-#---#-#-##--####---#--##-#-#-####--##-####-####
---#--#---##-#---#-#-##--####---#--##-#-#-####--##-####-####
---#--#---##-#---#-#-##--####---#--##-#-#-####--##-####-####
---#--#---##-#---#-#-##--####---#--##-#-#-####--##-####-####
---#--#---##-#---#-#-##--####---#--##-#-#-####--##-####-####

我的逻辑就在那里,但是output.txt似乎只写了text.txt的第一行。这让我相信我只是在读第一行。

我被迫使用C样式的字符串,因此我读入了char数组。

这是我的代码

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

int main()
{
    ifstream myfile;
    myfile.open("test.txt");

    ofstream outfile;
    outfile.open("output.txt"); //ofstream to output.txt based on character in words[]

    char words[10001] = {'\0'}; //c-style string
    if (myfile.is_open())
    {
        while (myfile >> words)
        {
            myfile >> words; //read myfile text into char words[]

            for (char c : words) //the ofstream to output.txt based on char c in words
            {
                if (c == '0')
                    outfile << "----";
                else if (c == '1')
                    outfile << "---#";
                else if (c == '2')
                    outfile << "--#-";
                else if (c == '3')
                    outfile << "--##";
                else if (c == '4')
                    outfile << "-#--";
                else if (c == '5')
                    outfile << "-#-#";
                else if (c == '6')
                    outfile << "-##-";
                else if (c == '7')
                    outfile << "-###";
                else if (c == '8')
                    outfile << "#---";
                else if (c == '9')
                    outfile << "#--#";
                else if (c == 'a')
                    outfile << "#-#-";
                else if (c == 'b')
                    outfile << "#-##";
                else if (c == 'c')
                    outfile << "##--";
                else if (c == 'd')
                    outfile << "##-#";
                else if (c == 'e')
                    outfile << "###-";
                else if (c == 'f')
                    outfile << "####";
            }
        }
        myfile.close();
    }

    return 0;
}

我怀疑是myfile>>words,但我不完全确定。我加了几条评论,试图解释一下我去的路线。


共2个答案

匿名用户

你用了

            ofstream outfile;
            outfile.open("output.txt");

在循环内。这使得文件在每次迭代中都是打开的,这将清除文件的内容。您应该在while循环之前移动它。

还要注意,您的条件while(!myfile.eof())是错误的。相反,您应该将读取myfile>>words移到条件中,以便在使用“read”之前检查读取是否成功。

匿名用户

我建议使用以下方法来处理文件:

ifstream infile("infile.txt");
ofstream outfile("outfile.txt");

if(infile.is_open()){
    while(!infile.eof()){
        //do the readings
    }
}else
{
    cout << "ERROR::FILE NOT SUCCESFULLY OPENED";
}