C/C++/QT/Python/MATLAB获取文件行数的示例

1. C获取文件行数

c 复制代码
#include <stdio.h>

int main() {
    FILE *file = fopen("path/to/your/file.txt", "r");
    if (file == NULL) {
        printf("Failed to open the file!\n");
        return 0;
    }

    int lineCount = 0;
    char ch;
    while ((ch = fgetc(file)) != EOF) {
        if (ch == '\n') {
            lineCount++;
        }
    }

    printf("Line count: %d\n", lineCount);

    fclose(file);

    return 0;
}

2. C++获取文件行数

cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ifstream file("path/to/your/file.txt");
    if (!file) {
        std::cout << "Failed to open the file!" << std::endl;
        return 0;
    }

    int lineCount = 0;
    std::string line;
    while (std::getline(file, line)) {
        lineCount++;
    }

    std::cout << "Line count: " << lineCount << std::endl;

    file.close();

    return 0;
}

3. Qt获取文件行数

cpp 复制代码
#include <QCoreApplication>
#include <QFile>
#include <QTextStream>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFile file("path/to/your/file.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug() << "Failed to open the file!";
        return a.exec();
    }

    QTextStream in(&file);
    int lineCount = 0;
    while (!in.atEnd())
    {
        QString line = in.readLine();
        lineCount++;
    }

    qDebug() << "Line count: " << lineCount;

    file.close();

    return a.exec();
}

4. Python获取文件行数

python 复制代码
file_path = 'path/to/your/file.txt'

try:
    with open(file_path, 'r') as file:
        line_count = sum(1 for line in file)
        print(f"Line count: {line_count}")
except IOError:
    print("Failed to open the file!")

5. MATLAB获取文件行数

  • 方法一:使用numel函数

    matlab 复制代码
    filename = 'your_file.txt';  % 文件名
    fileID = fopen(filename, 'r');  % 打开文件
    data = textscan(fileID, '%s', 'Delimiter', '\n');  % 按行读取数据并存储在一个单元格数组中
    fclose(fileID);  % 关闭文件
    
    numLines = numel(data{1});  % 计算行数
    disp(['文件行数为:', num2str(numLines)]);
  • 方法二:使用size函数

    matlab 复制代码
    filename = 'your_file.txt';  % 文件名
    fileID = fopen(filename, 'r');  % 打开文件
    data = textscan(fileID, '%s', 'Delimiter', '\n');  % 按行读取数据并存储在一个单元格数组中
    fclose(fileID);  % 关闭文件
    
    numLines = size(data{1}, 1);  % 计算行数
    disp(['文件行数为:', num2str(numLines)]);
相关推荐
七夜zippoe7 小时前
Python网络编程实战:从TCP/IP到WebSocket的协议演进与核心技术解析
网络·python·websocket·tcp/ip·socket·心跳机制
jj008u7 小时前
Garmin 中国区活动同步到国际区的一个简单实现方案
python·ai编程
GG向前冲7 小时前
【Python 金融量化】线性模型在AAPL股票数据的分析研究
大数据·python·机器学习·ai·金融
程序猿20237 小时前
Java Thread
java·开发语言·python
却道天凉_好个秋7 小时前
c++ 四叉树
c++·hevc·四叉树
王老师青少年编程7 小时前
信奥赛C++提高组csp-s之倍增算法思想及应用(2):LCA
c++·lca·csp·信奥赛·csp-s·提高组·倍增算法
CSDN_RTKLIB7 小时前
【编码实战】源文件不同编码控制台输出过程
c++
喵手7 小时前
Python爬虫零基础入门【第九章:实战项目教学·第8节】可观测性:日志规范 + trace_id + 可复现错误包!
爬虫·python·日志规范·python爬虫实战·python爬虫工程化实战·python爬虫零基础入门·可复性错误包
嫂子开门我是_我哥7 小时前
第五节:字符串处理大全:文本操作的“万能工具箱”
开发语言·python
2501_927773077 小时前
嵌入式——I.MX6ULL裸机环境配置
c语言·嵌入式硬件