Error in handling stream objects


 
Thread Tools Search this Thread
Top Forums Programming Error in handling stream objects
# 1  
Old 12-04-2009
Question Error in handling stream objects

Hi,
I am trying to write a C++ code to write a file with the similar data between two input files.Input files will be command line arguments.Following is the code ,I am able to open & create files but getting errors during comparison & there is no explicit error message I am getting please let me know where I am going wrong.

Code:
#include<iostream>
       #include<fstream>
       #include<string>

       using namespace std;

       class diff {
               private:
               ifstream infile1,infile2;
              fstream samedata;
          public:
          diff (char *,char *,char *);
          void write_file ();

      };

      diff::diff (char * file1, char * file2, char *samefile) {
          infile1.open(file1,ios::binary);
          infile2.open(file2,ios::binary);
          samedata.open(samefile,ios::in | ios::out | ios::trunc);

          /*if (infile1.is_open() || infile2.is_open() || samedata.is_open())
          {
          cout << "File Opened Succesfully !!!" <<endl;
          }*/

          /*infile1.close();
          infile2.close();
          samedata.close();*/
      }


      void diff::write_file () {
          string line1,line2;

          if (infile1.is_open() && infile2.is_open())
          {
              while (!infile1.eof())
              {
              getline (infile1,line1);
              if (line1.empty())
              break;
      while (!infile2.eof())
      {
              getline (infile2,line2);
              if (line2.empty())
              break;
      if (line1.compare(line2) == 0)
       {
              samedata << line1 << "\n";
      }
      }
      infile2.clear();
      infile2.seekg(0,ios::beg);
              }
        }
        }

      int main (int argc,char *argv[])
      {
          char *samedatafile="samedata.txt";
          string line1;
          diff diffobj(argv[0],argv[1],samedatafile);
          diffobj.write_file();
          return 0;
      }

# 2  
Old 12-04-2009
Code:
// diff diffobj(argv[0],argv[1],samedatafile);
diff diffobj(argv[1],argv[2],samedatafile);

Be aware that the program exits when a blank line is read.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error handling for file

Hi Guys, I got a csv with pipe delimted file and i want to check second column of the file has any alpha character becuase I am expecting only number in that, and if any alpha characters then it should throw an error Thanks in advance (1 Reply)
Discussion started by: Rizzu155
1 Replies

2. Shell Programming and Scripting

Error handling

Hello fellow UNIX gurus :) I have a problem regarding the script below: # Variables used in this shell. power=0 # Stores squared integer total=0 # Sum of all squared integers num=0 # Stores command line arguements # Provides error handling if command line... (5 Replies)
Discussion started by: Learn4Life
5 Replies

3. Shell Programming and Scripting

Error Handling

Below code works for different databases i.e. MYSQL and ORACLE The problem is for MYSQL in Block: if ; $? taking value accordingly but in case of ORACLE $? is always taking this value as zero (0). That is the reason in Oracle it always going in else Block in any case.. :( and in case of ... (4 Replies)
Discussion started by: ambarginni
4 Replies

4. Shell Programming and Scripting

Help needed with error handling

I am writing a wrapper to one of the .ksh script. The wrapper script should capture the error/fatal(if any) and exit out of the script. Here is the script: #!/bin/sh . main_script.ksh <arg1> <arg2> > mainscript.log 2>&1 cnt=`grep 'ERROR' -c mainscript.log` if ; then echo "Error" ... (6 Replies)
Discussion started by: stunnerz_84
6 Replies

5. Shell Programming and Scripting

Help with Error Handling on Script

Hi, I need your guys help again. I run a script which check for some process status in a loop. when i check the process some of the process could throw an error, how can i check that inside my script. Thanks, RR (3 Replies)
Discussion started by: rrb2009
3 Replies

6. Shell Programming and Scripting

[Video stream] network stream recording with mplayer

Hi I used this command: mplayer http://host/axis-cgi/mjpg/video.cgi -user root -passwd root \ -cache 1024 -fps 25.0 -nosound -vc ffh264 \ -demuxer 3 -dumpstream -dumpfile output.avi It's ok but... Video Playing is very fast! Why? Is it a synch problem? What parameter I have to use for... (1 Reply)
Discussion started by: takeo.kikuta
1 Replies

7. Programming

Assign NULL to stream objects

Hi, I am trying to initialize fstream objects to NULL but its throwing an error.Could somebody tell me better way to do this. I have code in C++ & compiling it using g++ compiler on a Linux box. ifstream file; file=NULL; error omega.cpp:38: error: no match for 'operator='... (4 Replies)
Discussion started by: forstudy3
4 Replies

8. Shell Programming and Scripting

Error Handling

Helo Experts, I need a help in handling errors in shell script, wants my errors displayed in text file instead of command window.. My shell script is here; cd /cygdrive/s/Files for FILES in ./*.* do temp=`basename $FILES` if cp $FILES /cygdrive/r/CopyFile1/$FILES; then echo "copy... (5 Replies)
Discussion started by: CelvinSaran
5 Replies

9. Solaris

Pla help - urgent-netstat error--- cant open mib stream

Hi, When i type netstat command as normal user it shows following error $ netstat arp open: Permission denied can't open mib stream I can execute the command as root user.. Pls reply at the earliest (1 Reply)
Discussion started by: vadivukumar
1 Replies

10. Shell Programming and Scripting

Handling ftp error

I have a script which connects to remote server and ftp the files It works fine, however if there is any failure in ftp connection can it be handled??? ftp log ftp session start time is: Thu Jun 19 00:00:02 BST 2008 Not connected. Not connected. Interactive mode off. Not connected.... (1 Reply)
Discussion started by: vivek_damodaran
1 Replies
Login or Register to Ask a Question