C++/QT/Python/MATLAB獲取文件行數(shù)的示例詳解
1. C獲取文件行數(shù)
#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++獲取文件行數(shù)
#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獲取文件行數(shù)
#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獲取文件行數(shù)
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獲取文件行數(shù)
方法一:使用numel函數(shù)
filename = 'your_file.txt'; % 文件名
fileID = fopen(filename, 'r'); % 打開文件
data = textscan(fileID, '%s', 'Delimiter', '\n'); % 按行讀取數(shù)據(jù)并存儲在一個單元格數(shù)組中
fclose(fileID); % 關(guān)閉文件
numLines = numel(data{1}); % 計算行數(shù)
disp(['文件行數(shù)為:', num2str(numLines)]);方法二:使用size函數(shù)
filename = 'your_file.txt'; % 文件名
fileID = fopen(filename, 'r'); % 打開文件
data = textscan(fileID, '%s', 'Delimiter', '\n'); % 按行讀取數(shù)據(jù)并存儲在一個單元格數(shù)組中
fclose(fileID); % 關(guān)閉文件
numLines = size(data{1}, 1); % 計算行數(shù)
disp(['文件行數(shù)為:', num2str(numLines)]);到此這篇關(guān)于C++/QT/Python/MATLAB獲取文件行數(shù)的示例詳解的文章就介紹到這了,更多相關(guān)獲取文件行數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux上設(shè)置Ollama服務(wù)配置(常用環(huán)境變量)
本文主要介紹了Linux上設(shè)置Ollama服務(wù)配置(常用環(huán)境變量),Ollama提供了多種環(huán)境變量供配置,如調(diào)試模式、模型目錄等,下面就來介紹一下,感興趣的可以了解一下2025-03-03
HTTP?413狀態(tài)碼詳解與前端處理請求體過大教程
狀態(tài)碼是服務(wù)器在處理請求后返回給客戶端的一組標(biāo)準(zhǔn)化數(shù)字代碼,它們通常與特定的含義相聯(lián)系,并且可以幫助開發(fā)者快速定位問題,這篇文章主要介紹了HTTP?413狀態(tài)碼詳解與前端處理請求體過大的相關(guān)資料,需要的朋友可以參考下2026-01-01
IntelliJ IDEA 2020最新注冊碼(親測有效,可激活至 2089 年
這篇文章主要介紹了IntelliJ IDEA 2020最新注冊碼,親測有效,可激活至 2089 年,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
如何在本地部署 DeepSeek Janus Pro 文生圖大模型
DeepSeek JanusPro模型在本地成功部署,支持圖片理解和文生圖功能,通過Gradio界面進(jìn)行交互,展示了其強(qiáng)大的多模態(tài)處理能力,本文介紹本地部署 DeepSeek Janus Pro 文生圖大模型的操作,感興趣的朋友一起看看吧2025-02-02
thymeleaf實現(xiàn)th:each雙重多重嵌套功能
今天給大家分享一個使用 thymeleaf 實現(xiàn)一個動態(tài)加載一二級文章分類的功能,本文通過代碼講解的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友參考下吧2019-11-11
MyBatisCodeHelper-Pro插件破解版詳細(xì)教程[2.8.2]
MyBatisCodeHelper-Pro是IDEA下的一個插件,功能類似mybatis plugin。這篇文章給大家介紹MyBatisCodeHelper-Pro插件破解版[2.8.2]的相關(guān)知識,感興趣的朋友跟隨小編一起看看吧2020-09-09
盤點網(wǎng)絡(luò)編程必須要知道的基礎(chǔ)知識
這篇文章主要介紹了盤點網(wǎng)絡(luò)編程必須要知道的基礎(chǔ)知識,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2020-07-07

