在c ++中读取Dat文本文件并将值输出到新文本文件 [英] Read Dat text file in c++ and output values to new text file

查看:116
本文介绍了在c ++中读取Dat文本文件并将值输出到新文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



给我另一份工作要弄明白。



我有Dat文件包含250000行数据,由程序创建。我必须获取此信息,将其读入c ++,然后输出具有相同信息的另一个文本文件,但我需要重新排列列,我需要保留注释。这是另一个程序来读取信息。



该文件主要是以下类型的信息:



**** CAISSON JOINTS

NODE XYZ

196268 14.5000 5.5000 -72.0000

197268 14.5000 8.5000 -72.0000

198068 14.5000 11.5000 -72.0000



**** LEVEL -71

NODE XYZ

201010 - 33.4000 -25.0000 -71.0000

201030 -13.0000 -25.0000 -71.0000

201070 13.0000 -25.0000 -71.0000



我想我可以用Node,X,Y和Z变量创建一个结构,从中可以相对容易地输出到所需的任何格式。不确定从哪里开始所以任何帮助都会受到赞赏。



谢谢

解决方案

你可以开始从非常基本的层面逐步移动。



写一个样品给



1.打开一个文件。

2.阅读内容。

3.显示内容。



再写一个样本给



1.打开文件。

2.写一些内容。



将组合样本写入



1.打开文件

2.阅读内容

3打开另一个文件

4.写入从源文件中读取的内容。





如果你还想要准备吃饭,你可以看看这个 [ ^ ]。


引用:

我以为我可以创建一个带有Node,X,Y和Z变量的结构,从中可以相对容易地输出无论采用何种格式。不确定从哪里开始所以任何帮助将不胜感激。



您的方法看起来是正确的。可能你需要在 struct 中添加 NODE 字段。



您可以逐行读取文本文件,并解析读取行以填充结构的一个实例(在您的情况下解析非常简单,只需拆分字符串,看看,例如在 string :: find [ ^ ]方法)。


 // Textreadwrite.cpp:main项目文件。< br /> 
< br />
#includestdafx.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 />
字符串行;< br />
ifstream in_stream(FileName);< br />
ofstream out_stream;< br />
< br />
//确定文件中的行数和列数< br />
//如果行长度不同,程序将停止*********** *******不工作*******************< br />
while(getline(in_stream,line,''' \ n'')){< br />
字符串段;< br />
int ColsPerRow = 0; //初始化计数器< br />
stringstream ss;< br />
ss<< line;< br />
while(getline(ss,segments,''\ t'')){< br />
ColsPerRow ++;< br />
} < br />
if(RowNum == 0){< br />
//将文件中的列数定义为< br />
//列中的列数第一行。< br />
ColNum = ColsPerRow;< br />
}否则{< br />
if(ColsPerRow!= ColNum){< br / >
cerr<< 行&l​​t;< RowNum<< 长度不一样< br />
为第0行。 << endl;< br />
退出(0);< br />
}< br />
}< br />
RowNum ++;< br />
}< br />
//声明数组,用于存储和访问文件中的< br />
//数据。< br />
float * * pa2d = new float * [RowNum];< br />
float * parr = new float [RowNum * ColNum];< br />
//重新定位以启动流缓冲区。< ; br />
in_stream.clear(); < br />
in_stream.seekg(0,ios :: beg);< br />
//将数据写入数组:< br />
for(int i = 0; i<(RowNum * ColNum); i ++){< br />
//声明。< br />
float in_float;< br />
char in_char;< br />
string in_s;< br />
stringstream in_ss;< br />
//将数据条目插入stringstream。< br />
in_stream>> in_s;< br />
in_ss<< in_s;< br />
//将数据条目转换为float。< br />
in_ss>> in_float;< br />
}< br />
//准备要返回的2D数组。 < br />
//在每行的开头定义指针位置。< br />
for(int i = 0; i< RowNum; i ++){< br />
pa2d [i] = parr +(i * ColNum);< br />
}< br />
返回pa2d;< br />
}< br />
void PrintMatrix(float ** matrix,const int RowNum,const int ColNum){< br />
//将矩阵打印到控制台。< 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 />
}< br />
cout<< ''\ n'';< br />
}< br />
}< br />
void OutPut(float ** matrix,const int RowNum,const int ColNum){< br />
//将矩阵打印到文本文件< br />
ofstream out_stream;< br />
out_stream.open(outfile.dat) ; //打开(连接)输出文件流< br />
< br />
if(out_stream.fail())< br />
{< br />
cout<< 输出文件打开失败。\ 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 />
}< br />
out_stream<< ''\ n'';< br />
}< br />
}< br />
int main(){< br />
//定义输入文件名。< br />
const char * FileName =test3.txt;< br />
//初始化行和列计数器。< br />
int RowNum = 0;< br />
int ColNum = 0;< br />
//执行将数据文件读入2D数组。< 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>


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

解决方案

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[^].


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).


// 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>


这篇关于在c ++中读取Dat文本文件并将值输出到新文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