fstream

C++ ofstream not writing to output file?

≡放荡痞女 提交于 2019-12-25 17:17:08
问题 The code is supposed to count the number of a, b, c, d, e, and f characters in the input text file and print the output into a second text file. When I run the code, it creates the output file but doesn't write anything into it. #include<iostream> #include<fstream> #include<cmath> using namespace std; int main(){ // establish counters for the number of each character char x; int acount=0; int bcount=0; int ccount=0; int dcount=0; int ecount=0; int fcount=0; ifstream iFile("plato.txt"); /

Why does this file stream, with no error flags set, fail to read unless I do tellg()?

半世苍凉 提交于 2019-12-25 09:09:58
问题 I have a fstream that seems to get into a phantom failure state, even though examining it (via conversion to bool ) reveals no error flags. Subsequent reads fail, which is unexpected. #include <fstream> #include <iostream> #include <cassert> int main() { const unsigned int SAMPLE_COUNT = 2; // Setup - create file with two "samples"; each sample has a double and a float { std::ofstream ofs("/tmp/ffs", std::ios::binary); const double colA[SAMPLE_COUNT] = { 1.0, 2.0 }; const float colB[SAMPLE

Writing a backspace in a file

那年仲夏 提交于 2019-12-25 09:02:56
问题 int main(){ std::cout << "Insert file name / or path. \n NOTE: ONLY INPUTS. DELETES PREVIOUS DATA.\nV.6" << std::endl; std::string filen; std::cin >> filen; std::ofstream myFile; try{ myFile.open(filen, std::ios::out); } catch(std::fstream::failure){ std::cout << "Could not open file!\n Make sure the name and data type are valid."; system("pause"); } while(true){ int press = getch(); if(press == 43) myFile.close(); if(press == 8){myFile << "\b" << " " << "\b";std::cout << "\b" << " " << "\b"

Displaying data from 2d array and stringstream

こ雲淡風輕ζ 提交于 2019-12-25 03:40:08
问题 I'm writing a program that reads data from a file that looks like: W Z W Y W Y W W Y W Y Z W Z Y Z W W W Y W Y W Z Y W Z W Y Y Z W Z Z Y Z Z W W W Y Y Y Z W Z W Z Z Y Z Z W W W Y Y Y Z W Z W Z Z Y Z Z W W W Y Y Y Z W These characters (W, Y, or Z) are parsed using a stringstream and stored in a 2D array that's 5x15. Now I'm having trouble counting the amount of times that each character appears on each line . I've tried multiple ways of achieving this by using counter variables in my function

Read in from file into structure

寵の児 提交于 2019-12-24 19:35:10
问题 I want to read in from txt file into structure using fstream. I save the data to the file in the way shown below: To read the data i tried some cheeky stuff with getlines or tabsin< struct tab{ int type,use; string name, brand; }; tab tabs[500]; ofstream tabsout; tabsout.open("tab.txt", ios::out); for (int i = 0; i < 500; i++){ if (tabs[i].use==1){ tabsout << tabs[i].type << " " << tabs[i].name << " " << tabs[i].brand << "\n"; } } tabsout.close(); //input part that fails me :( int i=0;

C++ Fstream to replace specific line?

牧云@^-^@ 提交于 2019-12-24 17:01:48
问题 okay i'm stumped on how to do this. I managed to get to the line I want to replace but i don't know how to replace it. say a file called file.txt containts this: 1 2 3 4 5 and I want to replace line 3 so that it says 4 instead of 3. How can I do this? #include <Windows.h> #include <iostream> #include <fstream> #include <string> using namespace std; fstream file; string line; int main(){ file.open("file.txt"); for(int i=0;i<2;i++){ getline(file,line); } getline(file,line); //how can i replace?

Problems with strings and fstream

Deadly 提交于 2019-12-24 10:47:51
问题 I'm currently working on a project where I need to implement several classes to simulate a cafeteria. Each "student" who is waiting on line to get their food has 5 variables which describe them, that is: name, group, entree type, snack/dessert type, and a number representing the amount of salad they plan on buying in ounces. The idea is that all this information will be read in using fstream from a text file (with the outline following a specific order and repeating for each student). Once

Can't Over write on a specific location on a file. Over write on a specific location deletes all the contents before it and moves the value after it

爷,独闯天下 提交于 2019-12-24 09:55:16
问题 In my project, there is a need to read and write to binary file, basically serializing linked lists in a file, where I store the value in binary format and remember the tellp()/tellg() offset, how ever, I cannot do that. That erases all the contents in it to zero and instead of inserting it pushes the current content back. For Example, in the below program, I open a file, write values say 1,120,323. Then close it and read it, it shows the exact correct values 1,120,323. But When I try to

fstream replace portion of a file

痴心易碎 提交于 2019-12-24 06:00:23
问题 When I do fstream someFile("something.dat", ios::binary|ios::out); someFile.seekp(someLocation, ios::beg); someFile.write(someData, 100); It seems to replace the entire file with those 100 bytes instead of replacing only the appropriate 100 bytes, as if I had specified ios::trunc. Is there a portable way to not have it truncate the file? Edit: adding ios::in seems to do the trick, by why is this required, and is that standard behavior? Edit #2: I am not trying to append to the existing file.

fstream replace portion of a file

谁都会走 提交于 2019-12-24 06:00:03
问题 When I do fstream someFile("something.dat", ios::binary|ios::out); someFile.seekp(someLocation, ios::beg); someFile.write(someData, 100); It seems to replace the entire file with those 100 bytes instead of replacing only the appropriate 100 bytes, as if I had specified ios::trunc. Is there a portable way to not have it truncate the file? Edit: adding ios::in seems to do the trick, by why is this required, and is that standard behavior? Edit #2: I am not trying to append to the existing file.