对于C++编译运行文件,我使用过两个编译器,一个是visual studio 2013,另外一个是devcpp,推荐使用devcpp。

vs的特点是界面整洁清晰,但是需要收费,这是微软的,并且在电脑上的运行速度是非常慢的; 而devcpp的特点是界面比较简陋,但是该软件免费下载,运行速度快,软件响应快。

例0.0:

这里介绍cin和cout对象的方法: cin.get()以及cout.put(),前者是将用户的输入获取,返回获取到的一个字符; 后者接受一个参数,返回值就是在控制台输出这个字符。如下所示:

#include <iostream>
using namespace std;
int main()
{
char c;
cout << "Input the words: " << endl;
while ((c= cin.get()) != EOF)
{
cout.put(c);
}
cout << "That's the end!" << endl;
return ;
}

这里就是将用户输入的依次获取,并且输出,知道判断为EOF,则停止程序。结果如下:

例0.1

这里在介绍getline函数和read函数。

其中 getline函数的作用是从输入流中读取一行字符,其用法和上面将的get函数输入一行字符的功能类似,如cin.getline();

read函数可以从输入流中读取指定书目的字符并将他们存放在指定的数组当中,格式如下:

cin.read(char *buf, int size);

eof函数是end of file的缩写,表示文件结束。 如果读取数据到达末尾,eof函数值为真,表示遇到文件结束符;eof函数值为假,表示没有遇到文件结束符。

另外,peek函数的作用是从输入流中返回下一个字符,但他只是观测,指针仍然停留在当前位置,遇到流结束标志时返回EOF。 调用形式为: c= cin.peek();

而cin.putback函数作用是将前面用get或者getline函数从输入流中读取的字符ch返回到输入流,插入到当前指针位置,供后面读取。

例1:

如下所示,首先创建一个文件流对象,接着打开文件,进行输入,最后关闭。 读取文件内容与之相似。

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
cout << "this is the begining!" << endl;
int n;
double d;
ofstream outfile;
outfile.open("C:\\Users\\Administrator\\Desktop\\fileTest\\fileTest\\test.txt", ios::out);
//outfile.open("./test.txt", ios::out);
outfile << << endl;
outfile << 3.8 << endl;
outfile.close();
cout << "successful!" << endl;
ifstream infile("C:\\Users\\Administrator\\Desktop\\fileTest\\fileTest\\test.txt", ios::in);
//ifstream infile("./test.txt", ios::in);
infile >> n >> d;
cout << n << "," << d << endl;
infile.close();
return ;
}

这里绝对路径使用“\\”是转义后的结果,相对路径暂未成功。

程序执行完毕,可发现内容为50 3.8,成功输入。

例2

#include <iostream>
#include <fstream> using namespace std;
int main()
{
cout << "this is the beginning!" << endl;
fstream outfile;
outfile.open("C:\\Users\\Administrator\\Desktop\\fileTest\\fileTest\\test1.txt", ios::out);
if (!outfile)
{
cout << "open failed!" << endl;
exit();
}
outfile << "Hello world! \n";
outfile << "github ours github! \n";
outfile.close();
return ;
}

如果打开文件失败,outfile返回0,则 exit(1)退出程序。

另外,注意 ios::out 这个实参必填。最后要 close 即可。

且如果没有该文件可打开,则会自动创建文件,并完成后续输入。

例3

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
fstream outfile;
outfile.open("C:\\Users\\Administrator\\Desktop\\fileTest\\fileTest\\test2.txt", ios::out);
if (!outfile)
{
cerr << "open out failed !" << endl;
exit();
}
outfile << "Hello world! \n";
outfile << "github! \n";
outfile.close(); char s[];
fstream infile;
infile.open("C:\\Users\\Administrator\\Desktop\\fileTest\\fileTest\\test2.txt", ios::in);
if (!infile)
{
cerr << "open failed!" << endl;
exit();
}
while (!infile.eof())
{
infile.getline(s, sizeof(s));
cout << s << endl;
}
infile.close();
return ;
}

如上所示:我们先通过outfile输入内容,然后使用infile读取磁盘内容,infile.eof() 中eof即end of file,即如果不是file的最后一行,就继续读取文件,并且输出。且每次在操作时,无论读取,都需要进行打开文件已经关闭文件的操作。

