简体   繁体   English

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

[英]Reading .txt using ifstream and writing to new .txt file with ofstream, but only first line works

I am trying to read a textfile, text.txt with text representing hexadecimal values:我正在尝试使用表示十六进制值的文本读取文本文件text.txt

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

I am supposed to read this file and use ofstream to write to a new file output.txt to write the binary equiavalent of these hexademical values, with - representing 0's and # representing 1's.我应该读取此文件并使用ofstream写入新文件output.txt以写入这些十六进制值的二进制等价物,其中-代表 0, #代表 1。

Example:例子:

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

And my output for output.txt is而我的 output 为output.txt

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

when it should be什么时候应该

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

My logic is there, but it seems that output.txt only writes the first line of text.txt .我的逻辑在那里,但似乎output.txt只写了text.txt的第一行。 This makes me believe that I am only reading the first line.这让我相信我只是在阅读第一行。

I am forced to use c-style strings, hence the char array I am reading into.我被迫使用 c 风格的字符串,因此我正在读入 char 数组。

Here is my code这是我的代码

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

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

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

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


            for (char c : words) //the ofstream to output.txt based on char c in words
            {
                if (c == '0')
                    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;
}

I suspect it's the myfile >> words , but I am not entirely sure.我怀疑这是myfile >> words ,但我不完全确定。 I added a few comments to try and explain the route I went.我添加了一些评论来尝试解释我去的路线。

You used你用过

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

inside the loop.循环内。 This makes the file opened in each iteration and this will clear the contents of file.这使得文件在每次迭代中打开,这将清除文件的内容。 You should move this befoer the while loop.您应该在while循环之前移动它。

Also note that your condition while (.myfile.eof()) is wrong .另请注意,您的条件while (.myfile.eof())错误的。 Instead of this, you should move the reading myfile >> words to the condition to check if reading is successful before using what is "read".取而代之的是,您应该将读取myfile >> words移动到条件以在使用“读取”之前检查读取是否成功。

I would suggest this approach for the file handling:我建议使用这种方法进行文件处理:

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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM