Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

compile but ....

Posted on 2004-10-25
10
Medium Priority
?
329 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
Comment
Question by:komlaaa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 5

Expert Comment

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

Expert Comment

by:Kent Olsen
ID: 12404098
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:
van_dy earned 1200 total points
ID: 12405302
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12406314
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
ID: 12406584
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
ID: 12407017
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
ID: 12407873
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
ID: 12408223
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
ID: 12408380
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
ID: 12408409
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

636 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