- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- A simple C langauage w/ the basic input/output
Operating System - HP-UX
1754809
Members
3691
Online
108825
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-11-2001 06:27 AM
тАО07-11-2001 06:27 AM
I wrote a very simple C program for any convenience.
I used scanf to get a standard input and printf for standard output like this.
--- a.out ----
main()
{
int instance ;
printf("Input A = ") ;
scanf("%d", &instance) ;
printf(%d, instance) ;
}
-- end of a.out ----
I'd like to save the output to a file.
If I run "a.out" using redirection, the result goes to certain file named but I can't see a message "Input A=" at the shell prompt.
For instance,
# a.out > aaa
"Input A=" is not displayed.
So I have to know what question is in advance.
If I execute "a.out" without any parameters, I can enter a value for the question displayed at the shell prompt but I can't save the result to a file.
Any ideas would be helpful.
I used scanf to get a standard input and printf for standard output like this.
--- a.out ----
main()
{
int instance ;
printf("Input A = ") ;
scanf("%d", &instance) ;
printf(%d, instance) ;
}
-- end of a.out ----
I'd like to save the output to a file.
If I run "a.out" using redirection, the result goes to certain file named but I can't see a message "Input A=" at the shell prompt.
For instance,
# a.out > aaa
"Input A=" is not displayed.
So I have to know what question is in advance.
If I execute "a.out" without any parameters, I can enter a value for the question displayed at the shell prompt but I can't save the result to a file.
Any ideas would be helpful.
Never say "no" first.
Solved! Go to Solution.
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-11-2001 06:53 AM
тАО07-11-2001 06:53 AM
Re: A simple C langauage w/ the basic input/output
Hi,
if you redirect stdout, you need to prompt on stderr:
fprintf(stderr, "Input A = ") ;
scanf("%d", &instance) ;
printf("Value= %d \n", instance) ;
--- Result :
# a.out
Input A = 23
Value= 23
# a.out > weg
Input A = 23
# cat weg
Value= 23
#
Hope this helps
Volker
if you redirect stdout, you need to prompt on stderr:
fprintf(stderr, "Input A = ") ;
scanf("%d", &instance) ;
printf("Value= %d \n", instance) ;
--- Result :
# a.out
Input A = 23
Value= 23
# a.out > weg
Input A = 23
# cat weg
Value= 23
#
Hope this helps
Volker
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-11-2001 07:55 AM
тАО07-11-2001 07:55 AM
Solution
Hi,
You have 3 choices:
1) print your prompt to stderr via fprintf(stderr,
That is not considered good form because stderr should only be used for error messages (but you can use it)
2) If you know that you will always be run as interactive, you can prompt to "/dev/tty".
int main(int argc, char *argv[])
{
int cc = 0;
FILE *f = NULL;
f = fopen("/dev/tty","w+");
if (f != NULL)
{
int instance = 0;
(void) fprintf(f,"Input A: ");
(void) fflush(f);
(void) fclose(f)
scanf("%d", &instance) ;
printf(%d, instance) ;
}
else cc = 2;
return(cc);
}
3) You could pass a filename as an argument
int main(int argc, char *argv)
{
int cc = 0;
if (argc > 1)
{
FILE *f = NULL;
f = fopen(argv[1],"w");
if (f != NULL)
{
int instance ;
printf("Input A = ") ;
scanf("%d", &instance) ;
fprintf(f,"%d", instance) ;
fclose(f);
}
else cc = 2;
}
else cc = 1;
return(cc);
}
Clay
You have 3 choices:
1) print your prompt to stderr via fprintf(stderr,
That is not considered good form because stderr should only be used for error messages (but you can use it)
2) If you know that you will always be run as interactive, you can prompt to "/dev/tty".
int main(int argc, char *argv[])
{
int cc = 0;
FILE *f = NULL;
f = fopen("/dev/tty","w+");
if (f != NULL)
{
int instance = 0;
(void) fprintf(f,"Input A: ");
(void) fflush(f);
(void) fclose(f)
scanf("%d", &instance) ;
printf(%d, instance) ;
}
else cc = 2;
return(cc);
}
3) You could pass a filename as an argument
int main(int argc, char *argv)
{
int cc = 0;
if (argc > 1)
{
FILE *f = NULL;
f = fopen(argv[1],"w");
if (f != NULL)
{
int instance ;
printf("Input A = ") ;
scanf("%d", &instance) ;
fprintf(f,"%d", instance) ;
fclose(f);
}
else cc = 2;
}
else cc = 1;
return(cc);
}
Clay
If it ain't broke, I can fix that.
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP