Solved

file stream in C C++ modification

Posted on 2009-07-16
7
488 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 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
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!

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
C++ mouse_event mouse look 7 112
Computer slow / BSOD 10 70
oracle 11g 23 130
How to delete files in a folder according to older file first using file operation in C 77 95
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

726 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