?
Solved

displaying piped input

Posted on 2003-03-10
6
Medium Priority
?
200 Views
Last Modified: 2010-04-15
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
Comment
Question by:harliquen
[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
  • 4
  • 2
6 Comments
 
LVL 8

Accepted Solution

by:
akshayxx earned 300 total points
ID: 8101494
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8101498
>>#define BUFSIZE 256  //any other value u comfortable with

not just any other .. any other >= 2
0
 

Author Comment

by:harliquen
ID: 8102700
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
Independent Software Vendors: 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 8

Expert Comment

by:akshayxx
ID: 8103628
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
 

Author Comment

by:harliquen
ID: 8103683
kewl, thanks for the explanation.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8103724
welcome
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

752 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