fstream

fstream get(char*, int) how to operate empty line?

早过忘川 提交于 2019-12-24 03:01:17
问题 code in strfile.cpp: #include <fstream> #include <iostream> #include <assert.h> #define SZ 100 using namespace std; int main(){ char buf[SZ]; { ifstream in("strfile.cpp"); assert(in); ofstream out("strfile.out"); assert(out); int i = 1; while(!in.eof()){ if(in.get(buf, SZ)) int a = in.get(); else{ cout << buf << endl; out << i++ << ": " << buf << endl; continue; } cout << buf << endl; out << i++ << ": " << buf << endl; } } return 0; } I want to operate all file but in strfile.out: 1: #include

Why doesn't creating/writing to file with fstream work on windows start up?

左心房为你撑大大i 提交于 2019-12-24 02:28:07
问题 So here I got this little program which everyone obviously understands. include <iostream> include <fstream> using namespace std; int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); return 0; } This program works just fine, however, the problem is that if I have added it to run on windows start up(in registries SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run ), the programs starts fine, but it does not create example.txt file.

How to edit a row in a text file with C++? [duplicate]

人盡茶涼 提交于 2019-12-24 02:16:30
问题 This question already has answers here : Edit a specific row in a file (5 answers) Closed 3 years ago . I have a txt file like this: "shoes":12 "pants":33 "jacket":26 "glasses":16 "t-shirt":182 I need to replace the number of jacket ( from 26 to 42 for example ). So, I have wrote this code, but I don't know how to edit a specific row where there is the word "jacket": #include <iostream> #include <fstream> using namespace std; int main() { ifstream f("file.txt"); string s; if(!f) { cout< <

How to collect and store tellp(), tellg() return types?

折月煮酒 提交于 2019-12-24 02:16:07
问题 I am writing a program that maintains a linked_list in a file. So I traverse across the file, by using tellp()/tellg() and adding it to a particular long integer(can be seen as an offset) to get to the new location. An simple example would be long next_offset = sizeof(long) + sizeof(int) .... //like size of all the elements in the record, etc curr_node = out.seekg(); while(curr_node != -1) { out.read(...); **curr_node.seekg(curr_node.tellp() + next_offset);** out.read((char *)&curr_node

C++, read and write to a binary file at the same time

半世苍凉 提交于 2019-12-24 01:39:08
问题 I wanted to know if it's possible to read a byte from a binary file with "fstream" and then change that byte and write it back. I tried this code but it didn't work and nothing happens but I'm sure it reads correctly. file.open(path, ios::in|ios::out|ios::binary|ios::ate); file.seekg(0, ios::end); int size=file.tellg(); file.seekg(0,ios::beg); char buffer; for(int i=0;i<size;i++) { file.read((char*)&buffer,sizeof(char)); buffer=(buffer+7)%256; file.write((char*)&buffer, sizeof(char)); }

How to read the whole lines from a file (with spaces)?

删除回忆录丶 提交于 2019-12-23 20:08:11
问题 I am using STL. I need to read lines from a text file. How to read lines till the first \n but not till the first ' ' (space)? For example, my text file contains: Hello world Hey there If I write like this: ifstream file("FileWithGreetings.txt"); string str(""); file >> str; then str will contain only "Hello" but I need "Hello world" (till the first \n ). I thought I could use the method getline() but it demands to specify the number of symbols to be read. In my case, I do not know how many

Why does calling istream::tellg() affect the behavior of my program?

岁酱吖の 提交于 2019-12-23 12:29:05
问题 I am trying to convert a 24 bit bitmap image into grayscale. #include<iostream> #include<fstream> #include<conio.h> #include<stdio.h> using namespace std; class pixel{ public: unsigned char b; unsigned char g; unsigned char r; void display() { cout<<r<<" "<<g<<" "<<b<<" "; } }p1; using namespace std; int main(){ unsigned char avg; fstream file("image.bmp",ios::binary|ios::in|ios::out); int start; file.seekg(10); file.read((char*)&start,4); file.seekg(start); int i=0; while(!file.eof()){ cout<

Does OpenMP copy private objects?

别等时光非礼了梦想. 提交于 2019-12-23 12:18:49
问题 I'm writing a program that reads huge file (3x280 GB) and does a fitting procedure to the data in the file. It's pretty convenient to parallelise such a program, where this is easily done with OpenMP. The thing I don't understand is how private variables are taken in OpenMP. As we all know, fstream's obejcts are a non-copyable, and intiuitively, that prevented me from using it as a private object. So the reader of the file was shared. I got some problem later, and I thought of trying have

C++ substitution of ios::noreplace

∥☆過路亽.° 提交于 2019-12-22 18:24:59
问题 I'm using fstream to open a file for write. I don't want to overwrite an existing file so after some searching, I found ios::noreplace. But when I compile this: #include <fstream> using namespace std; //......Did something else. ofstream fout; fout.open(outputFile,ios::noreplace);//outputFile is a C string I get an error error: ‘noreplace’ is not a member of ‘std::ios’ I'm just wondering is there any std:: subsitution for ios::noreplace? 回答1: Some searching on the internet reveals that you

c++ Reading string from binary file using fstream

你说的曾经没有我的故事 提交于 2019-12-22 16:43:26
问题 I am trying to read a string from a binary file but cant seem to get it to work. I am a pretty new to c++. Can anybody help please? Thanks. string Name = "Shaun"; unsigned short int StringLength = 0; int main() { StringLength = Name.size(); ofstream oFile("File.txt", ios::binary|ios::out); oFile.write((char*)&StringLength, sizeof(unsigned short int)); oFile.write(Name.c_str(), StringLength); oFile.close(); StringLength = 0; Name = "NoName"; ifstream iFile("File.txt", ios::binary|ios::in); if(