?
Solved

file stream in C C++ modification

Posted on 2009-07-16
7
Medium Priority
?
491 Views
Last Modified: 2013-11-17
Hi, i need help converting the input stream. Right now its working by running :
 ./a.out [filename]
i need this piece modified(in C++) to get a stream from a file. So i would run it like this:
cat [filename] | ./a.out
or
./a.out <[filename]

thanks!
int main(int argc, char **argv) {
    FILE *fh;
    char bf[bsize], *fmt, c;
    int max, b, i, j;
    int hist[count];
 
    for(i = 0; i < count; i++) hist[i] = 0;
 
//open file and convert
    if(argc > 1) {
      fh = fopen(argv[1],"r");
      }
 
    while( (b = fread(bf, sizeof(char), bsize, fh)) )
        for(i = 0; i < b; i++)
            if(isalpha((unsigned char)bf[i])) hist[tolower(bf[i]) - 'a']++;
    fclose(fh);//close file

Open in new window

0
Comment
Question by:Aliencenet
[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
7 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 24872047
>> i need this piece modified(in C++) to get a stream from a file. So i would run it like this:

Then you simply have to read from the standard input (stdin). That can easily be managed by removing the fopen and fclose, and replacing all 'fh' with 'stdin'.
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24872049
If you read from stdin FILE * that should work.

Instead of opening fh, just assign stdin to fh at the top of the program.

fh = stdin;


With a little work you can make your program work both ways.
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 24872052
You can do that be reading from stdin directly, i.e. assigning it to your 'FILE*'. E.g.
//open file and convert
    if(argc > 1) {
      fh = fopen(argv[1],"r");
      }
    else {
      fh = stdin;
      }

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24872064
if(argc > 1)
      fh = fopen(argv[1],"r");
else
      fh = stdin;

0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24872072
Cool I double simul-posted with 2 experts. Top form today! :)
0
 

Author Closing Comment

by:Aliencenet
ID: 31604355
thanks, even though all solutions were correct, i found yours is the most complete.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24872581
Be careful though with that solution, since you probably don't want to fclose stdin.

Was it a requirement that the original way of running the code should still be possible ? If not, there's no real reason to leave in the fopen and fclose.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

764 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