The program is this :
Write a program which takes 10 integer numbers from user. Save all the positive numbers to one file, all the negative numbersto another file.
So,the coding will be compare all integers with 0 and save according in two different files right?
If I am right then how to store it in files?
Can we save it in already existed file?
or we have to create one then save it?
And what is the syntax to do this?

Recommended Answers

All 8 Replies

So,the coding will be compare all integers with 0 and save according in two different files right?

Correct.

If I am right then how to store it in files?

See FILE* file handling functions (a tutorial is here).

Can we save it in already existed file?

Yes, see the above turorial how to do that. It's one of the flags given to fopen() function.

Ancient Dragon
Thanku :)
But I still got the doubt.
I tried it in this way,I mean I wrote half:

FILE *fp1,*fp2;
if a[i]<0
{
printf("the number is negative")
fp1=fopen("negative.txt","w+");

Till here,it will create check the conditiona nd create the file.
After this again m confused how to write the a[i] to negative.txt?
This below code is for writing the contents to the file so the user typed content will be written :

if(fp1!=NULL)
    {
    c=getchar();
    while(c!='\n')
    {
        putc(c,fp1);
        c=getchar();
    }



But,In this negative positive integers case,The already given number should be stored,user is not supposed to type again after its being proved that its negative or positive. So, how to store it ??

You need to open both files before doing anything and keep them open until the program is done comparing numbers. Opening files is very time consuming, so just open once during the lifetime of the program.

open file1
open file2
start of loop and repeat 10 times
   get input
   if value < 0 
      save in file1
    else if value > 0
      save in file2
end of loop
close both files

Is fputc() used to save in files?

I know the logic d above u wrote,I actually want the syntax to store it in file after comparison

Is fputc() used to save in files?

Yeas it can. But is it appropriate for your program? What did the tutorial tell you to do?

I know the logic d above u wrote,I actually want the syntax to store it in file after comparison

Of course you do. It's always easier to have someone else do your homework for you. But, unless some bumpkin comes along, you'll have to actually learn to do it yourself.

I actually want the syntax to store it in file after comparison

There isn't much to it -- just one line of code which you can easily get if you take the time to read a textbook or an online tutorial. I'm not going to tell you what it is because you need to learn how to look up the answers to your questions yourself before just asking others to do it for you.

thanx anyways I am trying

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.