Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic Input/Output C++ Error

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?

like image 429
user1083385 Avatar asked Jan 18 '23 03:01

user1083385


2 Answers

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)

like image 151
Martin York Avatar answered Jan 21 '23 17:01

Martin York


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.

like image 43
jrok Avatar answered Jan 21 '23 15:01

jrok