Thread: fprintf will NOT WORK !!!

  1. #16
    Unregistered
    Guest
    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.

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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]

  3. #18
    Unregistered
    Guest
    Hammer, what was the major error in the original code?

  4. #19
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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]

  5. #20
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    746
    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

  6. #21
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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
    Multiples are perfectly valid.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #22
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    746
    well...learning all the time.

    stop winking, i said i was a newbie
    Steve

  8. #23
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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]

  9. #24
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    746
    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

  10. #25
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    Quite right, it doesn't. It's really one of those style/personal preference things.

    >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]

  11. #26
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    746
    well ive learnt one little thing today. at least i'm coming back to programming, after being a bit C sick..........
    Steve

  12. #27
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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]

  13. #28
    Unregistered
    Guest
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM