ifstream

ifstream::read doesn't tell how many bytes it really reads?

可紊 提交于 2019-12-30 08:12:11
问题 I'm using ifstream::read to read a file, ifstream ifs("a.txt"); char buf[1024]; ifs.read(buf, 1024); But a.txt's size might be less than 1000 bytes , so how am I supposed to know how many bytes have been read from ifs ? 回答1: You can get the amount of characters extracted by the last operation with std::ifstream::gcount: ifstream ifs("a.txt"); char buf[1024]; ifs.read(buf, 1024); size_t extracted = ifs.gcount(); or ifstream ifs("a.txt"); char buf[1024]; size_t extracted = ifs.read(buf, 1024)

How can I speed up line by line reading of an ASCII file? (C++)

点点圈 提交于 2019-12-30 02:09:12
问题 Here's a bit of code that is a considerable bottleneck after doing some measuring: //----------------------------------------------------------------------------- // Construct dictionary hash set from dictionary file //----------------------------------------------------------------------------- void constructDictionary(unordered_set<string> &dict) { ifstream wordListFile; wordListFile.open("dictionary.txt"); std::string word; while( wordListFile >> word ) { if( !word.empty() ) { dict.insert

From a file, how do I read from a certain point in C++

≯℡__Kan透↙ 提交于 2019-12-29 09:34:27
问题 What I want to do: I need to create in void main() 3 auto objects. For each object I have the information in a txt file. How do I get the information (engine, max_speed... etc) for each object. How do I skip reading "engine" and how do I skip a whole auto when creating the second object. Thanks! The txt file: (auto.txt) auto1 engine: gasoline max_speed: 250 engine_cc: 1980 avg_consumption_urban: 11 avg_speed_urban: 50 avg_consumption: 8 avg_speed: 100 auto2 engine: diesel max_speed: 230

ifstream:: What is the maximum file size that a ifstream can read

一个人想着一个人 提交于 2019-12-29 07:43:15
问题 I tried to read a 3GB data file using ifstream and it gives me wrong file size, whereas when I read a 600MB file, it gives me the correct result. In addition to wrong file size, I am also unable to read the entire file using ifstream. Here is the code that I used std::wstring name; name.assign(fileName.begin(), fileName.end()); __stat64 buf; if (_wstat64(name.c_str(), &buf) != 0) std::cout << -1; // error, could use errno to find out more std::cout << " Windows file size : " << buf.st_size <<

Using ifstream to read floats

血红的双手。 提交于 2019-12-29 04:02:41
问题 I'm trying to read a series of floats from a .out file using ifstream, but if I output them afterwards, they are not correct. This is my input code: float x, y, z; ifstream table; table.open("Resources/bones.out"); if (table.fail()) { cout << "Can't open table" << endl; return ; } table >> x; table >> y; table >> z; cout << x << " " << y << " " << z << endl; table.close(); My input file: 0.488454 0.510216 0.466979 0.487242 0.421347 0.472977 0.486773 0.371251 0.473103 ... Now for testing, i'm

fopen ,cstdioFile,ifstream速率

回眸只為那壹抹淺笑 提交于 2019-12-26 04:42:42
// TODO: 在此添加控件通知处理程序代码 CString lpszFilePath= _T("C:\\cn.txt"); if (!PathFileExists(lpszFilePath)) return; __int64 nRows = 0, nRows1 = 0, nRows2=0, nRows3 = 0; FILE *fp; int nLen = 1024 * 128; char app1[1024 * 128] = { 0 }; __int64 time_start1 = GetTickCount(); // m_timeCount.Begin(); errno_t _result =_wfopen_s(&fp, lpszFilePath, _T("r")); printf("%d\n", GetLastError()); while (!feof(fp)) { memset(app1, 0x00, nLen); fgets(app1, nLen, fp); nRows++; } printf("%d\n", GetLastError()); fclose(fp); //printf("%s\n", app1); //__int64 nSpan = m_timeCount.End(); __int64 time_start2 = GetTickCount();

How to read a list of filenames from a text file and open them in C++?

孤街浪徒 提交于 2019-12-25 06:06:53
问题 I have a list of files stored in a text file. I read the file line by line and store them in a string array. The file list looks like this: 04_02_1310.csv 04_03_1350.csv 04_04_0421.csv 04_05_0447.csv and so on. Let's call my string array filelist[i] Assuming I am trying the open the first file in the list: inputFile.open(filelist[0].c_str()); // This cannot open file the file cannot be opened. If i place the file name in quotation marks, everything works out fine: inputFile.open("04_02_1310

c++ linux ifstream read csv file

别来无恙 提交于 2019-12-25 05:33:13
问题 void Lexicon::buildMapFromFile(string filename ) //map { ifstream file; file.open(filename.c_str() ); string wow, mem, key; unsigned int x = 0; while(true) { getline(file, wow); if (file.fail()) break; //check for error while (x < wow.length() ) { if (wow[x] == ',') { key = mem; mem.clear(); x++; //step over ',' } else mem += wow[x++]; } list_map0.put(key, mem); //char to string list_map1.put(mem, key); //string to char mem.clear(); //reset memory x = 0;//reset index } file.close(); } This

c++ linux ifstream read csv file

蹲街弑〆低调 提交于 2019-12-25 05:33:06
问题 void Lexicon::buildMapFromFile(string filename ) //map { ifstream file; file.open(filename.c_str() ); string wow, mem, key; unsigned int x = 0; while(true) { getline(file, wow); if (file.fail()) break; //check for error while (x < wow.length() ) { if (wow[x] == ',') { key = mem; mem.clear(); x++; //step over ',' } else mem += wow[x++]; } list_map0.put(key, mem); //char to string list_map1.put(mem, key); //string to char mem.clear(); //reset memory x = 0;//reset index } file.close(); } This

C++ Join two pipe divided files on key fields

女生的网名这么多〃 提交于 2019-12-25 05:03:18
问题 i am currently trying to create a C++ function to join two pipe divided files with over 10.000.000 records on one or two key fields. The fiels look like P2347|John Doe|C1234 P7634|Peter Parker|D2344 P522|Toni Stark|T288 and P2347|Bruce Wayne|C1234 P1111|Captain America|D534 P522|Terminator|T288 To join on field 1 and 3, the expected output should show: P2347|C1234|John Doe|Bruce Wayne P522|T288|Toni Stark|Terminator What I currently thinking about is using a set/array/vector to read in the