binary text file drama
first, i want to just know if binary files have any relevance?
second, check this out:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <string>
using namespace std;
int get_int(int default_value);
char name[20];
int main()
{
string filename;
int n;
int age;
int recsize = sizeof(name) + sizeof(int);
cout << "Enter file name: ";
getline(cin, filename);
fstream fbin;
fbin.open(filename, ios::binary | ios::in | ios::out);
if (! fbin)
{
cout << "Could not open file " << filename;
return -1;
}
cout << "Enter file record number: ";
n = get_int(0);
cout << "Enter name: ";
cin.getline(name, 19);
cout << "Enter age: ";
age = get_int(0);
fbin.seekp(n * recsize);
fbin.write(name, 20);
fbin.write(reinterpret_cast<char*>(&age), sizeof(int));
fbin.close();
return 0;
}
int get_int(int default_value)
{
char s[81];
cin.getline(s, 80);
if (strlen(s) == 0)
return default_value;
return atoi(s);
}
|
get the error when i compile in g++ on ubuntu in shell
writebin.cpp: In function ‘int main()’:
writebin.cpp:23: error: no matching function for call to ‘std::basic_fstream<char, std::char_traits<char> >::open(std::string&, std::_Ios_Openmode)’
/usr/include/c++/4.3/fstream:756: note: candidates are: void std::basic_fstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
writebin.cpp:23: error: no matching function for call to ‘std::basic_fstream<char, std::char_traits<char> >::open(std::string&, std::_Ios_Openmode)’ |
That means that there is no function that is available for the arguments that you have given. There are functions with the same name, however.
I think that the open( ) function requires a char array rather than a string. Try this instead:
fbin.open(filename.c_str( ), ios::binary | ios::in | ios::out);
Last edited on
wow super dope man....
thanks
Topic archived. No new replies allowed.