-
2019-08-06 10:43:20
- 问题:最近开发过程中,需要处理txt大文件,想要定位到某一行操作时,通过按行读取移动标志位方法明显卡顿,如何解决卡顿问题?
#include "stdafx.h" #include <sstream> #include <string> using namespace std; ifstream & seek_to_line(ifstream & in, int line); int main() { string labels_txt="E:/bdd100k.txt"; int line=100;//按逻辑修改行数 ifstream file; file.open(labels_txt); if (!file&&flag) cerr << "数据集标注txt文件不存在,检查路径!" << endl; seek_to_line(file, line);//定位到txt的指定行 while (getline(file, data))//按行读取txt { //操作 } file.close(); } //定位到txt文件的某一行 ifstream & seek_to_line(ifstream & in, int line) //将打开的文件in,定位到line行。 { int i; char buf[1024]; in.seekg(0, ios::beg); //定位到文件开始。 for (i = 0; i < line; i++) { in.getline(buf, sizeof(buf));//读取行。 } return in; }
更多相关内容 -
C++实现一行一行读取文本的方法
2020-08-27 04:54:39今天小编就为大家分享一篇C++实现一行一行读取文本的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 -
C++ 读取 txt 文件件中某一行
2019-09-02 18:49:26"parameter.txt" , 3 ) ; cout "命令格参数:" endl ; cout exePath endl ; cout command1 endl ; cout command2 endl ; getchar ( ) ; return 0 ; } 效果图...string ReadLine(char *filename,int line) { int i=0; string temp; fstream file; file.open(filename,ios::in); if(line<=0) { return "Error 1: 行数错误,不能为0或负数。"; } if(file.fail()) { return "Error 2: 文件不存在。"; } while(getline(file,temp)&&i<line-1) { i++; } file.close(); return temp; } int _tmain(int argc, _TCHAR* argv[]) { // 初始化 string exePath = ReadLine("parameter.txt",1); string command1 = ReadLine("parameter.txt",2); string command2 = ReadLine("parameter.txt",3); cout << "命令格参数:" <<endl; cout << exePath << endl; cout << command1 << endl; cout << command2 << endl; getchar(); return 0; }
效果图:
-
C++ 修改txt某一行内容
2021-03-26 13:50:29vs2019,Unicode,C++ 代码: #pragma once #include <afxwin.h> #include <iostream> #include <cstring> #include <string> #include <fstream> #include <direct.h> ...- vs2019,Unicode,C++
- 代码:
#pragma once #include <afxwin.h> #include <iostream> #include <cstring> #include <string> #include <fstream> #include <direct.h> bool GetTxtValue(_In_ string sKey,_Out_ string * strTxtValue) { USES_CONVERSION; char cWorkPath[MAX_PATH]; // 工作目录 char cLineBuffer[MAX_PATH]; string sAppConfig; // AppConfig.txt 路径,用于读 string sLineBuffer; int iPtr = 0; BOOL bFindKey = FALSE; ifstream ifAppConfig; getcwd(cWorkPath, MAX_PATH); sAppConfig = cWorkPath; sAppConfig = sAppConfig + "\\AppConfig.txt"; ifAppConfig.open(sAppConfig.c_str(), ios::in | ios::binary); if (ifAppConfig.is_open()) { while (ifAppConfig.getline(cLineBuffer, MAX_PATH)) // 获取 sKey 所在行 { sLineBuffer = cLineBuffer; if (string::npos != sLineBuffer.find(sKey)) // 找到关键词 { iPtr = sLineBuffer.find(sKey); // 获取 sKey 所在的下标 *strTxtValue = sLineBuffer.substr(iPtr + sKey.size() + 1, sLineBuffer.size()); // 获取路径 bFindKey = TRUE; } } } else { // 没有打开文件 MessageBox(_T("打开文件失败,请检查路径")); return NULL; } ifAppConfig.close(); return true; }
- 说明:该代码读取AppConfig.txt,第一个参数为AppConfig.txt中的关键词;第二个参数是获取关键词所在行的内容(不包括关键词本身)。
AppConfig.txt内容如下,每一行空格前是关键词,空格后是对应的值。
-
c++ 从txt文件读取内容,读取某一行数据
2021-05-31 20:45:11} } int main() { char filename[] = "g1.txt"; fstream file; file.open(filename,ios::in); ifstream myfile(filename); game g2; gj gj1; // if(!myfile.is_open()) // { // cout ; // return 0; // } //int row...#include <iostream> #include <fstream> #include <string> using namespace std; struct game { int num; int year; float arr[3]; }; struct gj { int a; game dz; }; int count_lines(char *filename) { int n = 0; ifstream ReadFile; string tmp; ReadFile.open(filename,ios::in); if(ReadFile.fail()) { return 0; } else { while (getline(ReadFile, tmp, '\n')) { n++; } ReadFile.close(); cout<<"the rows of txt = " << n<<endl; return n; } } int main() { char filename[] = "g1.txt"; fstream file; file.open(filename,ios::in); ifstream myfile(filename); game g2; gj gj1; // if(!myfile.is_open()) // { // cout << "can not open this file" << endl; // return 0; // } //int row = count_lines("g1.txt"); if (file.fail()) { cout<< "file not exit"<<endl; return 0; } string s ; int line = 4; int i = 0; // myfile >> gj1.dz.num >>gj1.dz.year >>gj1.dz.arr[0]>>gj1.dz.arr[1]>>gj1.dz.arr[2]; // cout << "value = " << gj1.dz.year; while (getline(myfile,s)&&i < line-2) { i++; } myfile >> gj1.dz.num >>gj1.dz.year >>gj1.dz.arr[0]>>gj1.dz.arr[1]>>gj1.dz.arr[2]; //myfile >> g2.num >> g2.year >> g2.arr[0] >>g2.arr[1]>>g2.arr[2]; cout << "value = " << gj1.dz.year; cout <<" value g2 = " <<gj1.dz.arr[0]<<endl; int years = gj1.dz.year; cout << years; myfile.close(); return 0; }
-
C++中读写txt文件并分离字符的方法
2020-12-26 07:35:10在实际工程中,经常遇到需要读取txt文件,txt文件中存的是一些小数或者整型数据,在C++中,可以利用string类和ifstream库文件对txt进行的读取,不过读回的数据经常是以字符串的形式返回,一般是txt的一行为一个字符... -
C++实现逐行读取TXT文件的内容,并将指定的内容输出来
2018-04-26 15:37:22C++实现读取TXT文件里面的内容,一行一行的读取,并自动换行,自动将指定的内容读取出来或是输出到界面显示 -
C++读取到回车换行符问题处理
2020-09-03 11:03:43有一个程序只需对输入的一行字符一个个进行独立判断,C的话用getchar()就好了,但是用C++的时候发现CIN似乎不接受回车符……搜索解决方法的时候很多人都建议将getline,然后处理数组或者定义一个流什么的,但是这样... -
c++按行读写txt文件
2011-12-26 22:59:16在VS2008下自己写的c++txt文件输入输出,包括:逐行读取Txt、将每行按指定分隔符分解、string类型向数值型转换、内存内容写txt,注释较详细,具有实用性,希望有所帮助。 -
C++逐行读取txt整条内容和分段内容
2018-10-23 16:05:14通过提取输入文件(txt格式),输出每个学生的作业平均分和总分,并根据人数和作业数自动修改 number_of_students 9 number_of_assignments 3 student_number first_name last_name 10 10 10 98861 BOB BARKER 8 5 9... -
读取txt文件的c++程序将特定数据从txt文件中摘出来
2019-04-11 18:52:06一段读取txt文件的小程序,使用c++编写,经测试安全可用 -
C++读取TXT文件识别特定内容修改
2022-04-14 15:46:03由于近期需要将一份Word文档转到Markdown格式,但是文件内容较大,自动转换...程序中实现了UTF-8格式转GBK格式,使用stream对TXT进行读取并识别,获取到需要的信息后对信息进行更改,最后依据一级标题对文件进行拆分。 -
C++用类读取和处理TXT文件数据
2018-04-23 14:44:29用C++类读取TXT文件数据,一行一个条数据,每条数据有3个不同数据字段,容器是用vector,字符串类型用string,指针的处理都是用封装的处理,C++就是强大 -
C++读取txt文件里每行的数据
2021-08-06 22:10:54#include <iostream> #include <stdio.h> #include <...const char *board_list = "./example_list.txt"; int main(int argc, char **argv) { FILE *f = fopen(board_list, "rt"); -
C++将txt文件内容保存到数组的方法
2020-08-27 03:49:49今天小编就为大家分享一篇C++将txt文件内容保存到数组的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 -
C++从txt文档里读取数据并且输出
2018-04-16 09:40:41在C++环境下读取txt文档中的数据,并且复制给矩阵,方便对矩阵数据的处理和运算,并实现输出检查代码的可用性。 -
C++ 从TXT文件中一行一行的读取数据并且保存到数组中
2020-07-05 22:26:17#include <fstream> ...// 功能:将filename 中的数据(共cols列)读取到_vector中,_vector可视为二维数组 int read_scanf(const string &filename, const int &cols, vector<... -
C++读写txt文档
2022-01-27 17:02:09: 打开文件供读写 */ #include<fstream> void main() { /*文件打开模式: *ios::in 只读 *ios::out 只写 *ios::app 从文件末尾开始写,防止丢失文件中原来就有的内容 *ios::binary -
【C++】读取txt文件中指定行的内容
2021-11-20 11:26:01使用c++读取TXT文件中指定行的内容 classification_classes_ILSVRC2012.txt:下载链接 验证: #include <iostream>...// 读取txt文件的某一行 int CountLines(string filename) { ifstream Read -
C++ 读取TXT文件中的数据 每一行空格符相隔的数据单独取出
2020-11-22 20:15:04infile.open("CarPose.txt"); infile.getline(line, 1024); stringstream word(line); word >> x >> y >> z >> roll >> pitch >> yaw; cout (9) ; system("pause"); return 0; } ![结果]... -
C++ 读文件某一行(特定行)数据
2020-01-02 16:51:51这段代码主要功能: ...//这两个函数一起可以读取文件中某一特定行,行数line从1开始,Readline返回值为string类型 int CountLines(string filename) { ifstream ReadFile; int n=0; string tmp; ... -
c++(bcb)读取txt最后一行
2019-07-23 23:51:32c++读取TXT文件,读取最后一行。 TStringList *slt=new TStringList; slt->LoadFromFile(".//a.txt"); String x = ""; int len = slt->Count; while(slt->Strings[--len] != " ") { x = Utf8ToAnsi(slt-&... -
C++读取txt文件的行数据和列数据
2020-03-30 21:16:43数据如下图:每一行的第一列是时间,后面的几列是附带的信息。如何读取特定行中的某些列数据。 基本思路:假设我们感兴趣的信息是第一列数据为“2019-01-20 23:43:29”后面的三列数据,将txt 文件按行读取后,通过... -
C++按行读取txt
2022-03-03 10:33:17#include #include #include #include using namespace std;...while (getline(f, line))//会自动把换行符去掉 { words.push_back(line); } //dictionary.txt在csdn里面可以下载,里面有4万多个单词,相当于 -
C++读写txt文件
2021-04-20 16:31:411、创建txt文件并写入 方法一: QFile file("文件存储地址"); if(! file.open(QIODevice::Append|QIODevice::Text)) //append追加,不会覆盖之前的文件 { QMessageBox::critical(this,"错误","文件打开失败,信息... -
基于C++从某一行开始读取数据
2019-10-11 16:43:43基于C++从某一行开始读取数据 #include <iostream> #include <fstream> using namespace std; int main() { string line1; string str; int a=0;//记录从哪一行开始读取 ifstream file; file.... -
linux下c/c++读取txt文件,多行文件,且每行都用逗号隔开
2022-02-10 16:00:25源码 test.cpp #include<string> #include<iostream> #include <stdlib.h> #include <stdio.h> using namespace std; int main() { FILE *fd;... if ((fd = fopen("data.txt -
C++按行读取txt 字符串
2019-02-01 18:14:29#include <fstream> #include <string> #include <iostream> using namespace std; ...1.txt"); string filename; string line; ... -
C++ 读写TXT
2020-10-29 16:01:481、头文件 文件头: #include <iostream>...//1 逐行读取 void readTxt(string file) { ifstream infile; infile.open(file.data()); //将文件流对象与文件连接起来 assert(infile.is_open()); .
收藏数
189,985
精华内容
75,994