Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

Another job given to me to figure out.

I have Dat file with 250000 lines of data on it which was created by a program. I have to take this information, read it into c++ and then output another text file with the same information, but I need to re-arrange the columns and I need to retain the comments. This is for another program to read the information.

The file is mainly the following type of information:

**** CAISSON JOINTS
NODE X Y Z
196268 14.5000 5.5000 -72.0000
197268 14.5000 8.5000 -72.0000
198068 14.5000 11.5000 -72.0000

**** LEVEL -71
NODE X Y Z
201010 -33.4000 -25.0000 -71.0000
201030 -13.0000 -25.0000 -71.0000
201070 13.0000 -25.0000 -71.0000

I was thinking that I could create a struct with a Node, X, Y and Z variable and from that it should be relatively easy to output to whatever format is required. Not really sure where to start so any help would be appreciated.

Thanks
Posted

You can start from very much basic level and move step by step.

Write a sample to

1. Open a file.
2. Read content.
3. Display content.

Write another sample to

1. Open a file.
2. Write some content to it.

Write a combined sample to

1. Open a file
2. Read content
3. Open another file
4. Write content read from source file.


If you still want to have ready to eat, you can have a look at this[^].
 
Share this answer
 
Quote:
I was thinking that I could create a struct with a Node, X, Y and Z variable and from that it should be relatively easy to output to whatever format is required. Not really sure where to start so any help would be appreciated.

Your approach looks correct. Possibly you need to add the NODE field in your struct.

You may read the text file line by line, and parse the read line to fill one instance of your struct (parsing is really simple in your case, just split the string, have a look, for instance at string::find[^] method).
 
Share this answer
 
Comments
Member 9357265 23-Apr-13 7:54am    
Okay as a start I've researched the forums, etc and have managed to cobble code for just getting to read the input file and output exactly what is in the input file.

I've removed all the "comments" and column headings to start with and just left the numbers. I have two problems:
1. The code goes through every row, but stops at column 1. It doesn't seem to recognise the tab or space as a delimiter
2. It is printing garbage, nothing like the numbers above.

Code is as follows:


// Textreadwrite.cpp : main project file.

#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstdlib>
using namespace std;

