基本输入/输出 C++错误
我想要的只是一个 C++ 程序,它将读取 txt 文件,将每一行放入一个数组中,然后将副本打印到另一个 txt 文件中。这是我的代码...
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string STRING ="";
string list[10000];
int i = 0;
ifstream infile;
infile.open ("C:/Users/Ryan/Desktop/data.txt");
ofstream myfile;
myfile.open ("C:/Users/Ryan/Desktop/data-2.txt");
while(!infile.eof()) // To get you all the lines.
{
getline(infile,STRING);
list[i]=STRING;
myfile<<list[i];
++i;
}
infile.close();
myfile.close();
return 0;
}
出于某种原因,虽然这样做,但每隔一行都会给我一堆时髦的中文符号。这是我的 data.txt...
BPC 20101206 V 0.13 0.13 0.13 0
BPC 20101207 V 0.13 0.13 0.13 6500
BPC 20101208 V 0.13 0.13 0.13 0
BPC 20101209 V 0.13 0.125 0.125 117000
BPC 20101210 V 0.125 0.125 0.125 0
BPC 20101213 V 0.125 0.125 0.125 0
BPC 20101214 V 0.13 0.13 0.13 5000
BPC 20101215 V 0.13 0.13 0.13 290
BPC 20101216 V 0.125 0.115 0.115 24000
这是输出 data-2.txt...
BPC 20101206 V 0.13 0.13 0.13 0
䈀倀䌀ऀ㈀ ㈀ 㜀ऀ嘀ऀ ⸀㌀ऀ ⸀㌀ऀ ⸀㌀ऀ㘀㔀 ഀ BPC 20101208 V 0.13 0.13 0.13 0
䈀倀䌀ऀ㈀ ㈀ 㤀ऀ嘀ऀ ⸀㌀ऀ ⸀㈀㔀ऀ ⸀㈀㔀ऀ㜀 ഀ BPC 20101210 V 0.125 0.125 0.125 0
䈀倀䌀ऀ㈀ ㈀㌀ऀ嘀ऀ ⸀㈀㔀ऀ ⸀㈀㔀ऀ ⸀㈀㔀ऀ ഀ BPC 20101214 V 0.13 0.13 0.13 5000
䈀倀䌀ऀ㈀ ㈀㔀ऀ嘀ऀ ⸀㌀ऀ ⸀㌀ऀ ⸀㌀ऀ㈀㤀 ഀ BPC 20101216 V 0.125 0.115 0.115 24000
有什么想法吗?
All I want is a c++ program that will read in a txt file, put each row in an array, then print a duplicate copy into another txt file. Here's my code...
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string STRING ="";
string list[10000];
int i = 0;
ifstream infile;
infile.open ("C:/Users/Ryan/Desktop/data.txt");
ofstream myfile;
myfile.open ("C:/Users/Ryan/Desktop/data-2.txt");
while(!infile.eof()) // To get you all the lines.
{
getline(infile,STRING);
list[i]=STRING;
myfile<<list[i];
++i;
}
infile.close();
myfile.close();
return 0;
}
For some reason though doing this, every other line gives me a bunch of funky Chinese symbols. Here's my data.txt...
BPC 20101206 V 0.13 0.13 0.13 0
BPC 20101207 V 0.13 0.13 0.13 6500
BPC 20101208 V 0.13 0.13 0.13 0
BPC 20101209 V 0.13 0.125 0.125 117000
BPC 20101210 V 0.125 0.125 0.125 0
BPC 20101213 V 0.125 0.125 0.125 0
BPC 20101214 V 0.13 0.13 0.13 5000
BPC 20101215 V 0.13 0.13 0.13 290
BPC 20101216 V 0.125 0.115 0.115 24000
And here's the output data-2.txt...
BPC 20101206 V 0.13 0.13 0.13 0
䈀倀䌀ऀ㈀ ㈀ 㜀ऀ嘀ऀ ⸀㌀ऀ ⸀㌀ऀ ⸀㌀ऀ㘀㔀 ഀ BPC 20101208 V 0.13 0.13 0.13 0
䈀倀䌀ऀ㈀ ㈀ 㤀ऀ嘀ऀ ⸀㌀ऀ ⸀㈀㔀ऀ ⸀㈀㔀ऀ㜀 ഀ BPC 20101210 V 0.125 0.125 0.125 0
䈀倀䌀ऀ㈀ ㈀㌀ऀ嘀ऀ ⸀㈀㔀ऀ ⸀㈀㔀ऀ ⸀㈀㔀ऀ ഀ BPC 20101214 V 0.13 0.13 0.13 5000
䈀倀䌀ऀ㈀ ㈀㔀ऀ嘀ऀ ⸀㌀ऀ ⸀㌀ऀ ⸀㌀ऀ㈀㤀 ഀ BPC 20101216 V 0.125 0.115 0.115 24000
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了解决您最初的问题,您似乎正在输出一些未格式化的字符(恰好是中文)。我没有看到您插入新行(但输出中似乎有新行),因此您的代码中缺少一些您没有向我们展示的内容。 请剪切/粘贴真实代码。
需要注意的主要事项:
永远不要这样做:
您应该在使用之前阅读并测试该行。
这可以通过将读取放入条件中来在一行中完成:
其他需要注意的事项:
在一行中声明并打开文件
不要测试 EOF 作为循环条件。
不要手动关闭文件(让析构函数来做)
To address your original problem it looks like you are outputting some un-formatted characters (that happen to be Chinese). I don't see you inserting new lines (yet there seems to be new lines in the output) so there is something missing from your code that you are not showing us. Please CUT/PASTE REAL code.
Main thing to note:
Never ever ever do this:
You should read and test the line before using it.
This can be done in a single line by putting the read into the condition:
Other things to watch for:
Declare and open file in one line
Don't test for EOF as the loop conition.
Don't manually close the file (let the destructor do it)
如果我没记错的话,你的操作系统是Windows?
这些“中文”符号的原因是您的 .txt 文件是用 Unicode 编码的。在记事本中打开它,单击“另存为”,然后在“编码”下拉列表(位于对话框底部的某个位置)中选择“ANSI”,然后保存。这应该可以解决“中文”问题:)
检查其他答案以解决您的代码问题。希望有帮助。
If I'm not mistaken, your OS is Windows?
The reason for these "chinese" symbols is that your .txt file is encoded in Unicode. Open it in notepad click Save As and in the Encoding drop-down list (somewhere at the bottom of the dialog box) choose ANSI, then save. That should fix the "chinese" problem :)
Check other answers to fix the problems with your code. Hope that helps.
当您写回数据时,您不会包含换行符,因此如果您有意这样做,则不会创建重复文件。
似乎没有理由拥有 List[] 数组,因为无论如何你都直接将其写回,相反你可以这样做
btw STRING 不是一个好的变量名,选择更具描述性的名称。
when you write back the data you are not including the newline so you are not creating a duplicate file if that was your intention.
there seems no reason to have the List[] array since you anyway are writing it back directly, instead you could do
btw STRING is not a good variable name, choose something more descriptive.
这是一篇很好的文章,详细介绍了 while(!eof) 的替代方案... 此处
Here is a nice article detailing alternatives to while(!eof)... here