Solved

file stream in C C++ modification

Posted on 2009-07-16
7
482 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

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.

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

708 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

15 Experts available now in Live!

Get 1:1 Help Now