如何阅读下面这样格式化的文件,分别用标签分隔成结构? 这是我到目前为止的代码,我无法让它工作。我可以得到整行,但那我将如何分离出数据?
New England Patriots 6 2 .750 3-2 3-0 1-0 1 Buffalo Bills 5 2 .714 4-0 1-2 1-0 1
...
@ComponentScan(basePackages = { "de.rest", "de.security", "de.targetPackage" })
public class RestConfiguration extends WebMvcConfigurerAdapter {
...
@Component
public class targetClass {
private static String absoluteServletContextPath;
@Autowired
ServletContext context;
@Bean
public ServletContext getServletContext() {
absoluteServletContextPath = context.getRealPath("/");
System.out.println(absoluteServletContextPath);
return context;
}
@Override
public void myMethod {
absoluteServletContextPath = absoluteServletContextPath.replaceAll("webapp\\\\", "")
.replaceAll("\\\\", "/");}}
答案 0 :(得分:3)
使用std::getline()
和\n
(默认值)作为分隔符来读取整行,然后使用std::istringstream
来解析每行的值,使用std::getline()
来读取由空格以外的其他内容分隔的字符串数据。
例如:
ifstream inFile(filename.c_str());
if (!inFile) {
cerr << "Unable to open file" << filename;
exit(1); // call system to stop
}
string line;
while (getline(inFile, line))
{
istringstream iss(line);
getline(iss, teams[a].name, '\t');
...
iss >> teams[a].wins;
/* or:
string temp;
getline(iss, temp, '\t');
istringstream(temp) >> teams[a].wins;
*/
... and so on ...
}
答案 1 :(得分:1)
在getline
来电中添加分隔符。 getline(inFile, line, '\t');
应该有效。其中'\t'
是制表符。注意:这仅在元素实际上由制表符分隔