<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 />
while(getline(in_stream,line,'\n')) {<br />
string segments;<br />
int ColsPerRow = 0;
stringstream ss;<br />
ss << line;<br />
while (getline(ss,segments,'\t')) {<br />
ColsPerRow++;<br />
}<br />
if (RowNum == 0) {<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 />
float** pa2d = new float*[RowNum];<br />
float* parr = new float[RowNum*ColNum];<br />
in_stream.clear(); <br />
in_stream.seekg(0, ios::beg);<br />
for (int i = 0; i < (RowNum*ColNum); i++) {<br />
float in_float;<br />
char in_char;<br />
string in_s;<br />
stringstream in_ss;<br />
in_stream >> in_s;<br />
in_ss << in_s;<br />
in_ss >> in_float;<br />
}<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 />
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 />
}<br />
cout << '\n';<br />
}<br />
}<br />
void OutPut(float** matrix, const int RowNum, const int ColNum) {<br />
ofstream out_stream;<br />
out_stream.open("outfile.dat");
<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 />
}<br />
out_stream << '\n';<br />
}<br />
}<br />
int main() {<br />
const char* FileName = "test3.txt";<br />
int RowNum = 0;<br />
int ColNum = 0;<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>