Solved

file stream in C C++ modification

Posted on 2009-07-16
7
484 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
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 500 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

910 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

17 Experts available now in Live!

Get 1:1 Help Now