比如我的文件中有如下字符串:
w_ang(3)
我该怎么编写代码将3读出来???
求大神指点,,,万分感谢!!!!急急急~~~~
C++中利用ifstream来读文件,例如:ifstream in("data2.txt"); string m_zStr; m_zStr
如何把一个文本文件一次行读到一个string对象中,方法有两种:
std::ifstream in("some.file");
std::istreambuf_iterator beg(in), end;
std::string st......
答案就在这里:c++从文件读取数据到字符串
----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。
#include <string.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
string s = "w_ang(1)";
int x = s.find("(");
int y = s.find(")");
string r = s.substr(x + 1, y - x - 1);
cout << r.c_str() << endl;
return 0;
}
先将整个字符串从文件中读出,然后就是基本的字符串操作:查找,和取子字符串的工作了。
使用函数sscanf可以的
你是用Java还是C++啊。用Java的话先把字符串读出来,然后用pattern matches等正则取出来。
定义数字标记为false,
一个一个字符读入,
数字标记为false且读入字符为数字时,数字标记改为true,将数字输出;
数字标记为true且读入字符为数字时,将数字输出;
数字标记为true且读入字符不为数字时,数字标记改为false,输出回车符;
读取下一个字符,直到字符串结束。
历遍字符串,如果遇到‘(’就把后一个字符取出来,并减去‘0’得到int型数值。