Hello!

I am trying to read some data from an existing .txt file, actually that data consists of three columns, and dozens of raws. I can read it and write it to another new .txt file, but the problem is, it generates only one column with all those data, which is my headache now.
The data in original .txt is something like:
1 1 1
2 2 2
...
10 10 10

How can I read these and create another new .txt with exactly the same columns and raws??
Could any one help me??
Thank you in advance!

Best,
Abayiz

it's only generating 1 column because you are using get() you need to use getline(). get() and cin are both " " delimiter.

Hello!
First, thank you all for your interest!
My code is as follows:

#include <iostream>
#include <iomanip>
#include <fstream>


using namespace std;

int main ()
{
	ifstream inFile;
	ofstream outFile;
	char inputFilename []="test1.txt";
	char outputFilename []="test1_output.txt";
	inFile.open(inputFilename, ios::in);
	
	if (!inFile) {
	cerr<<"Can't open input file "<<inputFilename <<endl;
//	exit(1);
	}

	outFile.open (outputFilename, ios::out);

	if (!outFile) {
	cerr<<"Can't open output file " <<outputFilename <<endl;
//	exit(1);
	}

	//char username [9]; //One extra for null char
	int d=5;

	while (!inFile.eof()) {
		inFile >> d;
		outFile <<d <<" " <<endl;
	}

	inFile.close ();
	outFile.close();


	return 0;
}

In it, "test1.txt" contains this:
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10

And, the output is like this:
1
1
1
2
2
2
3
3
3
4
4
4
5
5
5
6
6
6
7
7
7
8
8
8
9
9
9
10
10
10
10

Actually, what I really wanna do is to copy all the columns and rows of an existing data file, and use it to create a vtkPolyData. So, I write this code and simple input text as an exercise for my final goal.
I was thinking, maybe I should use "inFile.getline"??

Thank you again!
Abayiz

Edited 6 Years Ago by WaltP: Added CODE Tags - please use them

What part of my comment didn't make sense? >> and get() are "space" delimited. Meaning it finds a space and stops reading, you need to use getline(inFile,line);
Please enclose your code in (CODE) for the future

Edited 3 Years Ago by mike_2000_17: Fixed formatting

If you use endl, you will always get one line for each output. Use endl only after outputting 3 values.

This article has been dead for over six months. Start a new discussion instead.