fstream

How to edit text file data with c++

孤街浪徒 提交于 2019-12-13 07:15:51
问题 I have a program that create a text file of stock items, which contains detail of 'total production' , 'stock remaining' and so on. Now my question is how do I edit that text file with my program. For example if I mistake to enter a correct data (like production was 500 pieces but enter only 400) now how can I edit my file to make it correct without effecting other data. 回答1: You probably should not create a text file in the first place. Did you consider using sqlite (or indexed files à la

Extract and use specific data from a text file in C++

我与影子孤独终老i 提交于 2019-12-13 06:25:08
问题 Ok, I managed to extract the data I wanted from a file, but there is a problem when trying to calculate those extracted numbers (perform addition). The numbers are stored here cout << stod( line.substr( position + 1 ) ) << endl; I tried like this sum = stod( line.substr( position + 1 ) ); sum = sum * 5.0; cout << sum << endl; Since this is in a while loop, my program performes the calculation again adn again, so I get each price multiplied by five, but I only want to do addition like: sum+sum

How to write the position (x,y) of the object being tracked into text file?

偶尔善良 提交于 2019-12-13 06:09:54
问题 I am trying to record the position(x,y) of the object being tracked into text file. I am using opencv and c++ visual 2010. till now i can save a data but this data is the initial position but repeated. i want it to save the actual position at every frame. In short, how can i write the exact data writen by PutText() on the screen but to a file? see what puttext() write to the screen //write the position of the object to the screen putText(framein,"Tracking object at (" + intToString(x)+","

Read multiple values from file with fstream?

北城以北 提交于 2019-12-13 06:09:35
问题 Can I read multiple values from tab separated text file with double value1, value2, value3; ifstream in; fin.open ("myfile.dat", ifstream::in); fin >> value1 >> value2 >> value3; I get zeros in all values. 回答1: Ok, in your code there are three important mistakes: fin was not declared in this scope (you probably need to change the in at the second line to fin ) ofstream::in does not exist, you probably mean fstream::in you should also make sure that your input file exist. This can be done with

Read from a file line by line

一世执手 提交于 2019-12-13 05:19:50
问题 How can I read from a text file line by line , then use the same array to save them .. 回答1: Firstly, it seems that you are using using namespace std; in your code. This is highly discouraged. Here is how I would use std::vector . First, you have to import the <vector> header file. std::ifstream in_file("file.txt"); if (!in_file) { ... } // handle the error of file not existing std::vector<std::string> vec; std::string str; // insert each line into vec while (std::getline(in_file, str)) { vec

C++ fstream is_open() function always returns false

末鹿安然 提交于 2019-12-13 04:59:24
问题 I have tried to use open() to open html file in the same directory as the source file. However I dont know why is_open() always return false in my program.... Here is part of my source code one of the functions to open html file in readHTML.cpp #include "web.h" ... void extractAllAnchorTags(const char* webAddress, char**& anchorTags, int& numOfAnchorTags) { ifstream myfile; char line[256]; char content[2048] = ""; numOfAnchorTags = 0; myfile.open(webAddress); cout<< myfile.is_open()<<endl; if

Delete a line in an ofstream in C++

戏子无情 提交于 2019-12-13 03:48:38
问题 I want to erase lines within a file. I know you can store the content of the file (in a vector for example), erase the line and write again. However, it feels very cumbersome, and not very efficient if the file gets bigger. Anyone knows of a better, more efficient, more elegant way of doing it? 回答1: There's nothing particularly magical about disk files. They still like to store their data in contiguous areas (typically called something like "blocks"). They don't have ways of leaving data-free

Read from a text file and storing to an array C++

冷暖自知 提交于 2019-12-13 03:08:20
问题 I have a program that reads values from a text file(sortarraysin.txt) and stores those values into an array. However when I try to print the array to the console my output does not display the numbers from the text file. My text file and program are shown below. Text file: 45 59 302 48 51 3 1 23 Program: int array[8]; int i = 0; string inFileName, getcontent; cout << "Enter input file name -> "; cin >> inFileName; fstream inFileStr(inFileName.c_str(), ios::in); if(inFileStr.fail()){ cerr <<

Reading and writing to the same file using fstream

﹥>﹥吖頭↗ 提交于 2019-12-13 02:36:43
问题 void Withdraw(int index, int amount) { int Balindex = 0; fstream input("Balance.txt"); float balance = 0.0; while ((!input.eof())&&(Balindex != index)) { balance = 0.0; input >> balance; Balindex++; } input >> balance; balance = balance - amount; input << balance << endl; } i am trying to read balance from a text file, and deduct the amount withdrawn. index holds the chronological number of the balance. my file however wont overwrite the existing value with the new one. Any suggestions? 回答1:

Can we use std::fstream instead of SDL_RWops in SDL2?

自闭症网瘾萝莉.ら 提交于 2019-12-12 13:29:18
问题 As the title, does SDL_RWops have any advantages over std::fstream in dealing with I/O file? Can I use std::fstream instead because I am more familiar with it? 回答1: By reading their documentation, you can find that std::fstream is an: Input/output stream class to operate on files. On the other side, SDL_RWops is something more: SDL_RWops is an abstraction over I/O. It provides interfaces to read, write and seek data in a stream, without the caller needing to know where the data is coming from