C++ text txt data read and write

The accumulation of basic algorithms is not a problem, just to paste and copy quickly in the future, and also to improve efficiency.

This is for reading txt text data, and can be used after a little modification. A large copy of the article in the world, it depends on what effect you can surpass, no thanks to the original source of the code, if the original author sees it. Please let go of your mind, even if you help everyone, provide convenience!

void readInToMatrix(fstream &in, string FilePath, double data[ROW][COLUMN]) {
	in.open(FilePath, ios::in);//打开一个file
	if (!in.is_open()) {
		cout << "Can not find " << FilePath << endl;
		system("pause");
	}
	string buff;
	int i = 0;//行数i
	while (getline(in, buff)) {
		vector<double> nums;
		// string->char *
		char *s_input = (char *)buff.c_str();
		const char * split = ",";
		// 以‘,’为分隔符拆分字符串
		char *p = strtok(s_input, split);
		double a;
		while (p != NULL) {
			// char * -> int
			a = atof(p);
			//cout << a << endl;
			nums.push_back(a);
			p = strtok(NULL, split);
		}//end while
		for (int b = 0; b < nums.size(); b++) {
			data[i][b] = nums[b];
			//std::cout<<nums[b]<<" , ";
		}//end for
		//std::cout<<endl;
		i++;
	}//end while
	in.close();
	cout << "get  data over" << endl;
}

What is the shape without main()?

#include <iostream>  
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <opencv2/ml/ml.hpp>  

#include <cstdlib>
#include <ctime>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <fstream>

#include <vector>
#include <cassert>
#include <string>
#include <sstream>

using namespace cv;  
using namespace std;  
  #ifdef _DEBUG
#pragma comment(lib,"opencv_core2413d.lib")
#pragma comment(lib,"opencv_highgui2413d.lib")
#pragma comment(lib,"opencv_imgproc2413d.lib")
#pragma comment(lib,"opencv_features2d2413d.lib")
#pragma comment(lib,"opencv_calib3d2413d.lib")
#pragma comment(lib,"opencv_legacy2413d.lib")

#pragma comment(lib,"opencv_stitching2413d.lib")
#pragma comment(lib,"opencv_photo2413d.lib")
#pragma comment(lib,"opencv_contrib2413d.lib")
#pragma comment(lib,"opencv_flann2413d.lib")
#pragma comment(lib,"opencv_gpu2413d.lib")
#pragma comment(lib,"opencv_ml2413d.lib")


#pragma comment(lib,"opencv_nonfree2413d.lib")
#pragma comment(lib,"opencv_objdetect2413d.lib")
#pragma comment(lib,"opencv_ocl2413d.lib")
#pragma comment(lib,"opencv_superres2413d.lib")
#pragma comment(lib,"opencv_ts2413d.lib")
#pragma comment(lib,"opencv_video2413d.lib")

#pragma comment(lib,"opencv_videostab2413d.lib")
#else
#pragma comment(lib,"opencv_core2413.lib")
#pragma comment(lib,"opencv_highgui2413.lib")
#pragma comment(lib,"opencv_imgproc2413.lib")
#pragma comment(lib,"opencv_features2d2413.lib")
#pragma comment(lib,"opencv_calib3d2413.lib")
#pragma comment(lib,"opencv_legacy2413.lib")

#pragma comment(lib,"opencv_stitching2413.lib")
#pragma comment(lib,"opencv_photo2413.lib")
#pragma comment(lib,"opencv_contrib2413.lib")
#pragma comment(lib,"opencv_flann2413.lib")
#pragma comment(lib,"opencv_gpu2413.lib")
#pragma comment(lib,"opencv_ml2413.lib")


#pragma comment(lib,"opencv_nonfree2413.lib")
#pragma comment(lib,"opencv_objdetect2413.lib")
#pragma comment(lib,"opencv_ocl2413.lib")
#pragma comment(lib,"opencv_superres2413.lib")
#pragma comment(lib,"opencv_ts2413.lib")
#pragma comment(lib,"opencv_video2413.lib")

#pragma comment(lib,"opencv_videostab2413.lib")

#endif //_debug
#define ROW   1152
#define COLUMN  8

string mtrain_txt="alldl-save.txt";
ofstream myfileall(mtrain_txt, ios::app);

int main()
{
	double data[ROW][COLUMN];
	fstream  mfin;
	string txtfilepath="dataXYZ-0829.txt";
	readInToMatrix(mfin,txtfilepath,data);
 //显示定位效果
	 Mat picture(500,500,CV_8UC3,Scalar(255,255,255));  
	Point drawcenter = Point(250,250);  
	Point drawcenterSec = Point(250,250);  

	bool iscaulate=false;

	while(true)
	{
	
	//wait for 40 milliseconds
		int c = cvWaitKey(40);
		//exit the loop if user press "Esc" key  (ASCII value of "Esc" is 27) 
		if (27 == char(c)) break;
		
	int drawr = 2;  
		//承载图像  
		
	int color=255;
		 line(picture, Point(0,150), Point(500, 150), Scalar(0, 255, 0), 1, 8);
		 line(picture, Point(0,200), Point(500, 200), Scalar(0, 255, 0), 1, 8);
		 line(picture, Point(0,250), Point(500, 250), Scalar(0, 255, 0), 1, 8);
		 line(picture, Point(0,300), Point(500, 300), Scalar(0, 255, 0), 1, 8);
		 line(picture, Point(0,350), Point(500, 350), Scalar(0, 255, 0), 1, 8);


		line(picture, Point(150, 0), Point(150, 500), Scalar(255,0 , 0), 1, 8);
		line(picture, Point(200, 0), Point(200, 500), Scalar(255,0 , 0), 1, 8);
		line(picture, Point(250, 0), Point(250, 500), Scalar(255,0 , 0), 1, 8);
		line(picture, Point(300, 0), Point(300, 500), Scalar(255,0 , 0), 1, 8);
		line(picture, Point(350, 0), Point(350, 500), Scalar(255,0 , 0), 1, 8);

		
		if(!iscaulate)
		{
	for(int i=0;i<ROW;i++)
	{
		


	myfileall << data[i][0]<<","<<data[i][1]<<","<<data[i][2]<<","<<data[i][3]<<data[i][4]<<","<<data[i][5]<<","<<data[i][6]<<","<<data[i][7]<<"\t\n" ;

	drawcenter.x=250-(data[i][0])/5;
		
	drawcenter.y=250+data[i][1]/5;
	
	drawcenterSec.x=250-data[i][4]/5;
		
	drawcenterSec.y=250+data[i][5]/5;

		//参数为:承载的图像、圆心、半径、颜色、粗细、线型  
	circle(picture,drawcenterSec,drawr,Scalar(0,0,color));  

		//参数为:承载的图像、圆心、半径、颜色、粗细、线型  
	circle(picture,drawcenter,drawr,Scalar(0,color,0)); 
	
	}
			iscaulate=true;
		}
	
		imshow("Location_Show",picture);  

	}

	 myfileall.close();
	
	return 0;
}

It’s not well written, so please refer to it!

 

Guess you like

Origin blog.csdn.net/sun19890716/article/details/100580519