Answer the question
In order to leave comments, you need to log in
How to transfer numbers according to the condition from file to file?
The numbers in the file are integers and do not equal zero. You need to transfer the numbers from one file to another: 1) So that at the beginning there are 5 positive and 5 negative, and so on. 2)To 20 positive and 20 negative and so on. Please help. The best I can do is:
#include
#include
int main()
{
int i,k,j;
FILE *in;
FILE *out;
in=fopen("in","r");
out=fopen("out","w");
while(!feof(in))
{
fscanf(in,"%d",&k);
if(k>0)
{
fprintf(out,"%d ",k);
}
}
fprintf(out,"\n");
fclose(in);
fclose(out);
return 0;
}
Answer the question
In order to leave comments, you need to log in
Elementary Watson!
Read the file (in its entirety, why the hell are you fooling around with stream reading?)
Parse all the numbers in it into an array
From an array, you can form any file you like - it’s already easier with fprintf / sprintf, so that you don’t translate the number into text :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question