not printing the last number

This is a discussion on not printing the last number within the C Programming forums, part of the General Programming Boards category; 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"); ...

  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 01:31 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,528
    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 in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 02:35 AM
  2. Printing charcter by character
    By Ron in forum C Programming
    Replies: 8
    Last Post: 06-15-2008, 10: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, 09:04 PM
  5. Printing a Character from Binary
    By SavesTheDay in forum C Programming
    Replies: 6
    Last Post: 02-19-2002, 02:35 PM

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