• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

compile but ....

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
komlaaa
Asked:
komlaaa
  • 5
  • 3
  • 2
1 Solution
 
van_dyCommented:
I will take a look at it, and will let u know in a while
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
van_dyCommented:
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
Firewall Management 201 with Professor Wool

In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!

 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
komlaaaAuthor Commented:
Hey Kent,

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

Thanks.

Komlaaa
0
 
van_dyCommented:
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
 
komlaaaAuthor Commented:
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
 
komlaaaAuthor Commented:
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
 
van_dyCommented:
yea there is a reason to it,
take a look at this
http://www.cplusplus.com/ref/iostream/
0
 
van_dyCommented:
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now