Solved

file stream in C C++ modification

Posted on 2009-07-16
7
487 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
change colour of repeater control in asp.net c# 7 113
eclipse compiler vs Installed JREs option 3 105
Super Scope, DHCP 5 91
Error creating a new C++ project in ,net 20 41
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

860 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