| | |
fstream not working
Please support our C++ advertiser: Intel Parallel Studio Home
Hi, I tried to direct the output from a thread into a log file using fstream, but it dosent seem to work. If I use ostream then it works fine. Can you please let me know why fstream is not working.
Thanks in advance
Thanks in advance
#include <iostream> #include <cstdlib> #include <signal.h> #include <unistd.h> #include <pthread.h> #include <string.h> #include <fstream> #define NUM_THREADS 10 using namespace std; void *print_hello(void *threadid) { char path_file[50]; long tid; pthread_t self; self = pthread_self(); tid = (long)threadid; sprintf(path_file,"/disk2/home/arpit/myprog/my_worker_thread_%d",self); fstream logfile(path_file,std::ios::in|std::ios::out); // cout << "thread number: "<< self << endl; for(int i=0;i<20;i++) { logfile << "thread number: "<< self<<" " << "print hello " << endl; } return 0; } int main() { int i,rc; long t=0; pthread_t threads[NUM_THREADS]; int pid_a = fork(); if(pid_a == 0) { for(i = 0; i <10000; i++) { //cout << i << endl; if(i==1000) { cout << "IN WORKER ENGINE" << endl; sleep(2); pid_t id = getpid(); for(int worker = 0;worker < NUM_THREADS ; worker++) { rc = pthread_create(&threads[worker], NULL, print_hello, (void *)t); if (rc) { cout << "ERROR; return code from pthread_create() is " << rc << "for worker no :" << worker << endl; exit(-1); } } for(int worker = 0;worker < NUM_THREADS ; worker++) { pthread_join(threads[worker],NULL); } cout << "child process being killed:" <<id<< endl; if(id > 0) kill(id,9); } } } else { pid_t id1 = getpid(); cout << "IN MASTER ENGINE " << id1 << endl; sleep(2); cout << "i am in master engine" << endl; cout << "i am in master engine" << endl; } return 0; }
Similar Threads
- fstream Tutorial (C++)
- characters and double data types (C++)
- Help with fstream (C++)
- fstream.h + winsock2.h doesn't work? (C++)
- Renaming files in C++ (C++)
- fstream and struct question (C++)
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets



