【发布时间】:2015-02-16 04:06:14
【问题描述】:
我正在尝试使用getline 函数从tst.txt 读取一行的程序。但是,当我尝试从中获取子字符串时,我收到一条错误消息,上面写着method substr could not be resolved。
我的代码如下。
#include <iostream>
#include <fstream>
#include <string>
#include <istream>
#include <sstream>
#include <stdlib.h>
using namespace std;
int main () {
string line;
string substr(int a, int b);
string name[15];
int a[15];
int b[15];
ifstream myfile ("tst.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
int i;
getline (myfile,line);
a[i]= myfile.substr(4,2);
name[i]= myfile.substr(18,15);
b[i]= myfile.substr(36,1);
i=i+1;
cout << a[i] <<" "<< b[i] << " "<< name[i] << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
【问题讨论】:
-
“请尽快帮助”只是乞求投反对票..
-
作业什么时候交?
-
myfile是文件流,不是字符串,所以它没有子字符串。你的意思是line.substr()? -
还有,这里的
string substr(int a, int b);是什么? -
+
i没有初始化,不是说在while里面,就是说i=i+1没有意义……还有其他一些错误
标签: c++ file input substring substr