Solved

compile but ....

Posted on 2004-10-25
322 Views
Last Modified: 2010-04-15
Thanks you Van_dy and u all.

I finally compile this program but when i run
./executable inputFile name   OR
cat inputFile | ./executable

the file fails to open.  Van_dy, i send u the th program, please take a look.
Here is my sample run:
=================== ERROR ==================
cs450/a4_1> ls
BUG_LOG  Makefile  TIME_LOG    error     getopt.o  main.cppn  wc.cpp  wc.o
MANPAGE  README    cs450words  getopt.c  main.cpp  main.o     wc.h
cs450/a4_1> cat Makefile

all: cs450words

cs450words:  wc.o main.o getopt.o
        g++ main.o wc.o getopt.o -o cs450words

wc.o: wc.cpp wc.h
        g++ -g -Wall -ansi -c wc.cpp

main.o: main.cpp
        g++ -g -Wall -ansi -c main.cpp

getop.o: getopt.c
        cc -c getopt.c

clean:
        /bin/rm -f cs450words main.o wc.o getopt.o *~

cs450/a4_1> uname -rs
Linux 2.4.27
cs450/a4_1> ls
BUG_LOG  Makefile  TIME_LOG    error     getopt.o  main.cppn  wc.cpp  wc.o
MANPAGE  README    cs450words  getopt.c  main.cpp  main.o     wc.h
cs450/a4_1> cat Makefile

all: cs450words

cs450words:  wc.o main.o getopt.o
        g++ main.o wc.o getopt.o -o cs450words

wc.o: wc.cpp wc.h
        g++ -g -Wall -ansi -c wc.cpp

main.o: main.cpp
        g++ -g -Wall -ansi -c main.cpp

getop.o: getopt.c
        cc -c getopt.c

clean:
        /bin/rm -f cs450words main.o wc.o getopt.o *~

cs450/a4_1> uname -rs
Linux 2.4.27
cs450/a4_1>cat MANPAGE | ./cs450words
No input file provided
cs450/a4_1>



0
Question by:komlaaa
    10 Comments
     
    LVL 5

    Expert Comment

    by:van_dy
    I will take a look at it, and will let u know in a while
    0
     
    LVL 45

    Expert Comment

    by:Kdo
    Hi komlaaa,

    In general, if you'll provide the problem code then all of the experts here will be able to jump in and help.  This will usually get you answers more quickly than addressing queries to an individual.

    Offhand, I'd suggest that you're not reading from stdin.  You can do this a couple of ways.

    Whenever you issue a stream input function like fscanf(), fgets, etc, make sure that the target stream is stdin.

    If you've already got another stream name embedded throughout your program because you tried to open the file, simply replace the fopen with this statement:

      SomeStream = stdin;   /*  Assumes that you have a FILE *SomeStream; declaration in the program  */


    Good Luck,
    Kent
    0
     
    LVL 5

    Accepted Solution

    by:
    Hi komlaaa,

            sorry for the delay. here are a few suggestions:
    if you wants your executable cs450words
     to read from STDIN, you will have to use function overloading.
    see the difference between the following situations:

    cs450/a4_1>cat MANPAGE | ./cs450words        // you are reading the input from istream
    cs450/a4_1>./cs450words file                              // you are using ifstream here

    so you will have to define two functions for wc::read() with following prototypes:
    void wc::read(std::ifstream &f)
    &
    void wc::read(std::stream &f)

    in your main.cpp, you will have to make following changes:

    int main( int argc, char * const argv[])
    {
      wc w;

      bool checkL = false;
      bool checkT = false;
      bool checkD = false;
      bool checkC = false;
      bool noArgument = false;

     
      char * inputFile = NULL;
      //std::ifstream f(inputFile, std::ios::in);
      int ch;


      //  char options[] = ":ltdc";
    //      ifstream file;
      while ((ch = getopt(argc, argv,":ltdc")) != -1) {
        switch (ch) {
        case 'l':
          checkL = true;
          break;
        case 't':
          checkT = true;
          break;
        case 'd':
          checkD = true;
          break;
        case 'c':
          checkC = true;
          break;
        case '?':
        default:
          std::cout<<"Invalid option specified: "<< optopt<<"\n";
          w.helpMenu();
          exit(1);
          break;
        }

      }

      if(optind == 1){
        noArgument = true;
      }

      if (srgv[optind]){
            std::ifstream f(argv[optind]);
              if(!f){
                      std::cerr << unable to open file << argv[optind] << '\n';
                       exit();
              }
            w.read(f);
               f.close();
        } else  {
              w.read(std::cin);
        }

     if( true == checkT ){
     std::cout<<"TOTAL: "<<w.getTotalCount()<<"\n";
    }

     if( true == checkD ){
     std::cout<<"DISTINCT: " <<w.getDistinctCount()<<"\n";
     }
    ........
     
    now in wc.cpp, you will have to define two functions with prototypes:
    void wc::read(std::ifstream &f)
    &
    void wc::read(std::stream &f)

    the correct function will be invoked depending upon the arguements supplied.
    recode wc.cpp to include the second wc::read() and lets know the results

    hope this helps,
    van_dy
    0
     
    LVL 45

    Expert Comment

    by:Kdo
    Hi komalaaa,

    As I mentioned earlier, please post the problem here.  With rare exception, it is against the rules to take these discussions offline as this defeats the purpose of the Exchange.  If a problem is worth solving, it is probably of interest to others, so taking the discussion offline deprives others of the opportunity to learn and reduces EE to a signup sheet for private tutoring.

    Also, your problem is obviously C++.  This question should be in another forum.

    Kent
    0
     

    Author Comment

    by:komlaaa
    Hey Kent,

    Sorry if that is what my method for getting helped out imply.
    I will conider your concern in future.

    Thanks.

    Komlaaa
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    LOL, i think i need to add to this.

    komlaaa: kent is very right regarding the purpose of exchange. The problem should be brought
                    forth for all the experts to look at so that every one can give it a shot. you should post
                     your source files here itself.

    kent:       actually the problem has been open for quite a while, starting with this post:
      http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21173430.html
    the discussion above apparently failed to solve the problem for him because the same problem came
    up again later  in this post -> http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21175106.html
    komlaaa was having issues with linking properly to libc on his solaris machine from a c++ program
    which happened to use getopt(). you can sure follow the post to see how did it eventually get resolved.
    from then onwards, i have basically helped in putting together various translation units in a makefile
    to produce an executable which actually is not much work if you have all the source files with you. komlaa
    sent me his sources since none of the experts were involved in the discussion at that time.The
    present question is definitely c++ not c, however since i already knew the nature of the problem, it would
    be apt for me to help if i could, and that is what i am doing.

    peace.
    0
     

    Author Comment

    by:komlaaa
    i would like to reiterate to kent i am currently on C forum because of the problem i was with getopt.c(like Vand_dy mention)
    that i need to link to C++ program. However, i agree that if i posted a problem not an individual, i will be more helped out quickly. i will consider this in future.

    Thanks u all and peace!!!

    komlaaa
    0
     

    Author Comment

    by:komlaaa
    I am surprised i did not have to implement the bothe version of read but only istream one to get it working in both way. i left the main as u recommended.
    Any Comment.

    Thanks,

    komlaaa
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    yea there is a reason to it,
    take a look at this
    http://www.cplusplus.com/ref/iostream/
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    i havent actually done this, but i guess you can do it just for istream and use the same function for cin,  ifstream, istringstream
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Title # Comments Views Activity
    Why this code doesn't work? 8 86
    How to design, and implement simple interface 5 114
    why "." vs "->" 23 104
    how to understand recursion 12 180
    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now