Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I have a code in C and I need to add omething more. Check to tell me if you can help me.


C#
#include <stdio.h> 
#include <limits.h> 
int process( FILE *f ) { 
    
	int a, b; 
	int total = 1;						//adding how many times "a" was larger than "b"
	
	/*  and now I want to add total++  every time 
		a number found that is greater than another
		Where do I need to add it? 
		So when I get output with eg 9 lines to show me in the
		end of the process the number 10 in a new line -cause the 
		start value of the int total is 1 and to save the number
		10 in a new file called out.txt */
    if ( f == 0 ) return 0; 
    if ( fscanf( f, "%d", &a ) != 1 ) return INT_MIN; 
    if ( (b= process( f )) == INT_MIN ) return a; 
    if ( a > b ) return printf( "%d > %d and total now is %d\n", a, b, total ), a; 
    return b; 
} 
int main( void ) { 
    FILE *fp= fopen( "xxx.txt", "r" ); 
    process( fp ); 
    fclose( fp ); 
	
    return 0; 
}
Posted
Updated 31-Oct-14 3:49am
v3
Comments
Philippe Mori 30-Oct-14 19:55pm    
It is not clear what you are trying to do and your code is probably more complex that it need to be. What is the purpose of the recursivity?

By the way giving us 1 or 2 example of inputs and expected output might help a lot to understand.
Richard Deeming 31-Oct-14 9:53am    
I've rolled back your edit, as the only change you made was to delete the code sample. Without that code, the question makes absolutely no sense.

If I understand you right, you need to change this:
C#
if ( a > b ) return printf( "%d > %d and total now is %d\n", a, b, total ), a;
To this:
C#
if ( a > b )
{
total++;
return printf( "%d > %d and total now is %d\n", a, b, total );
}

And this:
C#
int total = 1;                      //adding how many times "a" was larger than "b"
To this:
C#
static int total = 1;                       //adding how many times "a" was larger than "b"


[edit]Just noticed the end of line rubbish in your code...[/edit]
 
Share this answer
 
v2
Comments
[no name] 30-Oct-14 17:05pm    
what do you mean "Just noticed the end of line rubbish in your code.."?
The code work though. But I want also to save the total number in a new text file called out.txt in the end of the hole process.Where do I need to add the code?
CPallini 31-Oct-14 10:14am    
What would be the use of the return value of printf?
OriginalGriff 31-Oct-14 11:53am    
That's a very good question...
Give me a year or two to think about it...:blush:
Hi,

You don't need to use recursive call for this functionality...

Just the read the line of file and then extract the character and check in which was you need to...

If this the program you should work then find the area when the recursive function call was returned to its initial stage and display the "total" their.

Also you need to make sure that the "total" variable was declared as global value.. or integer pointer..


Thanks,
Ullas
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900