![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| error reading sections error at install | doelman | SUN Solaris | 2 | 02-05-2007 12:21 PM |
| getting error 0403-016 Cannot find or open the file while reading a long line | karthee | Shell Programming and Scripting | 2 | 06-05-2005 12:00 AM |
| Reading an Error message | whegra | Shell Programming and Scripting | 3 | 11-24-2003 04:40 AM |
| reading from tape error | colesy | UNIX for Dummies Questions & Answers | 10 | 10-30-2002 11:44 AM |
| error reading from tape | colesy | UNIX for Dummies Questions & Answers | 3 | 08-30-2002 11:54 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Help:error in reading from stdin
Code:
void redirect(int argc, char *argv[])
{
int flag;
if (strcmp(argv[0], ">") == 0)
flag = 1;
else if (strcmp(argv[0], "<") == 0)
flag = 2;
else if (strcmp(argv[0], ">>") == 0)
flag = 3;
else
printf("Something Wrong,Please Check!\n");
switch (flag) {
case 1:
freopen("out.txt","w",stdout);
printf("This sentence is redirected to a file\n");
// fclose(stdout);
case 2:
freopen("in","r",stdin);
printf("This sentence is redirected to a file\n");
// fclose(stdin);
case 3:
freopen("out.txt","a+",stdout);
printf("This sentence is redirected to a file\n");
// fclose(stdout);
default:
wait(5);
}
}
|
|
||||
|
A few improvements and your code will be pretty good.
The switch/case statement is useless, just put the code inside the if/then/else. case 2 will NOT print to a file. That 'wait' is going to crash if it ever gets run since wait demands a pointer. But more likely it'll just hang forever. You have done absolutely no error checking anywhere at all ever here, so it could be failing anywhere without you ever knowing. Why not have redirect() return a value? After all, a user might ask for something impossible. If you run this program from a shell, chances are your program will never see the >> and < stuff since the shell handles that for you. You'll need to make them strings, like '<<', to prevent the shell doing that. You haven't told us what the error is, though. What is it? Last edited by Corona688; 09-22-2006 at 01:16 PM.. |
|
||||
|
Please look at my updated code
I try to redirect to a file from a shell such as "ls > out" Then I can save the file list in "out". However after run it, it just print the sentence "This goes to the file out" Then keep scilent, therefore I have to press ctrl c to stop it. What is wrong? Code:
void redirect1(int argc, char *argv[])
{
char *str;
str = argv[2];
printf("This goes to the file %s\n",(char *)str);
freopen((char *)str,"w",stdout);
/* write the context into file "str" */
/* ex: "ls > out" */
}
|
|
||||
|
I get the feeling this is not ALL of your code, that the important bits are missing.
Do you check the return value from freopen? You can print messages to console even when stdout is redirected with Code:
fprintf(stderr,"This is a message\n"); |
|
||||
|
no any return value.Just keep science.
Code:
int redirect1(int argc, char *argv[])
{
char *str;
str = argv[2];
printf("This goes to the file %s\n",(char *)str);
freopen((char *)str,"w",stdout);
/* write the context into file "str" */
/* ex: "ls > out" */
}
int main(void)
{
...
if (strcmp(argv[1],">")==0)
{ ret = redirect1(argc, argv);
printf("ret = %d\n",ret);}//never print//no print
...
}
|
|
||||
|
You're still missing many important bits of your program; every time I ask you to post more, you post less.
And just making your function an int isn't going to magically make it start returning error codes; you're still not doing any error checking, at all, ever. Your program could probably tell you what's going wrong if you'd let it. Even a simple thing like Code:
if(freopen(str,"w",stdout)==NULL)
{
fprintf(stderr,"Couldn't freopen\n");
return(0);
}
else
fprintf(stderr,"freopened to %s\n",str);
return(1);
Also, as I explained before, unless you're using some bizzare nonstandard shell, your shell is probably redirecting to file for you. Your program won't see > in the arguments at all. You have to use '>' instead of > to convince the shell to pass it as a string instead of processing it. |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|