fstream

Read data from fstream

孤人 提交于 2019-12-20 07:22:44
问题 I'm trying to read data from a fstream but this code doesn't work. It puts 0 to the console. Can you help me? // g++ -Wall main.cpp -o main.exe #include <fstream> #include <iostream> #include <string> #include <sstream> int main() { std::fstream file("main.txt", std::fstream::in | std::fstream::out | std::fstream::app); file << "45634w6\n"; file << "dtusrjt\n"; while (!file.eof()) { std::string line; std::cout << std::getline(file, line) << "\n"; } int a; std::cin >> a; } 回答1: Just try this

invalid conversion from 'int' to 'char *'

一曲冷凌霜 提交于 2019-12-20 04:06:13
问题 I am suppose to write a program that will read from a text file and store what is inside the text file using structures and regroup and print out the information in the text file. But I have encountered problems with getline . I have try to write getline like this getline(infile, info.name) but it doesn't work. I have also include <string> and <cstring> but I still encounter errors like error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std:

ostream_iterator for Binary Output

前提是你 提交于 2019-12-20 03:13:32
问题 I want to be able to use an ostream_iterator to stream to a binary file. But the ostream_iterator uses a FormattedOuputFunction so it will write ASCII, not binary: std::ostream_iterator is a single-pass OutputIterator that writes successive objects of type T into the std::basic_ostream object for which it was constructed, using operator<< Beyond writing my own iterator is there a way to use an iterator to write binary? A simplified example of what I'm trying to do, but the copy statement is

How can I determine the current size of the file opened by std::ofstream?

不羁的心 提交于 2019-12-19 19:48:42
问题 I have a class that has a filestream of type ofstream . The constructor opens the file in append mode and all the messages always get written at the end of the file. I need to write into outputFile up to some fixed size say 1Mb, then I need to close, rename, and compress it, and then open a new file of the same name. This needs to be done when a certain size of file is reached. I tried using tellg() but after reading stuffs (and this) on internet, I understood that this is not the right

How can I determine the current size of the file opened by std::ofstream?

♀尐吖头ヾ 提交于 2019-12-19 19:48:41
问题 I have a class that has a filestream of type ofstream . The constructor opens the file in append mode and all the messages always get written at the end of the file. I need to write into outputFile up to some fixed size say 1Mb, then I need to close, rename, and compress it, and then open a new file of the same name. This needs to be done when a certain size of file is reached. I tried using tellg() but after reading stuffs (and this) on internet, I understood that this is not the right

How to use std::ifstream to read in a binary file with a wide string path

元气小坏坏 提交于 2019-12-19 05:19:18
问题 I am reading a binary file as: const size_t stBuffer = 256; char buffer[stBuffer]; std::wstring wPath(L"blah"); std::wifstream ifs(wPath.c_str(), std::wifstream::in | std::wifstream::binary) while (ifs.good()) { ifs.read(buffer, sizeof(buffer)); ... } But I am realizing this is not a true binary read. The ifstream actually reads a byte and converts it to a wide char. So if the binary file has the content 0x112233...ff , I actually read 0x110022003300...ff00 . This doesn't make much sense to

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

泄露秘密 提交于 2019-12-18 14:47:24
问题 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? 回答1: You have two issues: Insuring you read the bytes you

Why can't I read and append with std::fstream on Mac OS X?

折月煮酒 提交于 2019-12-18 07:12:36
问题 Consider the following C++ program, which takes a file and prints each line. It's a slice of a larger program where I later append to the file, based on what I see. #include <fstream> using std::fstream; #include <iostream> #include <string> using std::string; int main() { fstream file("file.txt", fstream::in | fstream::out | fstream::app); string line; while (std::getline(file, line)) std::cerr << line << std::endl; return 0; } Now apply this version of file.txt (One word on the first line,

CLion C++ can't read/open .txt file in project directory

﹥>﹥吖頭↗ 提交于 2019-12-18 03:17:15
问题 I have a .txt file in my project directory that I made and populated with data. directory structure looks like: /Users/asd/ClionProjects/ProjectWithTemplates/ main.cpp cmake twoday.txt here is my code: #include <iostream> #include <array> #include <cmath> #include <fstream> using namespace std; /* print array prototype */ template <size_t N> void printArray(const array<double , N> & arr); /* mean function prototype */ template <size_t N> double meanArray(const array<double , N> & arr); /*

How to clear the content of a file after it is opened using fstream? [duplicate]

强颜欢笑 提交于 2019-12-17 21:32:24
问题 This question already has answers here : How to truncate a file while it is open with fstream (3 answers) Closed 2 years ago . I need to open a file in read/write mode, read its content and then clear all. So, I cannot open it in truncate mode. How can I do that? 回答1: Hate to disappoint you, but.. There's no standard way of clearing the contents of a file from an open std::fstream , the straight forward way is therefore to handle the two operations as what they really are.. two operations.