C + + writes data to. txt file reading data from. txt file string split __c++

Source: Internet
Author: User

Write data to a. txt file

Writes data to a. txt file
	vector<point2f> quad_pts;
	Quad_pts.push_back (point2f ( -602.935, 464.887));
	Quad_pts.push_back (point2f ( -401.548, 477.997));
	Quad_pts.push_back (point2f ( -1346.625, 261.575));
	Quad_pts.push_back (point2f ( -129.836, 283.220));

	Ofstream F1 ("Points.txt");//#include <fstream>
	char savepoints[100];//character array
	strcpy_s (savepoints, " Quad_pts ");
	F1 << savepoints << Endl;
	for (int i = 0; i < quad_pts.size (); i++)
	{
		sprintf_s (savepoints, "px%fy%f", quad_pts[i].x, QUAD_PTS[I].Y); C12/>f1 << savepoints << Endl;
	}

To read the. txt file data into C + +

Read data from. txt
	ifstream myFile;
	Myfile.open ("Points.txt");
	Char output[100];
	if (Myfile.is_open ()) 
	{while
		(!myfile.eof ()) 
		{
			myFile >> output;
			cout << output << Endl;
		}
	Myfile.close ();

Gets the part string between the two tag strings

std::string cf_getmiddlestr (std::string srcstr, std::string startmarkstr, std::string EndMarkStr)
{
	size_t Startpos, Endpos, length;
	startpos = Srcstr.find (startmarkstr, 0);
	Endpos = Srcstr.find (endmarkstr, 0);
	Length = Endpos-startpos-startmarkstr.size ();
	std::string resstr = srcstr.substr (startpos + startmarkstr.size (), length);//The second parameter represents the length return
	resstr;
}

The part of the string that precedes the tag string

std::string cf_getstartstr (std::string srcstr, std::string endmarkstr)
{size_t endpos  ;
	Endpos = Srcstr.find (endmarkstr, 0);
	std::string resstr = srcstr.substr (0, endpos);
	return resstr;
}
The part of the string after the token string

std::string cf_getendstr (std::string srcstr, std::string startmarkstr)
{size_t startpos
	, length;
	startpos = Srcstr.find (startmarkstr, 0);
	Length = Srcstr.size ()-startpos-startmarkstr.size ();
	std::string resstr = srcstr.substr (startpos + startmarkstr.size (), length);
	return resstr;
}






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.