Thread: input redirection <

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    144

    input redirection <

    I wrote a standard i/o program which was taken from "c for dummies"
    Code:
    #include <stdio.h>
    
    int main()
    
    {
    	char ch;	
    	
    	while ((ch = fgetc(stdin)) != EOF)
    		fputc(ch,stdout);
    
    	return(0);
    }
    I saved it as io.c and ran the program

    Also, I created a new text file called text.txt and wrote the words
    Code:
    Hello. I am a text file
    However, when I want to redirect stdin with
    Code:
    io < text.txt,
    bash says that command io is not found.

    Any ideas on how to resolve this?
    I tried cat < text.txt and it works fine.

    But a few pages later another command is shown ls -l | underline where underline is a program that I created, and again it shows that udnerline command is not found
    Last edited by bos1234; 03-27-2012 at 01:38 AM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Tell me how you compiled the program. Give the compiler and options you used if at all possible.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    thanks foir your reply
    I compiled via

    cc io.c -o io

    version is

    Code:
    Using built-in specs.
    Target: i386-redhat-linux
    Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --disable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
    Thread model: posix
    gcc version 4.1.2 20080704 (Red Hat 4.1.2-51)

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    As the executable is in the current directory, you have to give "./io <text.txt"

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by manasij7479 View Post
    As the executable is in the current directory, you have to give "./io <text.txt"
    That assumes the current working directory is not included in the path (the "./" is optional if it is). Which, admittedly, is good practice for several reasons.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char ch;   
         
    while ((ch = fgetc(stdin)) != EOF)
    The fgetc function returns an int, not a char. This can be important when comparing against EOF.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by hk_mp5kpdw View Post
    The fgetc function returns an int, not a char. This can be important when comparing against EOF.
    It is important when comparing against EOF, as EOF is generally an integral value that cannot be represented by a char. So no char can test equal to EOF.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by grumpy
    It is important when comparing against EOF, as EOF is generally an integral value that cannot be represented by a char. So no char can test equal to EOF.
    If char is signed, it is certainly possible that a char can test equal to EOF, e.g., if EOF is -1. In such a case, the code that uses char may appear to be correct, but then fail when compiled elsewhere.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input output redirection
    By chess2009 in forum C Programming
    Replies: 1
    Last Post: 02-26-2011, 06:46 PM
  2. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  3. Redirection
    By Rossco in forum C Programming
    Replies: 3
    Last Post: 11-23-2007, 06:49 AM
  4. Help / Loops; input redirection from files
    By Frank_Rye in forum C Programming
    Replies: 10
    Last Post: 10-16-2005, 01:15 AM
  5. Help with redirection
    By Nornny in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2004, 07:25 AM