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?
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.
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;
}
Declare and open file in one line
ifstream infile("C:/Users/Ryan/Desktop/data.txt");
Don't test for EOF as the loop conition.
Don't manually close the file (let the destructor do it)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With