Just as a test, in your fprintf, you might temporarily replace outptr with stdout. That should make it print to the screen, since it is stdout.
Just as a test, in your fprintf, you might temporarily replace outptr with stdout. That should make it print to the screen, since it is stdout.
This does what you want (I believe). It's your original code with a few mods.
Code:#include <stdio.h> #include <conio.h> /* Non standard header for getch() */ int main(void) { char c; int N,x,iter = 1; FILE *outptr; if ((outptr=fopen("myOutput.txt","w")) == NULL) { printf("Failed to open out file\n"); return (1); } printf("\nPlease enter a value for N "); if (scanf("%i",&N) != 1) { printf("Failed to read a number\n"); return (1); } putchar('\n'); for(x = N, iter = 0; x > 1; x--) { printf("%i + ", x); fprintf(outptr, "%i + ", x); iter += x; } printf(" 1 = %i", iter); fprintf(outptr, " 1 = %i", iter); fclose(outptr); getch(); return (0); }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Hammer, what was the major error in the original code?
Well... I compiled and ran the code you put in your first post, and (apart from the missing # at the start, } at the end and the "wt" mode in fopen), it did run and create an output file. It wasn't complete, it only had this in it:
5 + 4 + 3 + 2 +
It was missing the last bit because there were no fprintf's after the loop, only printf's.
Really all I did was tidy the code, and probably the most important bit, added error checking on the fopen() call. Without doing some testing on your PC, I can only presume that things weren't working because the file didn't open properly.
As you can also see, I added checking to ensure the user entered a number, but that wouldn't have affect your original code, provided the user did actually enter a number.
If you want to find out which bit made the difference, start by deleting the output file and rerunning your program with "w" instead of "wt". Then add error checking to te fopen(), and so, gradually working your way through.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
i'm only a newbie..
i thought you could only have one initialization, not many separated by a comma as he's got ie
for(x = N, iter = 0; x > 1; x--)
should be
iter=0;
for(x = N; x > 1; x--)
Steve
Multiples are perfectly valid.Originally posted by stevey
i'm only a newbie..
i thought you could only have one initialization, not many separated by a comma as he's got ie
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
well...learning all the time.
stop winking, i said i was a newbie
Steve
How did you know I was w.... oohh winking. Sorry matey it's nervous twitch of mine.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
you winker
seriously tho, it wasn't taught on my course, or in my teach yourself c in 21 days book!!
but it doesn't make any difference actually does it, if i am in the habit of initialising as i do, not in the for statement.
Steve
Quite right, it doesn't. It's really one of those style/personal preference things.Originally posted by stevey
but it doesn't make any difference actually does it, if i am in the habit of initialising as i do, not in the for statement.
>for(x = N, iter = 0; x > 1; x--)
I like to combine them in one this way if there's only a couple of variables to be initialised specifically for use within the loop. Too many though, and you'll loose clarity.
The way I see it, if you do it this way
>iter = 0;
>for(x = N; x > 1; x--)
it isn't so obvious that iter is being set to 0 specifically for use with the for loop. Eg, maybe it's used somewhere later in the function.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
well ive learnt one little thing today. at least i'm coming back to programming, after being a bit C sick..........
Steve
Now that's corny.. Maybe you should add it to ya joke thread in GD (good effort that one... keep it up).
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
What was char c for? You also do not need the math header file.
You can get by without it. Did you check your results? It was
mathmatically incorrect. It added 1 extra making it incorrect. You also were not printing the total to the file. Here this code works. You may want to rework it so you are not adding 0 or anything after you get out of your for loop. Also use int main & return 0.
Some teachers are lost themselves in what they're teaching. If this isn't what you need, post a more direct question & I'll help you. - kris
#include <stdio.h>
#include <conio.h>
int main( void )
{
char c;
int N = 0, x = 0, iter = 0;
FILE *outptr;
outptr=fopen("a:\\myOutput.txt","w");
printf("\nPlease enter a value for N ");
scanf("%i",&N);
printf("\n ");
for(x = N, iter = 0; x > 1; x--)
{
printf("%i", x);
printf(" + ");
iter = iter + x;
fprintf(outptr, "%i", x);
fprintf(outptr, " + ");
}
printf( " 0 = " );
printf("%i", iter);
fprintf( outptr, " 0 = " );
fprintf( outptr, "%d", iter );
printf( "\n\nPress any key to end this program." );
while ( !kbhit( ) )
{
}
fclose(outptr);
return 0;
}