• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > C++文件读写代码分享

C++文件读写代码分享

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了c++打开文件代码,c++文件读写,c++文本文件读写,c++中文件的读写,c++二进制文件读写等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

编写一个程序,统计data.txt文件的行数,并将所有行前加上行号后写到data1.txt文件中。

算法提示:

行与行之间以回车符分隔,而getline()函数以回车符作为终止符。因此,可以采用getline()函数读取每一行,再用一个变量i计算行数。

(1)实现源代码

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
 
using namespace std;
 
int coutFile(char * filename,char * outfilename)
{
  ifstream filein;
  filein.open(filename,ios_base::in);
  ofstream fileout;
  fileout.open(outfilename,ios_base::out);
  string strtemp;
  int count=0;
  while(getline(filein,strtemp))
  {
    count++;
    cout<<strtemp<<endl;
    fileout<<count<<" "<<strtemp<<endl;
  }
  filein.close();
  fileout.close();
  return count;
}
 
 
void main()
{
  cout<<coutFile("c:\\data.txt","c:\\data1.txt")<<endl;
}
</div>

再来一个示例:

下面的C++代码将用户输入的信息写入到afile.dat,然后再通过程序读取出来输出到屏幕

#include <fstream>
#include <iostream>
using namespace std;
  
int main ()
{
   
  char data[100];
 
  // open a file in write mode.
  ofstream outfile;
  outfile.open("afile.dat");
 
  cout << "Writing to the file" << endl;
  cout << "Enter your name: ";
  cin.getline(data, 100);
 
  // write inputted data into the file.
  outfile << data << endl;
 
  cout << "Enter your age: ";
  cin >> data;
  cin.ignore();
   
  // again write inputted data into the file.
  outfile << data << endl;
 
  // close the opened file.
  outfile.close();
 
  // open a file in read mode.
  ifstream infile;
  infile.open("afile.dat");
  
  cout << "Reading from the file" << endl;
  infile >> data;
 
  // write the data at the screen.
  cout << data << endl;
   
  // again read the data from the file and display it.
  infile >> data;
  cout << data << endl;
 
  // close the opened file.
  infile.close();
 
  return 0;
}

</div>

程序编译执行后输出如下结果

$./a.out
Writing to the file
Enter your name: Zara
Enter your age: 9
Reading from the file
Zara
9

</div>

以上所述就是本文的全部内容了,希望大家能够喜欢。

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C++文件读写代码分享

相关文章

  • 2017-05-28C/C++中指针和引用之相关问题深入研究
  • 2017-05-28C语言实现xml构造解析器
  • 2017-05-28使用c语言判断100以内素数的示例(c语言求素数)
  • 2017-05-28C++中运算符 &和&&、|和|| 的详解及区别
  • 2017-05-28浅谈c++调用python链接的问题及解决方法
  • 2017-05-28char str[] 与 char *str的区别详细解析
  • 2017-05-28理解数据结构
  • 2017-05-28浅谈返回函数内部new分配的内存的引用
  • 2017-05-28VC实现的病毒专杀工具完整实例
  • 2017-05-28C++中I/O模型之select模型实例

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 探讨编写int strlen(char *strDest);不允许定义变量的问题
    • C语言new操作的安全性分析
    • C语言 扫雷程序的实现
    • 浅析C++中的虚函数
    • c++11可变参数使用示例
    • 解读C++编程中派生类的构成和创建
    • C++设计模式之原型模式
    • 深入剖析设计模式中的组合模式应用及在C++中的实现
    • 使用C语言实现最小生成树求解的简单方法
    • 如何在程序中判断VS的版本(实现方法详解)

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有