Thread: I/O redirection. Is the problem with my compiler or with my OS?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    8

    Question I/O redirection. Is the problem with my compiler or with my OS?

    I keep on getting errors while trying to do file redirection.
    I wrote the following code to test if the problem is with the complexity of my other programs or just the compiler.
    Code:
    #include <stdio.h>
    int main()
    {
    	int x;
    	scanf("%d", &x);
    	printf("%d", x);
    	return 0;
    }
    Input is 4. Output should also be 4.
    When I first tried doing it in the usual way, it worked. But when I tried file redirection, I got a long series of numbers. Idk if it's the address of the input or what.

    Here's what I got:
    I/O redirection.  Is the problem with my compiler or with my OS?-sad-bmp
    File in.txt contains just the number 4.

    I tried doing this in our lab; it worked perfectly fine.
    I use MinGW compiler and my OS is Windows XP.
    Also, what could be the problem if it's neither the compiler nor the OS? :/

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you could try this, to perhaps help with diagnosis
    Code:
    #include <stdio.h>
    int main()
    {
    	int x = 0;
    	int r;
    	if ( (r=scanf("%d", &x)) == 1 ) {
    		printf("%d\n", x);
    	} else {
    		printf("scanf failed, returned=%d\n",r);
    	}
    	return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with execl() and redirection.
    By arunj in forum C Programming
    Replies: 6
    Last Post: 03-20-2008, 08:59 PM
  2. Redirection
    By Rossco in forum C Programming
    Replies: 3
    Last Post: 11-23-2007, 06:49 AM
  3. Cout redirection problem
    By MrLucky in forum C++ Programming
    Replies: 6
    Last Post: 06-06-2007, 11:11 AM
  4. no redirection
    By FinallyFriday in forum C Programming
    Replies: 1
    Last Post: 11-12-2005, 02:34 PM
  5. redirection
    By spinner in forum C Programming
    Replies: 1
    Last Post: 05-06-2003, 08:07 PM