fstream

Speed up integer reading from file in C++

喜你入骨 提交于 2019-12-03 21:48:50
I'm reading a file, line by line, and extracting integers from it. Some noteworthy points: the input file is not in binary; I cannot load up the whole file in memory; file format (only integers, separated by some delimiter): x1 x2 x3 x4 ... y1 y2 y3 ... z1 z2 z3 z4 z5 ... ... Just to add context , I'm reading the integers, and counting them, using an std::unordered_map<unsigned int, unsinged int> . Simply looping through lines, and allocating useless stringstreams, like this: std::fstream infile(<inpath>, std::ios::in); while (std::getline(infile, line)) { std::stringstream ss(line); } gives

How do I read a text file from the second line using fstream?

人盡茶涼 提交于 2019-12-03 19:21:10
问题 How can I make my std::fstream object start reading a text file from the second line? 回答1: Use getline() to read the first line, then begin reading the rest of the stream. ifstream stream("filename.txt"); string dummyLine; getline(stream, dummyLine); // Begin reading your stream here while (stream) ... (Changed to std::getline (thanks dalle.myopenid.com)) 回答2: You could use the ignore feature of the stream: ifstream stream("filename.txt"); // Get and drop a line stream.ignore ( std::numeric

What is the difference between flush() and sync() in regard to fstream buffers?

无人久伴 提交于 2019-12-03 15:51:55
问题 I was reading the cplusplus.com tutorial on I/O. At the end, it says fstream buffers are synchronized with the file on disc Explicitly, with manipulators: When certain manipulators are used on streams, an explicit synchronization takes place. These manipulators are: flush and endl. and Explicitly, with member function sync(): Calling stream's member function sync(), which takes no parameters, causes an immediate synchronization. This function returns an int value equal to -1 if the stream has

Best way to split a vector into two smaller arrays?

三世轮回 提交于 2019-12-03 14:48:32
问题 What I'm trying to do: I am trying to split a vector into two separate arrays. The current int vector contains an element per line in a text file. The text file is a list of random integers. How I'm planning to do it: My current idea is to create two regular int arrays, then iterate over the entire vector and copy n/2 elements to each of the arrays. What I would like to know: What is the most elegant way of accomplishing my task? I have a feeling that I can do this without iterating over the

Read 32-bit integer from binary file in C++?

狂风中的少年 提交于 2019-12-03 11:40:40
My binary file looks like this. 00000000: 0000 0803 0000 ea60 0000 001c 0000 001c 00000010: 0000 0000 0000 0000 0000 0000 0000 0000 left column is address. I just tried to read 0000 0803 (=2051) as follows ifstream if; if.open("file"); uint32_t a; if >> a; As expected...It did not work :-( a was just 0 after execution. I tried long, int, unsigned int, unsigned long . All failed. Why these are not working and how can I achieve the goal? You have two issues: Insuring you read the bytes you intend (no fewer, no more) from the stream. I'd recommend this syntax: uint32_t a; inFILE.read(reinterpret

Why does std::fstream set the EOF bit the way it does?

匿名 (未验证) 提交于 2019-12-03 10:24:21
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I recently ran into a problem caused by using fstream::eof(). I read the following line from here : The function eof() returns true if the end of the associated input file has been reached, false otherwise. and (mistakenly) assumed this meant that if I used fstream::read() and read past the end of the file, the function eof() would tell me. So I did something like this (very generalized): for(int i = 0; i The problem came because of what is explained later on the page linked above (which I failed to read initially, thanks to the misleading

Copy data from fstream to stringstream with no buffer?

血红的双手。 提交于 2019-12-03 09:27:02
问题 Is there anyway I can transfer data from an fstream (a file) to a stringstream (a stream in the memory)? Currently, I'm using a buffer, but this requires double the memory, because you need to copy the data to a buffer, then copy the buffer to the stringstream, and until you delete the buffer, the data is duplicated in the memory. std::fstream fWrite(fName,std::ios::binary | std::ios::in | std::ios::out); fWrite.seekg(0,std::ios::end); //Seek to the end int fLen = fWrite.tellg(); //Get length

Undefined symbols for architecture x86_64 c++ on Mac

匿名 (未验证) 提交于 2019-12-03 08:42:37
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Hi I have wrote a program in C++ in Netbeans on Macintosh that uses these these includes - iostream,fstream, string & cstdlib. The program compiles fine in Netbeans but I need to compile and run it with gcc because I need to pass an Image File to the program in terminal. When I try to compile I get the error below and to be honest I am completely lost as to what is going on. It seems there is an issue with the namespace? Anyone able to help? Thanks! Macintosh-2:ImageTool root$ gcc main.cpp -o main.out Undefined symbols for architecture x86

How to get the line number from a file in C++?

白昼怎懂夜的黑 提交于 2019-12-03 07:28:23
问题 What would be the best way to get the line number of the current line in a file that I have opened with a ifstream ? So I am reading in the data and I need to store the line number that it is on so that I can display it later if the data doesn't match the specifications. 回答1: If you don't want to limit yourself to std::getline , then you could use class derived from std::streambuf , and which keeps track of the current line number: class CountingStreamBuffer : public std::streambuf { /* see

How to use fgets if you don't know the number of characters to be read?

99封情书 提交于 2019-12-03 06:57:38
I need to read a file and send the text from it to a string so I can parse it. However, the program won't know exactly how long the file is, so what would I do if I wanted to use fgets() , or is there a better alternative? Note: char *fgets(char *str, size_t num, FILE *stream); Don't forget that fgets() reads a line at a time, subject to having enough space. Humans seldom write lines longer than ... 80, 256, pick a number ... characters. POSIX suggests a line length of 4096. So, I usually use: char buffer[4096]; while (fgets(buffer, sizeof(buffer), fp)) { ...process line... } If you are