Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 21-Apr-13 22:47pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^].
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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).
  Permalink  
Comments
Member 9357265 at 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
#include
#include
#include
#include
#include
#include
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 at 23-Apr-13 7:58am
   
Having trouble pasting my code... sorry
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

// Textreadwrite.cpp : main project file.
 
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
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);
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 329
2 Afzaal Ahmad Zeeshan 244
3 BillWoodruff 210
4 Maciej Los 192
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100