fstream的用法有关问题
fstream的用法问题!
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream inifile;
inifile.open( "e:/123.txt ");
if(!inifile)
{
cerr < < "file is not open " < <endl;
return -1;
}
while(inifile> > "hello ")
cout < < "ok " < <endl;
inifile.close();
inifile.clear();
return 0;
}
为什么会在执行时提示;Text256.exe 遇到问题需要关闭。我们对此引起的不便表示抱歉。
------解决方案--------------------
看兄台的意思好像是
(Dev-cpp)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
ifstream inifile;
inifile.open( "c://123.txt ");
if(!inifile)
{
cerr < < "file is not open " < <endl;
}
else
{
string str( "hello ");
string in;
while((inifile> > in)&&(in==str)) //从文件读入并判断读入内容
{
cout < <str < <endl
< < "ok " < <endl;
}
}
inifile.close();
inifile.clear();
system( "PAUSE ");
return EXIT_SUCCESS;
}
------解决方案--------------------
你上面代码的意思就是读入如果是写入的话
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
ofstream inifile;
inifile.open( "c://123.txt ");
if(!inifile)
{
cerr < < "file is not open " < <endl;
}
else
{
string str( "hello ");
while(inifile < <str) //文件写入
{
cout < <str < <endl
< < "ok " < <endl;
}
}
inifile.close();
inifile.clear();
system( "PAUSE ");
return EXIT_SUCCESS;
}
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream inifile;
inifile.open( "e:/123.txt ");
if(!inifile)
{
cerr < < "file is not open " < <endl;
return -1;
}
while(inifile> > "hello ")
cout < < "ok " < <endl;
inifile.close();
inifile.clear();
return 0;
}
为什么会在执行时提示;Text256.exe 遇到问题需要关闭。我们对此引起的不便表示抱歉。
------解决方案--------------------
看兄台的意思好像是
(Dev-cpp)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
ifstream inifile;
inifile.open( "c://123.txt ");
if(!inifile)
{
cerr < < "file is not open " < <endl;
}
else
{
string str( "hello ");
string in;
while((inifile> > in)&&(in==str)) //从文件读入并判断读入内容
{
cout < <str < <endl
< < "ok " < <endl;
}
}
inifile.close();
inifile.clear();
system( "PAUSE ");
return EXIT_SUCCESS;
}
------解决方案--------------------
你上面代码的意思就是读入如果是写入的话
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
ofstream inifile;
inifile.open( "c://123.txt ");
if(!inifile)
{
cerr < < "file is not open " < <endl;
}
else
{
string str( "hello ");
while(inifile < <str) //文件写入
{
cout < <str < <endl
< < "ok " < <endl;
}
}
inifile.close();
inifile.clear();
system( "PAUSE ");
return EXIT_SUCCESS;
}