ifstream

How to delete parts from a binary file in C++

南笙酒味 提交于 2019-12-04 04:59:13
问题 I would like to delete parts from a binary file, using C++. The binary file is about about 5-10 MB. What I would like to do: Search for a ANSI string "something" Once I found this string, I would like to delete the following n bytes, for example the following 1 MB of data. I would like to delete those character, not to fill them with NULL, thus make the file smaller. I would like to save the modified file into a new binary file, what is the same as the original file, except for the missing n

Cannot read simple binary integers from file? (C++)

♀尐吖头ヾ 提交于 2019-12-04 03:48:18
My code is simply as this: UPDATED : #include <iostream> #include <fstream> using namespace std; int main(int argc, char **argv) { ifstream r("foo.bin", ios::binary); ofstream w("foo.bin", ios::binary); int i; int ints[10] = {0,1,2,3,4,5,6,8,9}; w.write((char*)&ints, sizeof(ints)); int in_ints[10]; r.read((char*)&in_ints, sizeof(in_ints)); for(i = 0;i < 10;i++) { cout << in_ints[i] << " "; } cout << endl; return 0; } Now, the write portion appears to be successful, for example running the od command with 32 bit longs (my system is 32 bit) will display the correct sequence, including a hex dump

Returning ifstream in a function

你说的曾经没有我的故事 提交于 2019-12-04 02:41:34
Here's probably a very noobish question for you: How (if at all possible) can I return an ifstream from a function? Basically, I need to obtain the filename of a database from the user, and if the database with that filename does not exist, then I need to create that file for the user. I know how to do that, but only by asking the user to restart the program after creating the file. I wanted to avoid that inconvenience for the user if possible, but the function below does not compile in gcc: ifstream getFile() { string fileName; cout << "Please enter in the name of the file you'd like to open:

C++ ifstream from linux to arduino

北城余情 提交于 2019-12-04 01:54:26
问题 original code #include<iostream> #include<fstream> using namespace std; int main() { ofstream arduino_output("/dev/ttyACM0"); ifstream arduino_input("/dev/ttyACM0"); int value; string txt; while(cin >> value){ arduino_output << value << endl; arduino_input >> txt;//I never recieve the "OK" (Which I should get) cout << txt; } arduino_input.close(); arduino_output.close(); return(0); } Here is the problem: cin >> value; arduino_output << value << endl; arduino_input >> txt;//I never recieve the

What's wrong with the ifstream seekg

回眸只為那壹抹淺笑 提交于 2019-12-03 22:57:32
I am trying to do a seek and re-read the data. but the code fails. The code is std::ifstream ifs (filename.c_str(), std::ifstream::in | std::ifstream::binary); std::streampos pos = ifs.tellg(); std::cout <<" Current pos: " << pos << std::endl; // read the string std::string str; ifs >> str; std::cout << "str: " << str << std::endl; std::cout <<" Current pos: " <<ifs.tellg() << std::endl; // seek to the old position ifs.seekg(pos); std::cout <<" Current pos: " <<ifs.tellg() << std::endl; // re-read the string std::string str2; ifs >> str2; std::cout << "str2: (" << str2.size() << ") " << str2 <

Try catch exception handling C++

一笑奈何 提交于 2019-12-03 21:46:07
I have just started with exception handling in C++ using try and catch blocks. I have a text file with some data and I am reading this file using ifstream and getline as shown below, ifstream file; file.open("C:\\Test.txt", ios::in); string line; string firstLine; if (getline(file, line, ' ')) { firstLine = line; getline(file, line); } I would like to know how to implement exception handling in case file.open fails to open the specified file because it does not exist in the given path, for example there is no Test.txt in C: By default iostreams do not throw exceptions. Instead they set some

Using C++ ifstream extraction operator>> to read formatted data from a file

偶尔善良 提交于 2019-12-03 17:41:31
问题 As my learning, I am trying to use c++ ifstream and its operator>> to read data from a text file using code below. The text file outdummy.txt has following contents: just dummy Hello ofstream 555 My questions is how to read char data present in the file into a char array or string. How to do this using the ifstream::operator>> in code below. #include <iostream> #include <fstream> int main() { int a; string s; char buf[100]; ifstream in("outdummy.txt",ios_base::in); in.operator>>(a); //How to

How to open a file with relative path in C++?

≡放荡痞女 提交于 2019-12-03 14:22:44
I am writing test cases right now and I created some test files which I try to read. The absolute path is: /home/user/code/Project/source/Project/components/Project/test/file.dat but testing with an absolute path is bad for obvious reasons. So I try to convert the absolute path into a relative one and I don't know why it doesn't work. I created a file with the relative path findme.dat and I found it in /home/user/code/Project/build/source/Project/components/Project/test/findme.dat so I created the relative path /../../../../../../source/Project/components/Project/test/file.dat but the file is

ifstream, end of line and move to next line?

﹥>﹥吖頭↗ 提交于 2019-12-03 07:42:06
how do i detect and move to the next line using std::ifstream? void readData(ifstream& in) { string sz; getline(in, sz); cout << sz <<endl; int v; for(int i=0; in.good(); i++) { in >> v; if (in.good()) cout << v << " "; } in.seekg(0, ios::beg); sz.clear(); getline(in, sz); cout << sz <<endl; //no longer reads } I know good would tell me if an error happened but the stream no longer works once that happens. How can i check to see if i am at the end of line before reading another int? Use ignore() to ignore everything until the next line: in.ignore(std::numeric_limits<std::streamsize>::max(), '

Using C++ ifstream extraction operator>> to read formatted data from a file

ε祈祈猫儿з 提交于 2019-12-03 06:42:11
As my learning, I am trying to use c++ ifstream and its operator>> to read data from a text file using code below. The text file outdummy.txt has following contents: just dummy Hello ofstream 555 My questions is how to read char data present in the file into a char array or string. How to do this using the ifstream::operator>> in code below. #include <iostream> #include <fstream> int main() { int a; string s; char buf[100]; ifstream in("outdummy.txt",ios_base::in); in.operator>>(a); //How to read integer? How to read the string data.?? cout << a; in.close(); getchar(); return 0; } If you want