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

