Thread: not printing the last number

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    Bangalore,INDIA
    Posts
    14

    Unhappy not printing the last number

    1 2 3 4

    file "text1.txt" contain the above data

    Code:
    #include<stdio.h>
    void main()
     {
    	FILE *fp;
    	int i;
    	fp=fopen("text1.txt","r");
    	while((i=getw(fp))!=EOF)
            putw(i,stdout);
     }
    Output:
    1 2 3

    But it should be 1 2 3 4..Where is 4?
    I am not getting the answer..Please help me out.
    Last edited by raj.knd; 10-11-2011 at 12:31 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Is there a newline at the end of the line?

    Perhaps you need fflush(stdout); if there isn't.

    Then again, perhaps you're using that old fossil known as TurboC.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    Bangalore,INDIA
    Posts
    14
    That data is in a file. I am using TurboC 4.5.
    I have already used that fflush(stdout);

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So why are you using getw/putw (wide chars) instead of getc/putc ?
    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.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    Bangalore,INDIA
    Posts
    14
    Thanks. Its working. But if I want to do any arithmetic operation will getc work? I think not.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Depends on what you mean by "arithmetic operation". If '1' and '2' are read in, adding them will not yield the character '3'. However, since they are integral values, they can be added.
    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.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You've definitely got the wrong idea, if you thought that reading "1 2 3 4" using getw would have given you integers 1 2 3 4.

    getw reads 2 bytes WITHOUT any interpretation. So the first time around, you would have an int containing 0x3120 (or 0x2031), where 0x31 is the ASCII value for '1' and 0x20 is the ASCII character for a space.

    Look at something like fscanf, if you want to convert character '1' into numeric 1.
    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. Printing the size of a character
    By juice in forum C Programming
    Replies: 18
    Last Post: 09-25-2011, 01:35 AM
  2. Printing charcter by character
    By Ron in forum C Programming
    Replies: 8
    Last Post: 06-15-2008, 09:39 PM
  3. Printing single character from a string
    By TJJ in forum C Programming
    Replies: 4
    Last Post: 11-05-2003, 06:25 PM
  4. Trouble Printing ASCII Character
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2002, 08:04 PM
  5. Printing a Character from Binary
    By SavesTheDay in forum C Programming
    Replies: 6
    Last Post: 02-19-2002, 02:35 PM