使用 ifstream 读取 .txt 并使用 ofstream 写入新的 .txt 文件,但只有第一行有效
Reading .txt using ifstream and writing to new .txt file with ofstream, but only first line works
我正在尝试读取文本文件,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");
char words[10001] = {'[=14=]'}; //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;
}
我怀疑是 myfile >> words
,但我不完全确定。我添加了一些评论来尝试解释我所走的路线。
你用过
ofstream outfile;
outfile.open("output.txt");
在循环内。这使得文件在每次迭代中打开,这将清除文件的内容。您应该在 while
循环之前移动它。
另请注意,您的条件 while (!myfile.eof())
是 wrong。取而代之的是,您应该将阅读myfile >> words
移动到条件以检查阅读是否成功,然后再使用“阅读”。
我建议使用这种文件处理方法:
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";
}
我正在尝试读取文本文件,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");
char words[10001] = {'[=14=]'}; //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;
}
我怀疑是 myfile >> words
,但我不完全确定。我添加了一些评论来尝试解释我所走的路线。
你用过
ofstream outfile;
outfile.open("output.txt");
在循环内。这使得文件在每次迭代中打开,这将清除文件的内容。您应该在 while
循环之前移动它。
另请注意,您的条件 while (!myfile.eof())
是 wrong。取而代之的是,您应该将阅读myfile >> words
移动到条件以检查阅读是否成功,然后再使用“阅读”。
我建议使用这种文件处理方法:
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";
}