C++ 如何在文本文件中逐行读取字符串?
这段代码将只读取和计算input.txt文件中的第一个输入,而忽略输入文件中的其余输入。我一直在尝试解决它,以便它可以读取所有其余输入并计算它们。 这是我的代码,我觉得有点问题 我试过几种循环方法C++ 如何在文本文件中逐行读取字符串?,c++,C++,这段代码将只读取和计算input.txt文件中的第一个输入,而忽略输入文件中的其余输入。我一直在尝试解决它,以便它可以读取所有其余输入并计算它们。 这是我的代码,我觉得有点问题 我试过几种循环方法 int main() { string inputLine; ifstream file ("input.txt");// input file to be read ofstream file1; file1.open("output.txt"); freopen("out
int main()
{
string inputLine;
ifstream file ("input.txt");// input file to be read
ofstream file1;
file1.open("output.txt");
freopen("output.txt", "w", stdout);// store all the output to this file
while (std::getline (file, inputLine)) // read the strings in the input file
{
if( strncmp( "----", inputLine.c_str(), 4 ) == 0 )
continue;
//calculating binary and hexadecimal values
char *opr = "^+-/%*=,()";
std::string::iterator end_pos = std::remove(inputLine.begin(),
inputLine.end(), ' ');
inputLine.erase(end_pos, inputLine.end());
string str=inputLine;
string str2="";
int length=str.length();
char t[length];
str.copy(t, length);
t[length] = '\0';
char* tok;
char *cop=new char [length];
str.copy(cop,length);
char *w = strtok_fixed( t, opr );
while (w!=NULL)
{
string w2=w;
std::stringstream tr;
tr << w2;
w2.clear();
tr >> w2;
int x=w2.length();
int y=x-3;
string check= w2.substr(0,3);
string check1=w2.substr(0,x);
if(check.find("0x") != std::string::npos)
{
unsigned int x= strtol(w2.c_str(), NULL, 0);
std::ostringstream s;
s << x;
const std::string ii(s.str());
str2=str2+ ii;
}
else if (check1.find("b")!=std::string::npos)
{
w2.pop_back();
long bin=std::strtol(w2.c_str(),0,2);
std::ostringstream s2;
s2<<bin;
const std::string t2(s2.str());
//inputLine.replace(inputLine.find(w2),(w2.length()+1),t2);
str2=str2+t2;
}
else
{
str2=str2+w2;
}
char a =cop[w-t+strlen(w)];
string s1="";
s1=s1+a;
std::stringstream tr1;
tr1 << s1;
s1.clear();
tr1 >> s1;
str2=str2+s1;
w = strtok_fixed (NULL, opr);
}
//str2 should be taken to the parser for final evaluations
Parser p(str2);
double value = p.Evaluate ();
std::cout<<"----------------------"<<endl;
std::cout << "Result = " << value << std::endl;
std::cout<<"----------------------"<<endl;
return 0;
}
}
intmain()
{
字符串输入线;
ifstream文件(“input.txt”);//要读取的输入文件
流文件1;
file1.open(“output.txt”);
freopen(“output.txt”,“w”,stdout);//将所有输出存储到此文件中
while(std::getline(file,inputLine))//读取输入文件中的字符串
{
如果(strncmp(“---”,inputLine.c_str(),4)=0)
持续
//计算二进制和十六进制值
char*opr=“^+-/%*=,()”;
std::string::迭代器end\u pos=std::remove(inputLine.begin(),
inputLine.end(),“”);
inputLine.erase(end_pos,inputLine.end());
字符串str=输入行;
字符串str2=“”;
int length=str.length();
字符t[长度];
str.copy(t,长度);
t[length]='\0';
char*tok;
char*cop=新字符[长度];
str.copy(cop,长度);
char*w=strtok_固定(t,opr);
while(w!=NULL)
{
字符串w2=w;
std::stringstream tr;
tr>w2;
int x=w2.length();
int y=x-3;
字符串检查=w2.substr(0,3);
字符串check1=w2.substr(0,x);
if(check.find(“0x”)!=std::string::npos)
{
无符号int x=strtol(w2.c_str(),NULL,0);
std::ostringstream s;
问题终于解决了
return 0;
}
}
应该是
}
return 0;
}
您将从while循环内部返回,而不是在while循环完成后返回
您应该花时间正确缩进代码。这将帮助您发现此类错误。您还应该学会将代码分解为更小的函数。这同样会帮助您更好地理解自己的代码。我不明白。为什么要将strncmp
与std::string
一起使用std::string
?试试std::string::find
。请用调试会话的结果编辑您的帖子。指出导致问题的语句。同时说明预期的输入和预期的输出。为什么要打开而不使用输出流?char*opr=“^+-/%*=,()”;
必须是const char*opr=“^+-/%*=,()”;
字符串s1=“”
可以是字符串s1;
…我鼓励您使用选项-pedantic-Wall
进行编译,并删除编译器发出的问题信号