天天看點

C++讀取檔案并按逗号拆分字元串

C++讀取檔案并按逗号拆分字元串

#include <iostream>
#include <fstream>

//讀取txt檔案中的路徑資料
bool getPathData(char *pathname) {
	std::ifstream openfile;
	//打開檔案
	openfile.open(pathname);
	char line[80];
	int i = 0;
	float path_x[301], path_y[301];
	//擷取每一行資料
	while (openfile.getline(line, 80))
	{
		char *p, *p1, *p2;
		char *buf;
		//用逗号分離x-y的坐标
		p = strtok_s(line, ",", &buf);
		p1 = p;
		p = strtok_s(NULL, ",", &buf);
		p2 = p;
		//char*轉換為float
		float f1 = atof(p1);
		float f2 = atof(p2);
		//将float類型的軌迹資料存入數組
		path_x[i] = f1;
		path_y[i] = f2;
		i++;
	}
	openfile.close();
	return true;
}

int main()
{
	char pathname[] = "E:\\python\\python\\2021\\202105\\2105131\\path_data.txt";
	getPathData(pathname);
}
           
c++

繼續閱讀