基本输入/输出 C++错误

发布于 2024-12-19 18:26:35 字数 1657 浏览 5 评论 0原文

我想要的只是一个 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 技术交流群。

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

发布评论

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

评论(4

绝影如岚 2024-12-26 18:26:36

为了解决您最初的问题,您似乎正在输出一些未格式化的字符(恰好是中文)。我没有看到您插入新行(但输出中似乎有新行),因此您的代码中缺少一些您没有向我们展示的内容。 请剪切/粘贴真实代码

  1. 真正的代码是什么。
  2. 如何从文件生成输出(是在编辑器中打开它吗)?

需要注意的主要事项:

永远不要这样做:

   while(!infile.eof())

您应该在使用之前阅读并测试该行。
这可以通过将读取放入条件中来在一行中完成:

    while(getline(infile,STRING))
    {
        list[i]=STRING;
        myfile<<list[i];
        ++i;
    }

其他需要注意的事项:

  1. 很好地格式化您的代码!!!!!!
  2. 不要使用全部大写的标识符(这些通常是为宏保留的)。
  3. 不要使用数组。使用 std::vector 代替。
  4. 在一行中声明并打开文件

    ifstream infile("C:/Users/Ryan/Desktop/data.txt");
    
  5. 不要测试 EOF 作为循环条件。

    • 如果存在其他问题,可能会导致无限循环。
    • 如果您不检查读取是否有效,那么您将重复最后一行的处理。
  6. 不要手动关闭文件(让析构函数来做)

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.

  1. What is the real code.
  2. How do you generate the output from the files (was it cat did you open it in an editor)?

Main thing to note:

Never ever ever do this:

   while(!infile.eof())

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:

    while(getline(infile,STRING))
    {
        list[i]=STRING;
        myfile<<list[i];
        ++i;
    }

Other things to watch for:

  1. Format your code nicely!!!!!!
  2. Don't use all caps identifiers (these are usually reserved for macros).
  3. Don't use arrays. Use a std::vector instead.
  4. Declare and open file in one line

    ifstream infile("C:/Users/Ryan/Desktop/data.txt");
    
  5. Don't test for EOF as the loop conition.

    • It can cause an infinite loop if there is another problem.
    • If you don't check the read worked then you repeat the processing of the last line.
  6. Don't manually close the file (let the destructor do it)

凉城 2024-12-26 18:26:36

如果我没记错的话,你的操作系统是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.

和我恋爱吧 2024-12-26 18:26:36

当您写回数据时,您不会包含换行符,因此如果您有意这样做,则不会创建重复文件。

似乎没有理由拥有 List[] 数组,因为无论如何你都直接将其写回,相反你可以这样做

getline(infile,STRING);
myfile << STRING << endl;  

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

getline(infile,STRING);
myfile << STRING << endl;  

btw STRING is not a good variable name, choose something more descriptive.

寄意 2024-12-26 18:26:36

这是一篇很好的文章,详细介绍了 while(!eof) 的替代方案... 此处

Here is a nice article detailing alternatives to while(!eof)... here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文