Basic C ++ I / O Error

All I want is C ++ - a program that will read in a txt file, put each line in an array, and then print a duplicate copy to another txt file. Here is 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, while doing this, every other line gives me a bunch of funky Chinese characters. Here is 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 

But the output is 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?

+4
source share
4 answers

To solve your problem, it looks like you are outputting some unformatted characters (this is Chinese). I don’t see how you insert new lines (nevertheless, it seems that new lines appear on the output), so there is something missing in the code that you do not show us. Please CUT / PASTE REAL code .

  • What is real code.
  • How do you create output from files (was it a cat, did you open it in an editor)?

The main thing to note:

Never do this:

  while(!infile.eof()) 

You must read and check the string before using it.
This can be done in one line by putting the read in the condition:

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

Other things to look out for:

  • Format your code beautifully !!!!!!
  • Do not use all cap identifiers (they are usually reserved for macros).
  • Do not use arrays. Use std :: vector instead.
  • Declare and open the file in one line

     ifstream infile("C:/Users/Ryan/Desktop/data.txt"); 
  • Do not check the EOF as a loop loop.

    • This can cause an endless loop if there is another problem.
    • If you do not check what you read, then repeat the processing of the last line.
  • Do not close the file manually (let the destructor do this)

+4
source

If I am not mistaken, your OS is Windows?

The reason for these "Chinese" characters is that your .txt file is Unicode encoded. Open it in Notepad, click "Save As" and select "ANSI" from the "Encoding" drop-down list (somewhere at the bottom of the dialog box), and then save. This should fix the "Chinese" problem :)

Check other answers to fix code issues. Hope this helps.

+3
source

when you write data in which you do not include a new line so that you do not create a duplicate file, if that was your intention.

it makes no sense to have an array List [], since you still write it directly, instead you can do

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

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

+1
source

Here is a good article detailing the while (! Eof) alternatives ... here

+1
source

Source: https://habr.com/ru/post/1384891/


All Articles