writing ascii to a file

This is a discussion on writing ascii to a file within the C++ Programming forums, part of the General Programming Boards category; Hi, i'm currently working on a knn classificator and want to save the classifed probe as an ascii file (so ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    30

    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

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    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()

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    30
    is that C ? i want to do it in C++ with streams if possible

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    148
    >>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)
    ...

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    30
    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

  6. #6
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68

    basic template

    the code should look something like this

    Code:
    ofstream cFile("name of file.txt");
    
    cFile<<string<<"\n";  // \n is a return

  7. #7
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    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;
    }
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    148
    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;
    }
    Last edited by Wledge; 06-20-2003 at 05:07 AM.

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    30
    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

  10. #10
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    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;
    }
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21