The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-22-2006
zhshqzyc zhshqzyc is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 34
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);
      }
}
  #2 (permalink)  
Old 09-22-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,928
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..
  #3 (permalink)  
Old 09-23-2006
zhshqzyc zhshqzyc is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 34
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" */
}
  #4 (permalink)  
Old 09-25-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,928
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");
  #5 (permalink)  
Old 09-25-2006
zhshqzyc zhshqzyc is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 34
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
...
}
  #6 (permalink)  
Old 09-25-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,928
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);
would do a lot to narrow down what's going wrong.

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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0