Thread: getw/putw problem

  1. #1
    Registered User fenikkusu's Avatar
    Join Date
    Jan 2009
    Posts
    12

    getw/putw problem

    I tried to write/read a line of intergers(like 12345) to a file using getw, but I don't know how to stop the input loop, like this:

    Code:
    #include<stdio.h> 
    int main(void)
    { 
      FILE *f1,*f2,*f3; 
      int number; 
      printf("Content of the data file\n"); 
      f1=fopen("data","w"); 
    
      while((number=getw(stdin))!='\n')
       putw(number,f1);
    
      fclose(f1); 
      
      f1=fopen("data","r"); 
      f2=fopen("odd","w"); 
      f3=fopen("even","w"); 
      
      while((number=getw(f1))!=EOF)/* Read from data file*/ 
      { 
      if(number%2==0) 
       putw(number,f3);/*Write to even file*/ 
      else 
       putw(number,f2);/*write to odd file*/ 
      } 
      
      fclose(f1); 
      fclose(f2); 
      fclose(f3); 
      f2=fopen("odd","r"); 
      f3=fopen("even","r"); 
      
      printf("\n\nContent of the odd file\n\n"); 
      while((number=getw(f2))!=EOF) 
       printf("%d%d",number); 
       
      printf("\n\nContents of the even file"); 
      while((number=getw(f3))!=EOF) 
       printf("%d",number); 
       
      fclose(f2); 
      fclose(f3); 
      getch();
      return 0;
    }
    Please help me consider it

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Looks to me like these w() functions are legacy -- why do you want to use them?

    Anyway, if getw() does exactly what it says I would not trust it to find ANY particular character.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If getw reads a number (a wild guess based on typing "man getw" into Google), then why would you expect it to ever be \n? According to the man pages I'm seeing (assuming we're even talking about the same thing), getw returns EOF on error.

    I guess you need to decide, how you want the user to stop typing numbers.

  4. #4
    Registered User fenikkusu's Avatar
    Join Date
    Jan 2009
    Posts
    12
    Yeah, that's what I'm considering. I want user to stop typing numbers by pressing enter(suggest some other "smooth" ways?) but getw is only used with intergers

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by fenikkusu View Post
    Yeah, that's what I'm considering. I want user to stop typing numbers by pressing enter(suggest some other "smooth" ways?) but getw is only used with intergers
    Two simple variants:
    1. Use a "sentry value", e.g. enter 0, -1, -99999999 or some other value that is not in the valid range that can be used as "this is the end of input".
    2. Read the value as a string, and then convert using strtol() or a similar function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    ...or simply ask the user how many numbers he wants to enter and then ask for a number that many times.

    Greets,
    Philip

    PS: while(true); do read a; if [ `echo $a%2 | bc` -eq 0 ]; then echo $a >> even; else echo $a >> odd; fi; echo $a >> data; done;
    Last edited by Snafuist; 02-19-2009 at 05:56 AM.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  7. #7
    Registered User fenikkusu's Avatar
    Join Date
    Jan 2009
    Posts
    12
    Quote Originally Posted by matsp View Post
    Two simple variants:
    1. Use a "sentry value", e.g. enter 0, -1, -99999999 or some other value that is not in the valid range that can be used as "this is the end of input".
    2. Read the value as a string, and then convert using strtol() or a similar function.

    --
    Mats
    1.I've changed '\n' into -1 and it doesn't help. dunno
    2.I just want to see how getw works and don't want to resort to string usage here

    @Philip:
    ...or simply ask the user how many numbers he wants to enter and then ask for a number that many times.
    Asking for a number is ok but what I want to know is how getw stops (as scanf stops at the first space/tab/newline)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM