Thread: fprintf and writing on the screen at the same time

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    Portugal
    Posts
    5

    fprintf and writing on the screen at the same time

    Hi

    I'm learning files in C languagem but I'm with a problem. I always thought that when we do a fprintf to write something into a file (in this case using "a") this something would be written in the debug display :S

    I tested with this program but nothing is written in the debug:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
    	FILE * xyz;
    	
    	
    	xyz = fopen("example.txt","a");
    	fprintf(xyz,"12345\n");
    	fclose(xyz);
    
    	xyz = fopen("example.txt","a");
    	fprintf(xyz,"67890\n");
    	fclose(xyz);
    
    	system("pause");
    
    	return 0;
    }

    Is there any way or some process to write into a file and to the debug screen at the same time?

    I'm really confused :S

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    No, the program is doing exactly what you are telling it to do. If you want to echo the behavior to the screen just place printf statements everytime you use fprintf. So:
    Code:
    xyz = fopen("example.txt","a");
    printf("%s", "12345\n");
    fprintf(xyz,"12345\n");
    fclose(xyz);
    Plus don't forget you want to check your pointer prior to use to ensure it is valid.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No, and you don't want it to, either.

    The way it works is, you have a program with output. While you're working on the program, you'll have output sent to the screen, so you can easily check the accuracy and format of the output, easily.

    When you're satisfied that your output is OK, then you have it sent to a file. Since the program will be built up (probably), using several functions and blocks of code, the original data will undoubtedly be something you DON'T want to send to anybody - until you have checked it, and know it's accurate.

    If you have the printf()'s lines of code working right, it's simple to add the fprintf() lines of code to your program, when you're ready.

    Programs that write their output to a file, usually have too much data for a screen, so it's just a nuisance having it go there, once you have the program working well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing tab (\t) to the screen
    By vsovereign in forum C Programming
    Replies: 6
    Last Post: 06-04-2010, 07:38 AM
  2. Replies: 7
    Last Post: 03-26-2008, 03:21 AM
  3. Problem with writing in a file using fprintf
    By g_p in forum C Programming
    Replies: 5
    Last Post: 04-26-2007, 08:51 AM
  4. Problems writing some chars to files with fprintf
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 04-18-2006, 06:00 PM
  5. writing to the screen
    By zMan in forum Windows Programming
    Replies: 0
    Last Post: 01-24-2002, 01:06 PM