c++中文件读取的更多相关文章

  1. Perl中文件读取操作

    Perl中文件读取操作 http://blog.csdn.net/yangxuan12580/article/details/51506216

  2. 详解Js中文件读取机制

    前言,文件读取是提高应用体验度的必须接口,应用场景中需求很频繁. Js处理文件读取,由于处于安全方面的考虑,在2000年以前,都是以“<input type="file"&g ...

  3. Java开发中文件读取方式总结

    JAVA开发中,免不了要读文件操作,读取文件,首先就需要获取文件的路径. 路径分为绝对路径和相对路径. 在文件系统中,绝对路径都是以盘符开始的,例如C:\abc\1.txt. 什么是相对路径呢?相对路 ...

  4. 用python对txt中文件读取,然后按顺序标号存入excel中

    读取txt文件,然后存入excel中 import xlwt #从txt文件中读取内容,放到ftext1中 f1=open('/Users/XXX/Documents/pythonwork/tenso ...

  5. OC中文件读取类(NSFileHandle)介绍和常用使用方法

    NSFileHandle 1.NSFileManager类主要对于文件的操作(删除,修改,移动,赋值等等) //判断是否有 tagetPath 文件路径,没有就创建 NSFileManager *fi ...

  6. [转]关于Unity中文件读取 - 大世界

     原文  http://www.cnblogs.com/ThreeThousandBigWorld/p/3199245.html 存储: 在程序发布后文件的存放有两种,第一种是打包到Uniyt的资源包 ...

  7. 关于Unity中文件读取

    存储: 在程序发布后文件的存放有两种,第一种是打包到Uniyt的资源包中(*.unity3D),第二种就是把文件存放在一个特殊的目录如:StreamingAssets,这个目录的东西Unity不会打包 ...

  8. python文件夹中文件读取踩坑

    Q: 进行数据集图片预处理时,初始命名如下图(Fig1左),发现读取文件时,读取的结构并非如所设想的那样顺序读取 Fig 1 A: pyhton读取文件的时候,按照文件名的ascii码中的顺序进行逐位 ...

  9. java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

随机推荐

  1. 6-具体学习git--分支冲突,rebase|| stash 临时修改

    rebase很危险. https://morvanzhou.github.io/tutorials/others/git/

  2. Redis的appendfsync参数详解

    redis.conf中的appendfysnc是对redis性能有重要影响的参数之一.可取三种值:always.everysec和no. 设置为always时,会极大消弱Redis的性能,因为这种模式 ...

  3. ORA-06553: PLS-553: character set name is not recognized, while starting Content Store

    Symptom CM-CFG-5029 Content Manager is unable to determine whether the content store is initialized. ...

  4. Ng第四课:多变量线性回归(Linear Regression with Multiple Variables)

    4.1  多维特征 4.2  多变量梯度下降 4.3  梯度下降法实践 1-特征缩放 4.4  梯度下降法实践 2-学习率 4.5  特征和多项式回归 4.6  正规方程 4.7  正规方程及不可逆性 ...

  5. 让页面整体变灰css设置

    上次看到某人去世了,百度就把相应介绍某人的信息页面全部灰掉,于是寻找到了种简单的方法,只需设置html html { filter: grayscale(100%); -webkit-filter: ...

  6. (广搜)Fire Game -- FZU -- 2150

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/I Fire Game Time Limit:1000MS    ...

  7. nodeclub route

    这里是把web_router.js放在根目录下,也可以放在routes文件件下,其实都可以. 这里就是一些url与controller和middleware对应

  8. 3.1.1随机事件的概率的Breamer(2018-03-22)

    % !Mode:: "TeX:UTF-8" \documentclass[xcolor=svgnames,serif,table,12pt]{beamer}%, %\include ...

  9. [javascript]jsonp-function 代码段

    (function($1454395832823,arr_infoList /**/) { $1454395832823.push(' '); for(var i in arr_infoList) { ...

  10. unidbgrid显示列的合计值

    procedure TfrmClient.UniDBGrid1ColumnSummaryResult(Column: TUniDBGridColumn; GroupFieldValue: Varian ...