C++文件读写操作(二)逐字符读取文本和逐行读取文本

 

相关文章


C++文件读写操作(一)将字母表写入TXT文本文件 


C++文件读写操作(二)逐字符读取文本和逐行读取文本 


C++文件读写操作(三)如何统计文本的行数及如何读取文件某一行内容 


C++文件读写操作(四)读取文件数据到临时数组 


 
#include <iostream>
#include <fstream>
using namespace std;
void testByChar()
{
    fstream testByCharFile;
    char c;
    testByCharFile.open("inFile.txt",ios::in);
    while(!testByCharFile.eof())
    {
        testByCharFile>>c;
        cout<<c;
    }
    testByCharFile.close();
}
void testByLine()
{
    char buffer[256];
    fstream outFile;
    outFile.open("inFile.txt",ios::in);
    cout<<"inFile.txt"<<"--- all file is as follows:---"<<endl;
    while(!outFile.eof())
    {
        outFile.getline(buffer,256,'\n');//getline(char *,int,char) 表示该行字符达到256个或遇到换行就结束
        cout<<buffer<<endl;
    }
    outFile.close();
}
int main()
{
   cout<<endl<<"逐个字符的读取文件:testByChar() "<<endl<<endl;
   testByChar();
   cout<<endl<<"将文件每行内容存储到字符串中,再输出字符串 :testByLine()"<<endl<<endl;
   testByLine();
}
/**********************
运行结果

逐个字符的读取文件:testByChar()

1a2b3c4d5e6f7g8h9i10j11k12l13m14n15o16p17q18r19s20t21u22v23w24x25y26zz
将文件每行内容存储到字符串中,再输出字符串 :testByLine()

inFile.txt--- all file is as follows:---
 1      a
 2      b
 3      c
 4      d
 5      e
 6      f
 7      g
 8      h
 9      i
10      j
11      k
12      l
13      m
14      n
15      o
16      p
17      q
18      r
19      s
20      t
21      u
22      v
23      w
24      x
25      y
26      z


Process returned 0 (0x0)   execution time : 0.484 s
Press any key to continue.
*************************************************/

  • 24
    点赞
  • 88
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值