#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    How to store it after comparing ? I tried but its not storing in file.


    Write a program which takes 10 integer numbers from user. Save all the positive numbers to one file, all the negative numbersto another file.

    In this , I know after comparing numbers ,based on comparison condition ,we have to store it in respective positive and negative file.

    I created 2 pointers for this 2 files.
    I did the comparison part and as per the comparison inside it:

    the syntax I used to create file is :
    fp1=fopen("positive.txt","w");
    The file is being created but after that I am not able to do the storing part.
    To store I used this syntax:
    putc(a[i],fp1);


    Can somebody guide me?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    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
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,901
    Rep Power
    481
    You'd do better with

    fprintf

    Please read the manual about putc. See if you can figure out why it didn't work.

    [edit---> bad code cited in following message removed Sorry that this wrecks the thread but on the other hand, bad code in the thread is also bad.]
    Last edited by b49P23TIvg; September 28th, 2012 at 12:53 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Originally Posted by b49P23TIvg
    You'd do better with

    if (1 != fprintf(fp1,"%d ",a[i]))
    fputs("\nYour computer is hosed.\n",stderr);
    fprintf() returns the number of characters printed. Your format string has 2 directives. The first directive ("%d") causes at least 1 character to be printed; the second directive (" ") causes exactly 1 character to be written; so, barring output errors, the total number of characters written is 2 or more ... and that is the return value of the function.

    You're testing if it's different than 1. Barring output errors it is guaranteed to be different and the output of your code is to always print "\nYou computer is hosed.\n" to the standard error stream.

    Comments on this post

    • b49P23TIvg agrees : whoops!

IMN logo majestic logo threadwatch logo seochat tools logo