Like Tree1Likes
  • 1 Post By manasij7479

input redirection <

This is a discussion on input redirection < within the C Programming forums, part of the General Programming Boards category; I wrote a standard i/o program which was taken from "c for dummies" Code: #include <stdio.h> int main() { char ...

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

    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 02:38 AM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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
    141
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    As the executable is in the current directory, you have to give "./io <text.txt"
    MK27 likes this.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,670
    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, Sunshine, 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,806
    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,670
    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, Sunshine, and read this, this, and this before posting again.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02: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, 02:15 AM
  5. Help with redirection
    By Nornny in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2004, 07:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21