读取数据流

这是一个C++程序,演示了如何计算文本文件的行数以及如何按行读取文件内容。通过`CountLines`函数计算文件行数,`ReadLine`函数读取指定行的数据。程序在不同情况下给出错误提示,如输入行数错误、文件不存在或行数超出文件长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//如何统计文本的行数及如何读取文件某一行内容:  
  
#include <iostream>  
#include <fstream>  
#include <string>  
using namespace std;  
  
int CountLines(char *filename)  
{  
    ifstream ReadFile;  
    int n=0;  
    string tmp;  
    ReadFile.open(filename,ios::in);//ios::in 表示以只读的方式读取文件  
    if(ReadFile.fail())//文件打开失败:返回0  
    {  
        return 0;  
    }  
    else//文件存在  
    {  
        while(getline(ReadFile,tmp,'\n'))  
        {  
            n++;  
        }  
        ReadFile.close();  
        return n;  
    }  
}  
  
string ReadLine(char *filename,int line)  
{  
    int lines,i=0;  
    string temp;  
    fstream file;  
    file.open(filename,ios::in);  
    lines=CountLines(filename);  
  
    if(line<=0)  
    {  
        return "Error 1: 行数错误,不能为0或负数。";  
    }  
    if(file.fail())  
    {  
        return "Error 2: 文件不存在。";  
    }  
    if(line>lines)  
    {  
        return "Error 3: 行数超出文件长度。";  
    }  
    while(getline(file,temp)&&i<line-1)  
    {  
        i++;  
    }  
    file.close();  
    return temp;  
}  
int main()  
{  
    int line;  
    char filename[]="inFile.txt";  
    cout<<"该文件行数为:"<<CountLines(filename)<<endl;  
    cout<<"\n请输入要读取的行数:"<<endl;  
    while(cin>>line)  
    {  
        cout<<"第"<<line<<"行的内容是 :"<<endl;  
        cout<<ReadLine(filename,line);  
        cout<<"\n\n请输入要读取的行数:"<<endl;  
    }  
}  
/********************************** 
程序运行情况如下: 
该文件行数为:26 
 
请输入要读取的行数: 
-3 
第-3行的内容是 : 
Error 1: 行数错误,不能为0或负数。 
 
请输入要读取的行数: 
4 
第4行的内容是 : 
 4      d 
 
请输入要读取的行数: 
8 
第8行的内容是 : 
 8      h 
 
请输入要读取的行数: 
26 
第26行的内容是 : 
26      z 
 
请输入要读取的行数: 
33 
第33行的内容是 : 
Error 3: 行数超出文件长度。 
 
请输入要读取的行数: 
66 
第66行的内容是 : 
Error 3: 行数超出文件长度。 
 
请输入要读取的行数: 
^Z 
 
Process returned 0 (0x0)   execution time : 24.632 s 
Press any key to continue. 
 
**********************************/ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值