0

hi , getw() is used to read the next INTEGER from a filestream right ?

Assume
-------
My text document has the following
12 i am an 3 4 intern 5 67

Expected output
---------------
Then , i expected that, on running getw() inside a while loop, it would print 12 3 4 5 67
But my code just prints out the entire file.
Please refer to my code below and send me your valuable suggestions

My code
--------

#include<stdio.h>
#include<conio.h>

int main(void)
{
FILE* fp;
int num=0;

clrscr();

fp=fopen("e:\\desktop\\tc1\\bin\\fileh\\numbers.txt","r");
if(fp==NULL)
printf("\nFile could not be opened ");

else
{
  printf("\nNumbers are going to be read from the file \n------------------------------------------\n");
  while(1)
  {
   num=getw(fp);

   if(num==EOF)
   break;

   else
   {
    putw(num,stdout);
   }
  }
}
printf("\n\nAll numbers from the file have been read ");

getch();
return 0;
}

PS: Yes, i have tried changing the mode to "rb" instead of r as well , as i saw that in a few places.
---

Thank you so much for the help in advance

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by rubberman
0

The putw() function writes the raw integer data to the output stream. If you want it viewable by human eyes, then use printf("%d ", num) instead. Also, you might find this reads better:

for (num = getw(fp); num != EOF; num = getw(fp))
{
    printf("%d ", num);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.