使用C ++从文本文件读取数据
这是一个C++程序,用于从文本文件读取数据。
输入值
tpoint.txt is having initial content as “nhooo.com.”
输出结果
nhooo.com.
算法
Begin
Create an object newfile against the class fstream.
Call open() method to open a file “tpoint.txt” to perform write operation using object newfile.
If file is open then
Input a string “nhooo.com" in the tpoint.txt file.
Close the file object newfile using close() method.
Call open() method to open a file “tpoint.txt” to perform read operation using object newfile.
If file is open then
Declare a string “tp”.
Read all data of file object newfile using getline() method and put it into the string tp.
Print the data of string tp.
Close the file object newfile using close() method.
End.示例
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
fstream newfile;
newfile.open("tpoint.txt",ios::out); // open a file to perform write operation using file object
if(newfile.is_open()) //checking whether the file is open
{
newfile<<"nhooo.com \n"; //inserting text
newfile.close(); //close the file object
}
newfile.open("tpoint.txt",ios::in); //open a file to perform read operation using file object
if (newfile.is_open()){ //checking whether the file is open
string tp;
while(getline(newfile, tp)){ //read data from file object and put it into string.
cout << tp << "\n"; //print the data of the string
}
newfile.close(); //close the file object.
}
}输出结果
nhooo.com
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短