fstream

ifstream and ofstream or fstream using in and out

痴心易碎 提交于 2019-12-12 08:29:56
问题 When dealing with files, which of the two examples below is preferred? Does one provide better performance than the other? Is there any difference at all? ifstream input("input_file.txt"); ofstream output("output_file.txt"); vs fstream input("input_file.txt",istream::in); fstream output("output_file.txt",ostream::out); 回答1: Performance-wise, there are probably only negligible differences in this case. At best you're saving a little memory. What matters is that the first case helps with the

Low level disk operations in Linux for C++

送分小仙女□ 提交于 2019-12-12 08:27:54
问题 What kind of methods exists in linux for low level disk operations in C++? I am attempting to write my own manager of data on a disk. For example, I would like to create a C++ program in the Linux environment that allocated a certain amount (continuous) on a disk and then freely allows me to read/write to that chunk of data. I don't think I want to use the standard fstream::open because then the file is managed by the OS and I might not get a continuous section on the disk. Thanks. 回答1:

Writing a class object into a file using fstream and then read it

醉酒当歌 提交于 2019-12-12 07:24:33
问题 I want to make a class of a student and take 3 inputs information and make an output of this file. How to this? This is my try: #include <iostream> using namespace std; class Student{ private: char name[50]; char id[50]; int age; public: void getdata() { //take name as input //take id as input //take age as input } void showdata() { //display stored file } } int main() { Student s1; ofstream s1("student.txt"); //i want to store that 's1' object //anything else return 0; } 回答1: C++ uses the

C++ Read/Write to textfile issue

£可爱£侵袭症+ 提交于 2019-12-12 06:00:10
问题 I need to write numbers and text in a text file, for an unknown reason, the writing works, the reading does not, the problem is reading the text as in the following example: fstream ff,ff2; int a,b; ff.open("simtestagain.txt",ios::out); CString mycstring = _T("Name with spaces"); char mycharbuffer[255]; //destination buffer size_t convertedChars = 0; //number of characters converted wcstombs_s( &convertedChars, mycharbuffer, mycstring.GetLength()+1,mycstring.GetBuffer(), _TRUNCATE); ff << 1 <

Reading multiple data types from input file

倾然丶 夕夏残阳落幕 提交于 2019-12-12 02:56:28
问题 I have 2 questions: I am trying to take input from another file, initialize them into variables, then output the variables to another file. I am able to get all input into separate variables except for 1 line (the line should be a variable). My input file contains this data: 557.9012043 0.673621489 7210984732 1.891092837 238 a 789.234 b Yes Please cannot wait for hawaii The line "Yes Please" should be taken as a whole string, however, "Yes" and "Please" are getting separated. My code for this

Convert VERY large ppm files to JPEG/JPG/PNG?

为君一笑 提交于 2019-12-12 02:54:30
问题 So I wrote a C++ program that produces very high resolution pictures (fractals). I use fstream to save all the data in a .ppm file. Everything works fine, but when I go into really high resolution (38400x21600) the ppm file has ~8 Gigabytes. With my 16 Gigabytes of Ram, however, I am still not able to convert that picture. I downloaded couple of converters, but they couldn't handle it. Even Gimp crashed when I try to "export as...". So, does anyone know a good converter that can handle really

Why fstream::tellg() return value is enlarged by the number of newlines in the input text file, when file is formated for Windows (\r\n)?

霸气de小男生 提交于 2019-12-12 01:59:35
问题 Program openes input file and prints current reading/writing position several times. If file is formated with '\n' for newline, values are as expected: 0, 1, 2, 3. On the other side, if the newline is '\r\n' it appears that after some reading, current position returned by all tellg() calls are offsetted by the number of newlines in the file - output is: 0, 5, 6, 7. All returned values are increased by 4, which is a number of newlines in example input file. #include <fstream> #include

Reading in lines from a text file in reverse order c++

*爱你&永不变心* 提交于 2019-12-12 01:53:14
问题 I need to find a way of reading in the last 6 lines of data from a file. For example if I have 1 2 3 4 5 6 7 8 9 10 I need to read be able to get 10, 9, 8, 7, 6, 5, 4. These need to then be put into variables or strings to be outputted later. Currently I have managed to read in the last line of the file but I have no idea how to then read in the other 5 numbers. #include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; int main() { std::ifstream in("test

C++: Mock Catering Company Billing Program - Not able to ouput bad data to error file

試著忘記壹切 提交于 2019-12-12 01:14:59
问题 This program is basically supposed to read data from a file, and then process that data depending on what it is. It's sort of a mock catering company, and the variables are number of adults, number of children, type of meal (deluxe or standard), type of day (weekend [Yes or No], initial deposit, etc. and the surcharge, tax, total, etc. are calculated in the CalcData function depending on what that data is (i.e. If it is a deluxe meal (D or S), the price is $25.80, instead of $21.75 (for

Incorrect data input with fstream

痞子三分冷 提交于 2019-12-12 01:14:15
问题 I tried to read in data from a text file using fstream but got wrong data. ifstream fin ("C:\\Users\\rEgonicS\\Documents\\test.in"); int number; fin >> number; cout << number; test.in is simply 12 . cout reads 4273190 . Can someone explain why this is so and how to fix it? 回答1: The most likely cause is that the file open failed. Check the status after opening, and also after reading; for a simple test, do something like this: ifstream fin ("C:\\Users\\rEgonicS\\Documents\\test.in"); if (!fin)