fstream not working

Please support our C++ advertiser: Intel Parallel Studio Home
arpit.mishra arpit.mishra is offline Offline 24 Days Ago, 2:18 am |
1
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
Quick reply to this message  
C++ Syntax
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <signal.h>
  4. #include <unistd.h>
  5. #include <pthread.h>
  6. #include <string.h>
  7. #include <fstream>
  8. #define NUM_THREADS 10
  9. using namespace std;
  10.  
  11. void *print_hello(void *threadid)
  12. {
  13. char path_file[50];
  14. long tid;
  15. pthread_t self;
  16. self = pthread_self();
  17. tid = (long)threadid;
  18. sprintf(path_file,"/disk2/home/arpit/myprog/my_worker_thread_%d",self);
  19. fstream logfile(path_file,std::ios::in|std::ios::out);
  20. // cout << "thread number: "<< self << endl;
  21. for(int i=0;i<20;i++)
  22. {
  23. logfile << "thread number: "<< self<<" " << "print hello " << endl;
  24. }
  25. return 0;
  26. }
  27.  
  28. int main()
  29. {
  30. int i,rc;
  31. long t=0;
  32. pthread_t threads[NUM_THREADS];
  33. int pid_a = fork();
  34. if(pid_a == 0)
  35. {
  36. for(i = 0; i <10000; i++)
  37. {
  38. //cout << i << endl;
  39. if(i==1000)
  40. {
  41. cout << "IN WORKER ENGINE" << endl;
  42. sleep(2);
  43. pid_t id = getpid();
  44. for(int worker = 0;worker < NUM_THREADS ; worker++)
  45. {
  46. rc = pthread_create(&threads[worker], NULL, print_hello, (void *)t);
  47. if (rc)
  48. {
  49. cout << "ERROR; return code from pthread_create() is " << rc << "for worker no :" << worker << endl;
  50. exit(-1);
  51. }
  52. }
  53. for(int worker = 0;worker < NUM_THREADS ; worker++)
  54. {
  55. pthread_join(threads[worker],NULL);
  56. }
  57. cout << "child process being killed:" <<id<< endl;
  58. if(id > 0)
  59. kill(id,9);
  60. }
  61. }
  62. }
  63. else
  64. {
  65. pid_t id1 = getpid();
  66. cout << "IN MASTER ENGINE " << id1 << endl;
  67. sleep(2);
  68. cout << "i am in master engine" << endl;
  69. cout << "i am in master engine" << endl;
  70. }
  71. return 0;
  72. }

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC