Writing to an output file using fstream in C++ -
assuming have proper headers , using namespace std; correct in terms of using fstream output string output file? included relevant file api parts of code entirety of long. i've included iostream , fstream headers. seem particular errors, way im using myfile class or object.
#include <curses.h> #include <math.h> #include "fmttime.h" #include <sys/time.h> #include <time.h> #include <unistd.h> #include <stdio.h> // needed file api #include <iostream> // file api headers c++ #include <fstream> using namespace std; // enumeration list colors of grid, title, sin , cos waves enum colors { gridcolor = 1, titlecolor, sincolor, coscolor, }; void sim_advance(int degrees); // function prototype phase angle double sim_cos(); // function calculates cos double sim_sin(); // function calculates sin char* usage(); // function returns error mssg static const double pi = (m_pi/180); // degrees radian factor static double pa = 0; // phase angle static double x; // x dimension of screen static double y; // y dimension of screen static int delay1 = 300000; // 300ms delay static int currenty = 0; // y index static const int buffsize = 25; // size of character buffer time // function prototype frame of plot function void frame(const char* title, int gridcolor, int labelcolor); // function prototype symbols form plot void mark(window* window, char ch, int color, double value); int main( int argc, // number of command line arguements char* argv[] // string of each command line ) { initscr(); // curses.h initilizations cbreak(); nodelay(stdscr, true); noecho(); // supress character inputs on screen start_color(); // enable bg/fg colors // color initializations enumeration list init_pair(gridcolor, color_red, color_black); init_pair(titlecolor, color_green, color_black); init_pair(sincolor, color_yellow, color_black); init_pair(coscolor, color_magenta, color_black); int keyhit; // holder getch command int ctr = 1; // exit flag int degrees = 10; // interval added phase int enablelog = 0; // flag logging enable int disabletrunc = 0; // flag logging without truncation char* outputname = null; // name of output program file* plog; // file pointer o/p write getmaxyx(stdscr,y,x); // find max x , y values of stdscrn // defining new window in terminal printing plot window* window = newwin(y-4, x, 2, 0); x = x - 2 - buffsize; // move window allow timestamp scrollok(window, true); // enable scrolling window // title string plotter char ctitle[] = {"real time sine/ cosine plot"}; // api code file output ofstream myfile (plog); // int = 1; // index how many times getopt needs // called. starts @ 1 offset // program call string, without options while (i < argc) { switch (getopt (argc, argv, "ao:")) { case 'a': disabletrunc = 1; break; case 'o': enablelog = 1; outputname = optarg; // gets name of textfile // open file designated user // , assign file pointer plog // following if else statement opens file // logging in 2 distinct modes, extended , truncation if (disabletrunc == 1) { plog = myfile.open (outputname, ios::out | ios::app); } else { // print truncation plog = myfile.open (outputname, ios::out | ios::trunc); } break; // case of error, print usage message case '?': endwin(); puts(usage()); return 0; break; // no more options on command line case -1: = argc; break; } ++i; } // if '-a' enabled still error case // if statement handles case if (disabletrunc == 1 && enablelog == 0) { endwin(); puts("\nwarning: -a must used in conjuction -o filename"); puts(usage()); return 0; } while (ctr == 1) // run program till { // exit detected (ctrl-x) usleep(300000); // delays program execution keyhit = getch(); frame(ctitle, gridcolor, titlecolor); // prints out frame once struct timeval tv; char buf[buffsize]; // buffer being sent formattime gettimeofday(&tv, null); // calling function epoch time formattime(&tv, buf, buffsize); // calling formatime timestamp wattrset(window, color_pair(gridcolor)); wprintw(window,"%s", buf); wrefresh(window); mark(window,'|', gridcolor, 0); mark(window,'c', coscolor, sim_cos()); mark(window,'s', sincolor, sim_sin()); // scroll next y coordinate wmove(window, currenty, buffsize + x); wrefresh(window); wprintw(window, "\n"); wrefresh(window); currenty = getcury(window); // print desired data output file if (enablelog == 1) myfile << buf << sim_sin() << sim_cos() << endl; //fprintf(plog, "%s, %f, %f\n", buf, sim_sin(), sim_cos()); sim_advance(degrees); // advances pa "degrees" value (10) if (keyhit == 24) { ctr = 0; // exit flag set } } // close file if file exists avoid error if (enablelog == 1) { myfile.close(plog); //close(plog); } endwin(); return 0; } // function provide usage message , give user // list of acceptable option characters use program // when non valid option character used or used improperly char* usage() { // string printed error message static char errors[] = {"\nsorry invalid option entered\n\n" "please enter valid option specifier:\n" "-o filename enable logging specified " "output file: filename\n" "-a enable extended logging" " rather truncated logging\n\n" "please note: -a cannot used without -o" " filename\n"}; return errors; }
and print file can this?
myfile << buf << sim_sin() << sim_cos() << endl; myfile.close(plog);
sim_sin() , sim_cos() return double values, , buf formatted string. tried apply of resources on internet doesn't seem agree implementation (which wrong).
here errors
plot.cc: in function 'int main(int, char**)': plot.cc:93: error: no matching function call 'std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(file*&)' /usr/include/c++/4.4/fstream:623: note: candidates are: std::basic_ofstream<_chart, _traits>::basic_ofstream(const char*, std::_ios_openmode) [with _chart = char, _traits = std::char_traits<char>] /usr/include/c++/4.4/fstream:608: note: std::basic_ofstream<_chart, _traits>::basic_ofstream() [with _chart = char, _traits = std::char_traits<char>] /usr/include/c++/4.4/iosfwd:84: note: std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(const std::basic_ofstream<char, std::char_traits<char> >&) plot.cc:117: error: expected ';' before 'plog' plot.cc:124: error: void value not ignored ought plot.cc:208: error: no matching function call 'std::basic_ofstream<char, std::char_traits<char> >::close(file*&)' /usr/include/c++/4.4/fstream:736: note: candidates are: void std::basic_ofstream<_chart, _traits>::close() [with _chart = char, _traits = std::char_traits<char>]
this not correct.
for moment, i'm going ignore other issues, , only address writing itself. obvious problem writing want something separate 1 value next. can space, comma, tab, new-line, ..., pretty need something. without that, it'll impossible figure out digits belong number -- example: 1234.56789.876
so, when writing out data, want put recognizable between pieces of information:
myfile << buf << "\t" << sim_sin() << "\t" << sim_cos() << endl;
second, endl
almost never desirable. many (most?) think of adding new-line output, flushes stream. long you're writing line or 2 of data, doesn't matter, if you're writing data, can slow code down tremendously.
myfile << buf << "\t" << sim_sin() << "\t" << sim_cos() << "\n";
in relatively rare case want flush stream, i'd advise using std::flush
make intent clear.
edit: after edit, appears you're trying mix iostreams c-style file *
s (for example, attempting open stream, supplying c-style file *
parameter). i'd advise picking either c-style file *
s (if have no choice) or iostreams (if possible) , sticking 1 throughout. mixing 2 you're doing can lead not lot of confusion, can slow down code in cases (extra time keep 2 coordinated).
std::ofstream myfile("name.txt"); myfile << buf << "\t" << sim_sin() << "\t" << sim_cos() << "\n";
Comments
Post a Comment