fstream

how can I read exactly 128 bytes from an fstream into a string object? [duplicate]

对着背影说爱祢 提交于 2019-12-09 10:23:23
问题 This question already has answers here : Reading directly from an std::istream into an std::string (6 answers) Closed 3 years ago . How do I read exactly 128 bytes from an fstream into a string object? I wrote some code to read the first 128 bytes of a file and print it and then the last 128 bytes of the file and print that. The last part works, since you can easily iterate to EOF, but how do I get exactly 128 bytes from the front? The code below doesn't work since you can't add 128 to an

C++: Everytime I read in by fstream I got 1 extra character at the end

你离开我真会死。 提交于 2019-12-09 05:29:56
问题 Everytime I read in by fstream I got 1 extra character at the end, How can I avoid this? EDIT: ifstream readfile(inputFile); ofstream writefile(outputFile); char c; while(!readfile.eof()){ readfile >> c; //c = shiftChar(c, RIGHT, shift); writefile << c; } readfile.close(); writefile.close(); 回答1: This typically results from testing for the end of file incorrectly. You normally want to do something like: while (infile>>variable) ... or: while (std::getline(infile, whatever)) ... but NOT: while

C++ fstream function that reads a line without extracting?

六月ゝ 毕业季﹏ 提交于 2019-12-08 15:40:16
问题 In C++, is there a function in the fstream library (or any library) that allows me to read a line to a delimiter of '\n' without extracting? I know the peek() function allows the program to 'peek' at the next character its reading in without extracting but I need a peek() like function that does that but for a whole line. 回答1: You can do this with a combination of getline , tellg and seekg . #include <fstream> #include <iostream> #include <ios> int main () { std::fstream fs(__FILE__); std:

How do I operate on file using fstream pointers?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 09:10:41
问题 I want to operate on streams using some abstraction and thus I want to use fstream* instead of ifstream and ofstream. I tried to do sth like that but is wil cause access violation: char* text= "test"; fstream* o = new fstream(); o = &fstream("name.txt"); o->write(text, 4); o->close(); How can I fix it, or use another idea ? I want to use pointer in this case (You can look here for more general information) How to implement my own IO file API in C++ After the changes it now looks like this:

FStream C++ automatic line addition

会有一股神秘感。 提交于 2019-12-08 07:27:40
问题 It is a pain to do an endline between each value with OFSTREAM. Here's an example of what I've got: ofstream fout("~/xample.txt"); fout << val1; fout << endl; fout << val2; I want to be able to do, instead, ofstream fout("xample.txt"); fout << val1; fout << val2; I don't care how the file is stored because I will write a configuration wizard anyways. 回答1: If fout << val1 << endl; does not work with you, you might be able to inherit ofstream and create your own stream that adds endl

Text File Binary Search

最后都变了- 提交于 2019-12-08 06:54:30
问题 I have a test file that looks like this: Ampersand Gregorina 5465874526370945 Anderson Bob 4235838387422002 Anderson Petunia 4235473838457294 Aphid Bumbellina 8392489357392473 Armstrong-Jones Mike 8238742438632892 And code that looks like this: #include <iostream> #include <string> #include <fstream> class CardSearch { protected: std::ifstream cardNumbers; public: CardSearch(std::string fileName) { cardNumbers.open(fileName, std::ios::in); if (!cardNumbers.is_open()) { std::cout << "Unable to

Assign txt file data to struct node in linked list

夙愿已清 提交于 2019-12-08 04:02:26
问题 Ok so I have never worked with fstream before or opened and read and files in a program. My instructor just gave a few lines of code that open, read, and close a text file. I'm supposed to take the data out of the text file and put it into separate nodes in a linked list and then go on to do other things with it which is not important because I know how to do it. My problem is that I don't know how to a assign these values to the struct values. The txt file looks like this: Clark Kent 55000

C++. After writing into a relative file, I cannot display the content

流过昼夜 提交于 2019-12-08 03:49:33
问题 I can write into a relative file 1 , then when I try to read the content and display it on the screen (to check if data are actually into the file), I do not have the records which I believe are already present in the file. I am using Dev-C++. Any help will be appreciated. The code is below; #include <iostream> // cin, cout #include <iomanip> #include <fstream> #include <conio.h> using namespace std; #define SIZE 10 struct client // Client record { int account; // from 1 to SIZE char name[20]

Detect newline byte from filestream

拟墨画扇 提交于 2019-12-07 21:26:23
问题 I'm trying to collect information from a textfile which contains names of organisations (without spaces) and floating integers. I want to store this information in an array structure. The problem I'm having so far is collecting the information. Here is a sample of the textfile: CBA 12.3 4.5 7.5 2.9 4.1 TLS 3.9 1 8.6 12.8 4.9 I can have up to 128 different numbers for each organisation, and up to 200 organisations in the textfile. This is what my structure looks like so far: struct callCentre

Reading and writing binary files in C++

感情迁移 提交于 2019-12-07 20:40:41
问题 I am entirely new to C++ with just a couple of hours self-teaching, that started yesterday. SO: I have a uncompressed simple beep.wav file, just about 3 seconds long with a single beeping sound in it. What I am ultimately trying to achieve is, just to read the file, and at the same time write the binary data, all including: the header , ftm and data or all that is hex readable data and dump it into a simple beep.txt file. is this possible, in C++? If so, how should I begin reading and dumping