ifstream

C++ reading unsigned char from file stream

情到浓时终转凉″ 提交于 2019-12-17 19:15:27
问题 I want to read unsigned bytes from a binary file. So I wrote the following code. #include <iostream> #include <fstream> #include <vector> #include <istream> std::string filename("file"); size_t bytesAvailable = 128; size_t toRead = 128; std::basic_ifstream<unsigned char> inf(filename.c_str(), std::ios_base::in | std::ios_base::binary) ; if (inF.good()) { std::vector<unsigned char> mDataBuffer; mDataBuffer.resize(bytesAvailable) ; inF.read(&mDataBuffer[0], toRead) ; size_t counted = inF.gcount

Replace a line in text file

拟墨画扇 提交于 2019-12-17 09:59:42
问题 I want replace a line of text in a file, but I don't know a functions to this. I have this: ofstream outfile("text.txt"); ifstream infile("text.txt"); infile >> replace whit other text; Any answers for this? I miss to say, for add text in Some line in the file... Example infile.add(text, line); Does C++ have functions for this? 回答1: I'm afraid you'll probably have to rewrite the entire file. Here is how you could do it: #include <iostream> #include <fstream> using namespace std; int main() {

C++ ifstream failbit and badbit

微笑、不失礼 提交于 2019-12-17 08:55:25
问题 In case of ifstream in C++, under what conditions are failbit and badbit flags set ? 回答1: According to cplusplus.com: failbit is generally set by an input operation when the error was related to the internal logic of the operation itself, so other operations on the stream may be possible. While badbit is generally set when the error involves the loss of integrity of the stream, which is likely to persist even if a different operation is performed on the stream. badbit can be checked

文件流学习笔记

瘦欲@ 提交于 2019-12-15 20:35:33
这个年头不会个文件流还看不懂代码了,看了一遍文件流还是看不懂代码,我决定再看一遍 emmmm那么就开始我的学习笔记了 part1介绍 1.首先iostream标准库的cin和cout是从键盘上读取输入和输出的,文件流就是c++标准库的fstream库 有3个数据类型(类似于我们常说的int和char型) ofstream 可以用来新建文件然后把信息写入文件。 实现过程就是在代码里面写一个文件的定义,然后运行之后就会在相应地址生成一个文件,不用手动建立文件;同样的,假如我在我新建的文件里面写东西,我也可以用ofstram ,比如说可以用来新建查找数据结果的文件ofstream out_100(“100.txt”),out_1k(“1k.txt”),out_10k(“10k.txt”),out_100k(“100k.txt”),out_1M(“1M.txt”) ifstream 这个数据类型表示输入文件流,用于从文件读取信息,假如我要把一个文件的信息读取,比如说要读取我想查找的数据,ifstream sucess(“sucess.txt”); fstream 这个数据类型通常表示该文件流,兼备有 ofstream 和 ifstream 功能,这意味着它可以创建文件,编写文件,以及读文件。 就是个万金油 2.使用文件流的时候头文件 #include < iostream>和

Passing istream into a function

旧时模样 提交于 2019-12-14 04:25:21
问题 I am making a game-type program similar to the idea of pokemon. We have a tournament class that keeps track of several teams(its own class) which consists of pets(its own class) with different kinds of pets being subclasses of CPet. We are attempting to pass a file name into main, from main pass that file name into the Tournament class. In the Tournament class we open the file with: 14 //Construct a tournament 15 CTournament::CTournament(const char *Filename){ 16 //opening file 17 ifstream

Reading from text file or stdin

喜夏-厌秋 提交于 2019-12-14 03:47:34
问题 I have a program that basically reads a text file and counts the number of occurrences of each word on each line. Everything works properly when reading from a text file using an ifstream, however, if a file name is not entered on the command line, I need to read from stdin instead. I use the following to open and read in the file currently: map<string, map<int,int>,compare> tokens; ifstream text; string line; int count = 1; if (argc > 1){ try{ text.open(argv[1]); } catch (runtime_error& x){

C++ buffered file reading

陌路散爱 提交于 2019-12-14 03:42:30
问题 I wonder if reading a large text file line by line (e.g., std::getline or fgets) can be buffered with predefined read buffer size, or one must use special bytewise functions? I mean reading very large files with I/O operations number optimization (e.g., reading 32 MB from the HDD at a time). Of course I can handcraft buffered reading, but I thought standard file streams had that possibility. 回答1: Neither line-by-line, nor special byte-wise functions. Instead, the following should do your job:

Why doesn't uint8_t and int8_t work with file and console streams? [duplicate]

拜拜、爱过 提交于 2019-12-14 03:22:18
问题 This question already has answers here : uint8_t iostream behavior (3 answers) Closed 6 years ago . $ file testfile.txt testfile.txt: ASCII text $ cat testfile.txt aaaabbbbccddef #include <iostream> #include <fstream> #include <string> #include <cstdint> typedef uint8_t byte; // <-------- interesting typedef std::basic_ifstream<byte> FileStreamT; static const std::string FILENAME = "testfile.txt"; int main(){ FileStreamT file(FILENAME, std::ifstream::in | std::ios::binary); if(!file.is_open()

ifstream won't open file

拟墨画扇 提交于 2019-12-14 01:30:32
问题 I'm trying to open a file so I can read from it. #include <iostream> #include <fstream> #include <cstdlib> using namespace std; ifstream input_file("blah.txt", ios::in); ofstream output_file("output.txt", ios::out); Bank::Bank(void){ input_file.open("blah.txt"); if(!input_file){ cerr << "Error" << endl; exit(1); } else{ cout << "good 2 go" << endl; } } This is the code I have for reading the file named blah.txt and the output I'm getting at the terminal is the "Error". I am using Linux Mint

Reading a dataset file(Hex values) onto a block of memory-part 2

蹲街弑〆低调 提交于 2019-12-13 21:15:21
问题 I have a block of code that is trying to read the data from a dataset on to a randomly allocated block of memory. I don't know what exactly is inside the dataset but they access matrix values(Hex values) and put on to a memory location. And it works perfectly fine! const unsigned int img_size = numel_img * sizeof(float);// (1248*960)4bytes= 4.79MB for (unsigned int i=0; i<p_rctheader->glb_numImg; ++i)// 0 to 496(Total no of images) { const unsigned int cur_proj = i; // absolute projection