Thread: legacy code problem

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    legacy code problem

    Hi,

    I'm let say stuck with some "legacy" code that i cannot make it to work

    example:

    Code:
    
    struct noop {
        void operator()(...) const {}
    };
    
    void Sweep (const string file){
    
       int fd;
      if(file.compare("stdin") == 0)
        fd = STDIN_FILENO;
      else
        fd = open(&file[0], O_RDONLY);
      
      char buf[BUFFER_SIZE + 1];
    
      while(size_t bytes_read = read(fd, buf, BUFFER_SIZE)){ 
        cout << buff;
        // plus some additional complicated functions  that i do not wish to reimplement
       }
    
    
    }
    
    
    
    int main(int argc, char **argv){
      
      string file = SetOptions(argc,argv); // function not shown
    
      shared_ptr<istream> input;
      int entries = 0, cnt =0;
      char c, t;
    
    
      // old  code
       if ( file.compare("-") == 0 || file.compare("") == 0 ){
          Sweep("stdin");
        }else{
          Sweep(file);
        } 
    
    
        // new code
        if ( file.compare("-") == 0 || file.compare("") == 0 ){
          input.reset(&cin, noop());
        }else{
          input.reset(new ifstream(file.c_str()));
        }
    
        entries = 18;
    
        cout <<(char) (c=(*input).get()) << endl; // this prints a symbol i beliewe  is eof if stdin is chosen.
    
    
        while ((c=(*input).get())>0){
          if (c == 'L' && t == '\n')
            cnt++;
          if(cnt > entries)
            break;
          cout << c;
          t = c;
        }
    }
    Actually it works if i specify the actual file but it does not if I try to cat file directly

    Code:
    ./program -i file   <- works
    
    
    ./program < file   <- not 
    �   is what i get
    could anyone hint/provide a solution??

    thnx

  2. #2
    Registered User
    Join Date
    Dec 2014
    Posts
    143
    What do you mean by "function not shown"?

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Ten Minutes of Google implies to me to try changing "STDIN_FILENO" to "fileno(stdin)" in the setting of fd.

    No idea if it will work; but, it seems possible.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Looking over the code you likely are calling it wrong.


    Code:
    ./program -i stdin  < file
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem or compiler problem....?
    By miloki in forum C Programming
    Replies: 4
    Last Post: 03-05-2015, 12:48 AM
  2. C programming on a legacy console system.
    By A34Chris in forum C Programming
    Replies: 1
    Last Post: 04-29-2011, 01:21 PM
  3. Re-creating legacy floppy file system, doable?
    By Subsonics in forum Tech Board
    Replies: 0
    Last Post: 10-16-2009, 09:49 PM
  4. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 06:14 AM
  5. Can c legacy program call c++ library?
    By happylee in forum C Programming
    Replies: 2
    Last Post: 02-20-2002, 12:57 PM