Help:error in reading from stdin


 
Thread Tools Search this Thread
Top Forums Programming Help:error in reading from stdin
# 1  
Old 09-22-2006
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  
Old 09-22-2006
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 02:16 PM..
# 3  
Old 09-23-2006
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  
Old 09-25-2006
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  
Old 09-25-2006
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  
Old 09-25-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

script hangs when reading from stdin

script: while read inputline; do if ; then if ; then break fi fi done Looks like the script hangs when stdin is empty or contains space. Any ideas on how to circumvent this? is it possible to use getline to process stdin content? (4 Replies)
Discussion started by: ux4me
4 Replies

2. SCO

Error: msgcnt 1 vxfs: mesg 016: vx_ilisterr - / file system error reading

Hello... i got an error in my SCO OpenServer 6. the error is: msgcnt 1 vxfs: mesg 016: vx_ilisterr - / file system error reading inode 373 Can anyone help me? (1 Reply)
Discussion started by: AndryMB
1 Replies

3. UNIX for Advanced & Expert Users

ssh error: Error reading response length from authentication socket

Hi - I am getting the error `Error reading response length from authentication socket' when I ssh from my cluster to another cluster, and then back to my cluster. It doesn't seem to affect anything, but it's just annoying that it always pops up and tends to confuse new users of the cluster. I... (1 Reply)
Discussion started by: cpp6f
1 Replies

4. Shell Programming and Scripting

reading from stdin in a shell script

Hello, I've managed to get my .procmailrc file to work. At least it triggers a script which creates a file. But the file is empty. How do I get at the data that's been piped? I've done much creative googling to no avail. I belive it should be in stdin, but I can't figure out how to access... (4 Replies)
Discussion started by: mmesford
4 Replies

5. UNIX for Dummies Questions & Answers

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (3 Replies)
Discussion started by: vvaidyan
3 Replies

6. Programming

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (1 Reply)
Discussion started by: vvaidyan
1 Replies

7. Solaris

error reading sections error at install

Hi, I'm trying to install Solaris 10 on my Ultra 10 but it won't boot. I tried an original cd and a downloaded one. I got the following error: "error reading sectors" (yes, i checked the md5 sums) For the complete error see the following shot: ... (2 Replies)
Discussion started by: doelman
2 Replies

8. Shell Programming and Scripting

Reading an Error message

I have a script that reads a file then moves all the files that are listed within that file. So read file A then copy all files that are listed within. I have the script append a log everytime a file is moved. My issue is that it's appending the log even though the file was not found. ... (3 Replies)
Discussion started by: whegra
3 Replies

9. UNIX for Dummies Questions & Answers

reading from tape error

I have been attempting to read a DLT tape, but encountered problems. I have applied patches from SUN that 'resolve' known problems with tape reading etc...but still the error! Techsupport reckon that the data/tape is corrupt. I would like to think they aren't. Are there any issues with... (10 Replies)
Discussion started by: colesy
10 Replies

10. UNIX for Dummies Questions & Answers

error reading from tape

Hi all, I am attempting to read tar images from a DLT tape, and have recieved an error, which i don't know: tar: blocksize = 8 tar: directory checksum error I have been able to read from other tapes, using the same command (tar -tvf /devicename) but this time i have an... (3 Replies)
Discussion started by: colesy
3 Replies
Login or Register to Ask a Question