float** ReadTable(const char* FileName, int& RowNum, int& ColNum) {
string line;
ifstream in_stream(FileName);
ofstream out_stream;

// Determine number of rows and columns in file
// Program halts if rows are not of same length******************Doesn't Work*******************
while(getline(in_stream,line,'\n')) {
string segments;
int ColsPerRow = 0; // Initialize counter
stringstream ss;
ss << line;
while (getline(ss,segments,'\t')) {
ColsPerRow++;
}
if (RowNum == 0) {
// Define number of columns in file as the number
// of columns in the first row.
ColNum = ColsPerRow;
} else {
if (ColsPerRow != ColNum) {
cerr << "Row " << RowNum << " is not the same length "
"as row 0." << endl;
exit(0);
}
}
RowNum++;
}
// Declare arrays for storing and accessing
// data from the file.
float** pa2d = new float*[RowNum];
float* parr = new float[RowNum*ColNum];
// Reposition to start of stream buffer.
in_stream.clear();
in_stream.seekg(0, ios::beg);
// Write data to array:
for (int i = 0; i < (RowNum*ColNum); i++) {
// Declarations.
float in_float;
char in_char;
string in_s;
stringstream in_ss;
// Insert data entry into stringstream.
in_stream >> in_s;
in_ss << in_s;
// Convert data entry to float.
in_ss >> in_float;
}
// Prepare 2D array to be returned.
// Define pointer position at start of each row.
for (int i = 0; i < RowNum; i++) {
pa2d[i] = parr + (i*ColNum);
}
return pa2d;
}
void PrintMatrix(float** matrix, const int RowNum, const int ColNum) {
// Print "matrix" to console.
cout << '\n';
for (int i=0; i < RowNum; i++) {
for(int j=0; j < ColNum; j++) {
cout << (j?"\t":"") << matrix[i][j];
// Don't insert tab before first element of each row.
}
cout << '\n';
}
}
void OutPut(float** matrix, const int RowNum, const int ColNum) {
//Print matrix to text file
ofstream out_stream;
out_stream.open("outfile.dat"); // open (connect) output file stream

if (out_stream.fail())
{
cout << "Output file opening failed.\n";
cout << '\n';
}
for (int i=0; i < RowNum; i++) {
for(int j=0; j < ColNum; j++) {

out_stream << (j?"\t":"") << matrix[i][j];
// Don't insert tab before first element of each row.
}
out_stream << '\n';
}
}
int main() {
// Define input filename.
const char* FileName = "test3.txt";
// Initialize row and column counters.
int RowNum = 0;
int ColNum = 0;
// Perform reading of data file into 2D array.
float** data = ReadTable(FileName,RowNum,ColNum);

PrintMatrix(data,RowNum,ColNum);
OutPut(data,RowNum,ColNum);

system ("pause");
return(0);
}
Member 9357265 23-Apr-13 7:58am    
Having trouble pasting my code... sorry
// Textreadwrite.cpp : main project file.<br />
<br />
#include "stdafx.h"<br />
#include <string><br />
#include <vector><br />
#include <iostream><br />
#include <fstream><br />
#include <sstream><br />
#include <cmath><br />
#include <cstdlib><br />
using namespace std;<br />
<br />
float** ReadTable(const char* FileName, int& RowNum, int& ColNum) {<br />
	string line;<br />
	ifstream in_stream(FileName);<br />
	ofstream out_stream;<br />
<br />
	// Determine number of rows and columns in file<br />
	// Program halts if rows are not of same length******************Doesn't Work*******************<br />
		while(getline(in_stream,line,'\n')) {<br />
		string segments;<br />
		int ColsPerRow = 0; // Initialize counter<br />
		stringstream ss;<br />
		ss << line;<br />
		while (getline(ss,segments,'\t')) {<br />
			ColsPerRow++;<br />
		}<br />
		if (RowNum == 0) {<br />
			// Define number of columns in file as the number<br />
			// of columns in the first row.<br />
			ColNum = ColsPerRow;<br />
		} else {<br />
			if (ColsPerRow != ColNum) {<br />
				cerr << "Row " << RowNum << " is not the same length "<br />
					"as row 0." << endl;<br />
				exit(0);<br />
			}<br />
		}<br />
		RowNum++;<br />
	}<br />
	// Declare arrays for storing and accessing<br />
	// data from the file.<br />
	float** pa2d = new float*[RowNum];<br />
	float*  parr = new float[RowNum*ColNum];<br />
	// Reposition to start of stream buffer.<br />
	in_stream.clear();              <br />
	in_stream.seekg(0, ios::beg);<br />
	// Write data to array:<br />
	for (int i = 0; i < (RowNum*ColNum); i++) {<br />
		// Declarations.<br />
		float in_float;<br />
		char in_char;<br />
		string in_s;<br />
		stringstream in_ss;<br />
		// Insert data entry into stringstream.<br />
		in_stream >> in_s;<br />
		in_ss << in_s;<br />
		// Convert data entry to float.<br />
		in_ss >> in_float;<br />
	}<br />
	// Prepare 2D array to be returned. 	<br />
	// Define pointer position at start of each row.<br />
	for (int i = 0; i < RowNum; i++) {<br />
		pa2d[i] = parr + (i*ColNum);<br />
	}<br />
	return pa2d;<br />
}<br />
void PrintMatrix(float** matrix, const int RowNum, const int ColNum) {<br />
	// Print "matrix" to console.<br />
	cout << '\n';<br />
	for (int i=0; i < RowNum; i++) {<br />
		for(int j=0; j < ColNum; j++) {<br />
			cout << (j?"\t":"") << matrix[i][j];<br />
			// Don't insert tab before first element of each row.<br />
		}<br />
		cout << '\n';<br />
	}<br />
}<br />
void OutPut(float** matrix, const int RowNum, const int ColNum) {<br />
	//Print matrix to text file<br />
	ofstream out_stream;<br />
	out_stream.open("outfile.dat"); // open (connect) output file stream<br />
<br />
	if (out_stream.fail())<br />
	{<br />
		cout << "Output file opening failed.\n";<br />
		cout << '\n';<br />
	}<br />
	for (int i=0; i < RowNum; i++) {<br />
		for(int j=0; j < ColNum; j++) {<br />
<br />
			out_stream << (j?"\t":"") << matrix[i][j];<br />
			// Don't insert tab before first element of each row.<br />
		}<br />
		out_stream << '\n';<br />
	}<br />
}<br />
int main() {<br />
	// Define input filename.<br />
	const char* FileName = "test3.txt";<br />
	// Initialize row and column counters.<br />
	int RowNum = 0;<br />
	int ColNum = 0;<br />
	// Perform reading of data file into 2D array.<br />
	float** data = ReadTable(FileName,RowNum,ColNum);<br />
<br />
	PrintMatrix(data,RowNum,ColNum);<br />
	OutPut(data,RowNum,ColNum);<br />
<br />
	system ("pause");<br />
	return(0);<br />
}</cstdlib></cmath></sstream></fstream></iostream></vector></string>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900