使用fgets(char* buf, sizeof(buf), FILE* stream) 以及 sscanf (const char *str,const char * format,........)。这里要了解format格式的正则表达式写法。
针对如何在C++读入以下复杂text 文本:
运行结果(保存在几个数组中用printf输出来的结果):
附上代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<mkl.h>
#include <string>
#include <stdlib.h>
#include <ctype.h>
#include <iostream>
using namespace std;
int main()
{
int n_body = 0;
int n_joint = 0;
int n_marker = 0;
int n_motion = 0;
int body_marker[100] = { 0 }; //n_body + 1;
double U_vec[100][6] = { 0.0 };
double Mass[100] = { 0.0 };
double Inertia[100][3] = { 0.0 };
double CM[100][6] = { 0.0 };
int Joint_Marker[100][4] = { 0 };
char Joint_Name[100][10];
char motion_Name[100][10];
double Motion_para[4] = { 0.0 };
double tspan[2] = { 0.0 };
//--------------------------------- READ TEXT ---------------------------------------//
FILE* fp;
char buf[256];
char blank[] = "\n";
char Ground_char[] = "Ground";
char PART_char[] = "PART";
char Joint_char[] = "Joint";
char Motion_char[] = "Motion";
char Time_char[] = "Time";
fp = fopen("multibody.txt", "r");
int n_temp_body_marker = 0;
while (fgets(buf, sizeof(buf), fp))
{
//printf("%s", buf);
if (buf[0] == blank[0] )
{
continue;
}
else if (buf[0]== Ground_char[0])
{
continue;
}
else if (buf[0] == PART_char[0])
{
body_marker[n_body] = n_temp_body_marker;
n_temp_body_marker = 0;
sscanf(buf, "%*5c%*d%*[ ]%*4c%*[ = ]%lf, %*2c%*[ = ]%lf, %lf, %lf, %*2c%*[ = ]%lf, %lf, %lf, %lf, %lf, %lf", &Mass[n_body],&Inertia[n_body][0], &Inertia[n_body][1], &Inertia[n_body][2],
&CM[n_body][0], &CM[n_body][1], &CM[n_body][2], &CM[n_body][3], &CM[n_body][4], &CM[n_body][5]);
printf("mass:%lf, I[0]:%lf, I[1]:%lf, I[2]:%lf, x:%lf, y:%lf, z:%lf, a1:%lf, a2:%lf, a3:%lf\n\n", Mass[n_body], Inertia[n_body][0], Inertia[n_body][1], Inertia[n_body][2],
CM[n_body][0], CM[n_body][1], CM[n_body][2], CM[n_body][3], CM[n_body][4], CM[n_body][5]); // mass, inertia, CM
n_body++;
}
else if (buf[0] == Joint_char[0])
{
body_marker[n_body] = n_temp_body_marker;
sscanf(buf, "%*6c%*d%*[ ]%*7c%*[ = ]%*[{ ]%d, %d%*[ }], %*7c%*[ = ]%*[{ ]%d, %d%*[ }], %s", &Joint_Marker[n_joint][0], &Joint_Marker[n_joint][1], &Joint_Marker[n_joint][2], &Joint_Marker[n_joint][3], &Joint_Name[n_joint]);
printf("markerI[0]:%d, markerI[1]:%d, markerJ[0]:%d, markerJ[1]:%d, Joint_Name:%s\n\n", Joint_Marker[n_joint][0], Joint_Marker[n_joint][1], Joint_Marker[n_joint][2], Joint_Marker[n_joint][3], Joint_Name[n_joint]);// Joint_Marker, Joint_Name
n_joint++;
}
else if (buf[2] == Motion_char[2])
{
sscanf(buf, "%*7c%*d%*[ ]%*5c%*[ = ]%*d%*[, ]%s%*[ = ]%*5c%lf, %lf, %lf, %lf%*[)]", &motion_Name[n_motion], &Motion_para[0], &Motion_para[1], &Motion_para[2], &Motion_para[3]);
printf("motion_Name:%s, Motion_para[0]:%lf, Motion_para[1]:%lf, Motion_para[2]:%lf, Motion_para[3]:%lf\n\n", motion_Name[n_motion], Motion_para[0], Motion_para[1], Motion_para[2], Motion_para[3]);// Motion_para, motion_Name
n_motion++;
}
else if (buf[0] == Time_char[0])
{
sscanf(buf, "%*4c%*[ = ]%lf, %lf", &tspan[0], &tspan[1]);
printf("tspan[0]:%lf,tspan[1]:%lf\n\n", tspan[0], tspan[1]);
}
else
{
sscanf(buf, "%*6c%*d%*[ = ]%lf, %lf, %lf, %lf, %lf, %lf", &U_vec[n_marker][0], &U_vec[n_marker][1], &U_vec[n_marker][2], &U_vec[n_marker][3], &U_vec[n_marker][4], &U_vec[n_marker][5]);
printf("x:%lf, y:%lf, z:%lf, a1:%lf, a2:%lf, a3:%lf\n\n", U_vec[n_marker][0], U_vec[n_marker][1], U_vec[n_marker][2], U_vec[n_marker][3], U_vec[n_marker][4], U_vec[n_marker][5]);
n_marker++;
n_temp_body_marker++;
}
}
fclose(fp);
return 0;
//Marker11
/*double pos1 = 0.0, pos2 = 0.0, pos3 = 0.0, ang1 = 0.0, ang2 = 0.0, ang3 = 0.0;
fscanf(fp, "%*6c%*d%*[ ]%*[=]%*[ ]%lf, %lf, %lf, %lf, %lf, %lf", &pos1, &pos2, &pos3, &ang1, &ang2, &ang3);
printf("x:%lf, y:%lf, z:%lf, a1:%lf, a2:%lf, a3:%lf\n", pos1, pos2, pos3, ang1, ang2,ang3);*/
// PART/1
/*double mass = 0.0, I1 = 0.0, I2 = 0.0, I3 = 0.0;
double pos1 = 0.0, pos2 = 0.0, pos3 = 0.0, ang1 = 0.0, ang2 = 0.0, ang3 = 0.0;
fscanf(fp, "%*4c%*[ ]%*[=]%*[ ]%lf, %*2c%*[ ]%*[=]%*[ ]%lf, %lf, %lf, %*2c%*[ ]%*[=]%*[ ]%lf, %lf, %lf, %lf, %lf, %lf", &mass, &I1, &I2, &I3, &pos1, &pos2, &pos3, &ang1, &ang2, &ang3);
printf("mass:%lf, I1:%lf, I2:%lf, I3:%lf, x:%lf, y:%lf, z:%lf, a1:%lf, a2:%lf, a3:%lf\n", mass, I1, I2, I3, pos1, pos2, pos3, ang1, ang2,ang3);
*/
// Joint/1
/*int markerI[2] = { 0 };
int markerJ[2] = { 0};
fscanf(fp, "%*7c%*[ ]%*[=]%*[ ]%*[{ ]%d, %d%*[ }], %*7c%*[ ]%*[=]%*[ ]%*[{ ]%d, %d%*[ }], %s", &markerI[0], &markerI[1], &markerJ[0], &markerJ[1], &Joint_Name[0]);
printf("markerI[0]:%d, markerI[1]:%d, markerJ[0]:%d, markerJ[1]:%d, Joint_Name:%s\n", markerI[0], markerI[1], markerJ[0], markerJ[1], Joint_Name[0]);
return 0;*/
/*/注意:什么位置的空格才可以被自动略过
double x = 0.0, y = 0.0, z = 0.0;
const char * text1 = "ENU : 37.69129, 110.97952 ,2583.369";
int num = sscanf(text1, "%*[A-Z]%*[ ]%*[:]%*[ ]%lf,%lf%*[ ],%lf", &x, &y, &z);
printf("x:%lf, y:%lf, z:%lf\n", x, y, z); //x:37.691290, y:110.979520, z:2583.369000
x = 0.0, y = 0.0, z = 0.0;
num = sscanf(text1, "%*6c%lf,%lf%*[ ],%lf", &x, &y, &z);
printf("x:%lf, y:%lf, z:%lf\n", x, y, z); //x:37.691290, y:110.979520, z:2583.369000
char * des = (char*)malloc(BUFSIZE);
char * des0 = (char*)malloc(BUFSIZE);
char buffer[BUFSIZE] = " 38.259 , 5896 DEVOTIOON";
char buf[BUFSIZE] = "38.259 , 5896 stay foolish";
//1.一般用法
sscanf(buffer, "%s", des);
printf("%s \n", des);
//2.取指定长度的字符串,取最大长度为4的字符串
memset(des, '\0', BUFSIZE);
sscanf(buffer, "%4s", des);
printf("%s \n", des); //38.2
//3.取到指定字符为止的字符串, 如取遇到空格为止的字符串
memset(des, '\0', BUFSIZE);
sscanf(buf, "%[^ ]", des);
printf("%s \n", des); //38.259
//4.取仅包含指定字符集的字符串: 取包含1-9和小写字母的字符串
memset(des, '\0', BUFSIZE);
sscanf(buffer, "%[1-9 ,.]", des); //"%[1-9 ,.]"含有空格
printf("%s \n", des); // 38.259 , 5896
//取到指定字符集为止的字符串。 取遇到大写字母为止的字符串
memset(des, '\0', BUFSIZE);
sscanf(buffer, "%[^A-Z]", des);
printf("%s \n", des); // 38.259 , 5896
//给定一个字符串itertor/54367#,获取 / 和 # 之间的字符串,
//%*[^/]过滤掉非'/'的字符,'/'字符存在,%[^#]读取非'#'的字符,遇到'#'停止,将读取的非"#"字符传到des
memset(des, '\0', BUFSIZE);
sscanf("itertor/54367#", "%*[^/]/%[^#]", des);
printf("%s\n", des); //54367
//scanf() 的两个小例子
//输入一个字符串,这个祖父穿只含有小写字符,遇到第一个不是小写字符时停止
char * str = (char*)malloc(BUFSIZE);
printf("Please input lowercase characters:");
scanf("%[a-z]", str);
printf("received lowercase characters%s \n", str);
//输入一个字符串,遇到字符"."停止。
memset(str, '\0', BUFSIZE);
printf("Please input lowercase string(quit with signal ';' ouccrring):");
scanf("%[^;]", str);
printf("received lowercase characters%s \n", str);
free(des);
free(des0);*/
}