Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

displaying piped input

Basically I am trying to get a program to redisplay the contents of the file that is piped into it, unfortunately it only accepts input up to the newline character, and I can't figure out how to get the other lines to display.

eg. when the following program is run by executing: "program < filetodisplay.txt" it will only show the first line. How do I get it to display all the lines? Thanks!
 
main () {
 char *string;
 scanf("%s", string);
 printf("%s", string);
}
0
harliquen
Asked:
harliquen
  • 4
  • 2
1 Solution
 
akshayxxCommented:
if ur whole aim is to read the input and write ir back .. then here is what u shud try..
this is ONE of the MANY methods possible

#include <stdio.h>
#define BUFSIZE 256  //any other value u comfortable with
main () {
char buf[BUFSIZE];
while(NULL!=fgets(buf,BUFSIZE,stdin)) printf("%s",buf);
}

hanoi@AKSHAY[17:50]~/test$ gcc zz.c
hanoi@AKSHAY[17:52]~/test$ ./a.out <zz.c
#include <stdio.h>
#define BUFSIZE 256  //any other value u comfortable with
main () {
char buf[BUFSIZE];
while(NULL!=fgets(buf,BUFSIZE,stdin)) printf("%s",buf);
}
hanoi@AKSHAY[17:52]~/test$
0
 
akshayxxCommented:
>>#define BUFSIZE 256  //any other value u comfortable with

not just any other .. any other >= 2
0
 
harliquenAuthor Commented:
Thanks! but what happens if you don't know how large the input file is going to be? ie. what if it is larger than the BUFSIZE you specify? how can you make it adjust accordingly?

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.

 
akshayxxCommented:
in my example if u are concerned with only reading and displaying the file .. the buffer size doesnt matter.. ( shud be greater than 2)

as by definition of gets and fgets .. it reads only that much characters that will fit in the size of buffer ( minus one space for NULL string terminitator)..
and i keep reading in while loop .. till i can read anyting ( which is till end of file occurs)

hence no problems here..
0
 
harliquenAuthor Commented:
kewl, thanks for the explanation.
0
 
akshayxxCommented:
welcome
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now