[英]In C++, how to extract a substring from a string (or char array) and write it to a string, without getting warnings?
In C++, I'm reading a file in which the lines are something like 在C ++中,我正在读取一个文件,其中的行是类似的
65-82 0.015 0.655
where the first is a string (tipically made of numbers separated by a dash or a comma, that then gets processed) and two floating point numbers. 其中第一个是字符串(由短划线或逗号分隔的数字,然后被处理)和两个浮点数。 I'm currently reading them with the following code, with [line] being a character array: 我正在使用以下代码阅读它们,[line]是一个字符数组:
std::string temp_string;
double temp_kb, temp_kt;
int res = sscanf(line, "%s %lf %lf",temp_string,&temp_kb,&temp_kt);
this yields a warning: 这会产生一个警告:
~/TEPInteraction.cpp:1002:76: warning: writing into constant object (argument 3) [-Wformat=]
int res = sscanf(line, "%s %lf %lf",temp_string.c_str(),&temp_kb,&temp_kt);
^
which of course makes sense, since c_str() returns a constant pointer. 这当然有意义,因为c_str()返回一个常量指针。 What would be the proper way of doing this without a warning? 如果没有警告,这样做的正确方法是什么?
Many thanks, Ferdinando 非常感谢,费迪南多
EDIT: The value of res was needed to do some controls further down the line, so I'll have to briefly rewrite my code so that it doesn't use it. 编辑:需要res的值来进行更深入的控制,所以我将不得不简单地重写我的代码,以便它不使用它。 It's not a big deal but It'll wait until tomorrow. 这不是什么大不了的事,但它会等到明天。 Thanks guys :-) I really appreciate your help. 谢谢你们:-)非常感谢你的帮助。
Since your input is space delimited you can get away with reading the input with the >>
operator of the file stream. 由于您的输入是以空格分隔的,因此您可以使用文件流的>>
运算符读取输入。
std::ifstream fin("my_file_to_read");
std::string data;
double a, b;
fin >> data >> a >> b;
The above will take 以上将采取
65-82 0.015 0.655
from the file and input 65-82
into data
and then stop at the space. 从文件中输入65-82
到data
然后停在空间。 Then 0.015
gets added to a
and then stops at the space and then 0.655
gets added to b
. 然后将0.015
添加到a
然后在空格处停止然后将0.655
添加到b
。
Since your input starts with a number if you want to make sure that you have 3 values per line then you would need to read in the whole line from the file using std::getline
. 如果您想确保每行有3个值,那么您的输入以数字开头,那么您需要使用std::getline
从文件中读取整行。 Then you can load that line into a std::stringstream
and check if the extraction succedes. 然后你可以将该行加载到std::stringstream
并检查提取是否成功。
std::ifstream fin("my_file_to_read");
std::string data, line;
double a, b;
std::getline(fin, line);
std::stringstream ss(line);
if (!(ss >> data >> a >> b))
{
//error handling code here
}
In C++ you would typically do something like: 在C ++中,您通常会执行以下操作:
std::istringstream stream(line);
std::string temp_string;
double temp_kb, temp_kt;
if (!(stream >> temp_string >> temp_kb >> temp_kt)) {
// unable to read all values. handle error here
}
You cannot write to a string object like that. 你不能写这样的字符串对象。 If you're really bent on using sscanf()
, you'll need to use a char[]
array instead and potentially convert that to a string
afterward. 如果您真的想使用sscanf()
,则需要使用char[]
数组,然后将其转换为string
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.