-
writing ascii to a file
Hi,
i'm currently working on a knn classificator and want to save the classifed probe as an ascii file (so i can edit it manually for missclassified tfingertip).
This is my struct:
Code:
typedef struct{
int x;
int y;
int c;
} tfingertip;
i want to write each tfingertip as one row seperated by ;
so it looks like:
15;30;1;
44;26;3;
then i want to read it out rowwise so i can math the distance to each list element (putting the values in the list isnt the problem, the problem is the file writing/reading)
i would also like to name the filename by timestamp so while automated learning (if turned on) i can select which file to take if the knn begin to fail with misclassified tfingertips.
edit: i would also like to sort the rows by its c (fingerclass) so its easier to read for manual editing.
Thanks in advance!
~Jan
-
To put a timestamp in the filename, use the _strdate()/_strtime() functions, and sprintf() them to a var.
To read/write from/to files, use fread()/fprintf()
-
is that C ? i want to do it in C++ with streams if possible
-
>>is that C ? i want to do it in C++ with streams if possible
Writing to stream
Code:
#include <fstream>
#include <list>
using namespace std;
struct fingertip {
int x;
int y;
int c;
};
ostream& operator<<(ostream& os,const fingertip& ft)
{
os << ft.x << ';' << ft.y << ';' << ft.c << '\n';
return os;
}
...
ofstream fileOut("ft.txt");
list<fingertip> lft;
fingertip tf = {1,2,3};
lft.push_back(tf);
lft.push_back(tf);
typedef list<fingertip>::const_iterator ftiter;
for(ftiter i = lft.begin();i != lft.end();++i)
fileOut << *i;
For reading implement the istream operator>> .
Code:
istream& operator>>(istream& is,fingertip& ft)
...
-
the istream is the problem i have. I don't know how to divide the string x;y;c so i have again ft.x ft.y ft.c
Thanks in advance!
~Jan
-
basic template
the code should look something like this
Code:
ofstream cFile("name of file.txt");
cFile<<string<<"\n"; // \n is a return
-
Not the most eloquent code, but it ought to work:
Code:
fingertip& operator>>(istream& is, fingertip& ft)
{
string line;
int first, last;
getline(is, line);
first = line.find_first_of(';');
last = line.find_last_of(';');
ft.x = atoi(line.substr(0, first).c_str());
ft.y = atoi(line.substr(first + 1, last - (first + 1)).c_str());
ft.c = atoi(line.substr(last + 1, line.size() - (last + 1)).c_str());
cout << ft.x << endl;
cout << ft.y << endl;
cout << ft.c << endl;
return ft;
}
-
Works for input like 1;2;3
Code:
istream& operator>>(istream& is,fingertip& ft)
{
fingertip tempft;
char c = 0;
if(is.fail())
return is;
is >> tempft.x >> c;
if(is.fail() || c != ';')
throw invalid_argument("Error description...");
is >> tempft.y >> c;
if(is.fail() || c != ';')
throw invalid_argument("Error description...");
is >> tempft.c;
if(is.fail())
throw invalid_argument("Error description...");
ft = tempft;
return is;
}
-
this is giving me a headache:
Code:
int count(char filen) {
ifstream filena(filen);
while (filena.getline( line, 6))
{
num++;
};
return num;
}
Compiler:
knnprobe.cc: In function `int count(char)':
knnprobe.cc:145: error: invalid conversion from `char' to `const char*'
knnprobe.cc:145: error: initializing argument 1 of `
std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*,
std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]'
knnprobe.cc:297:2: warning: no newline at end of file
thanks in advance!
~Jan
-
The ifstream constructor requires a char pointer, not a char. So does the getline function in ifstream.
Code:
int count(string filen) {
ifstream filena(filen.c_str());
while (getline(filena, line, 6))
{
num++;
} //; not nescessary
return num;
}