Thread: printing from output...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    printing from output...

    here's a very simple program:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
            system("cls");
            printf("\n\n\tHello, world!");
            system("pause");
    
            return 0;
    }
    ...my question is, how am I going to print on my printer the program's output directly?

    If it can't be done directly, how am I going to save it as a text-file (*.txt)?



    Thanks in advance for your help... Merry Christmas and Happy New Year... ^.^

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Generally speaking, you don't print to the printer directly (you can have your OS print to the printer, but that's something else). You use fprintf to print to a file (which you must first open with fopen and then later close with fclose).

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    44
    ok, if that's the case, then let me change the question... How am I going to save the program's output into a text-file (*.txt)? -_+

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You would still need to use fprintf to print to a file (which you must first open with fopen and then later close with fclose).

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, most environments allow you to open the printer as a file (eg: fopen( "LPT1", "w' )), but this isn't very reliable, and handling errors gracefully can be problematic...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    44
    ok, Thank you very much for your help...

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main ()
    {
    	FILE * testFile;
    	int age;
    	char name [40];
    
    	clrscr();
    
    	testFile = fopen ("testFile.txt","w");
    
    	printf("Enter your name here: ");
    	gets(name);
    
    	printf("Enter your age: ");
    	scanf("%i", &age);
    
    
    	fprintf (testFile, "The name you have inputed is: %s", name);
    	fprintf (testFile, "\nYour age is: %i", age);
    
    
    	fclose (testFile);
    
    	return 0;
    }
    ...hmmm, how am I going to open this file, automatically, right after the user presses any key.. I mean, to pop it up on the screen, the result?

    thanks... -_+

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Depends on what you are actually trying to achieve - it may be simplest to just duplicate all fprintf() statements with the equivalent printf() statements to produce the same output on scree as on the file. Alternatively, you may ask the user if they want to output to a file or the screen - then either use printf or fprintf as approprite. [Or, if you want to be really tricky, when the user answers console, just set the testFile = stdout, so that the output can be done using fprintf(testFile, ...) for both cases - stdout is a file that normally is represented by the scree/console].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    44
    Depends on what you are actually trying to achieve - it may be simplest to just duplicate all fprintf() statements with the equivalent printf() statements to produce the same output on scree as on the file. Alternatively, you may ask the user if they want to output to a file or the screen - then either use printf or fprintf as approprite. [Or, if you want to be really tricky, when the user answers console, just set the testFile = stdout, so that the output can be done using fprintf(testFile, ...) for both cases - stdout is a file that normally is represented by the scree/console].
    i don't get it, I mean, I want to open the file, to notepad... Like those programs that output the log-files.. Yeah, that's what I mean... Thanks... -_+





    and uhmm,, another question please...

    Code:
    	FILE * testFile;
    	int age;
    	char name [40];

    what does that asterisk between FILE and testFile; mean? I'm confused.. I saw this one on pointers... and it's really confusing for me... what would happen if I remove the asterisk? thanks... ^.^

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    FILE *testFile declares a pointer to a FILE object. It must be a pointer (unless you REALLY want to get yourself in deep water).

    For now, you can ignore the fact that it is a pointer, tho' - because the way you'll use it throughout your life as a programmer is just "an identifier of a file" - the fact that it happens to be a pointer or that it's pointing to a file object is as meaningful as knowing the stoichometric mixture values for fuel and air in an engine when driving a car - pointless.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    9
    Quote Originally Posted by ShadeS_07 View Post
    ok, Thank you very much for your help...

    #include <stdio.h>
    #include <conio.h>

    int main ()
    [...]

    note that conio.h is not a standard header this may make your code less portable. Also
    IHMO 'int main ( void )' is highly recommended if you want to omit main's arguments.

  11. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Question

    IHMO 'int main ( void )' is highly recommended if you want to omit main's arguments.
    ..I noticed that one... but I don't get it... especially the int main()'s arguments.. what's their use? Hmmmm, right. From now on, whenever I write programs with C, I'll use int main(void) instead of int main()... thanks for the tip... -_+


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main(void)
    {
    	FILE *testFile;
    	int age;
    	char name[40];
    
    	system("cls");
    
    	testFile = fopen("c:\\testFile.txt", "w");
    
    	printf("\n\n\tEnter your name here: ");
    	gets(name);
    
    	printf("\tEnter your age here: ");
    	scanf("%i", &age);
    
    
    	fprintf(testFile, "The name you have inputed is: %s", name);
    	fprintf(testFile, "\nYour age is: %i", age);
    
    	fclose(testFile);
    
    	return 0;
    }

    ..Now how am I going reopen the file after that? I mean, the testFile (testFile.txt)... I want it to open right after the user presses any key from the program... as if that the user have double-clicked the file, and opens with notepad... I don't want the user to go to "C:\TESTFILE.TXT" and then double click the file to see the result manually, I want the file to pop up on the screen, just like those programs that open the log-file generated by the program per se...

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ShadeS_07
    ..I noticed that one... but I don't get it... especially the int main()'s arguments.. what's their use? Hmmmm, right. From now on, whenever I write programs with C, I'll use int main(void) instead of int main()... thanks for the tip... -_+
    In this case there is no difference since it is a function definition. In a function declaration (prototype), the empty parentheses means the function takes an unspecified number of arguments, while the parentheses with the void keyword means the function has no parameters. It is arguably better (in C) to keep the void keyword even when it is unnecessary in order to be consistent.

    Quote Originally Posted by ShadeS_07
    Now how am I going reopen the file after that? I mean, the testFile (testFile.txt)... I want it to open right after the user presses any key from the program
    Why not just display the file's contents from your program?

    By the way, do not use gets(). Use fgets() instead in order to avoid buffer overflow.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jul 2008
    Posts
    44
    Why not just display the file's contents from your program?
    ...for some reasons... Plus, I want to know how to do this one... -_+

    By the way, do not use gets(). Use fgets() instead in order to avoid buffer overflow.
    I know, but every time I input data with fgets(), then print it on screen, there's a new-line there.. How am I going to get rid of that?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ShadeS_07
    ...for some reasons... Plus, I want to know how to do this one... -_+
    Okay... one method is to use system(), e.g.,
    Code:
    system("notepad.exe c:\\testFile.txt");
    Of course, the very requirement that you want to use a specific editor (notepad) to open the file means that your program will not be portable.

    Quote Originally Posted by ShadeS_07
    I know, but every time I input data with fgets(), then print it on screen, there's a new-line there.. How am I going to get rid of that?
    Replace that new line with a null character, e.g., using the fact that it is the last character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jul 2008
    Posts
    44
    Okay... one method is to use system(), e.g.,
    Code:

    system("notepad.exe c:\\testFile.txt");
    thanks man, i'll try that...

    Replace that new line with a null character, e.g., using the fact that it is the last character.
    how?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing output
    By TAboy24 in forum C Programming
    Replies: 2
    Last Post: 12-07-2007, 12:41 AM
  2. Output an array in a textbox
    By Diablo02 in forum C# Programming
    Replies: 5
    Last Post: 10-18-2007, 03:56 AM
  3. Printing to file / Output issues
    By Roflcopter in forum C++ Programming
    Replies: 19
    Last Post: 10-11-2007, 07:38 PM
  4. sorting output on student info program
    By indigo0086 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2002, 11:29 AM
  5. printing output from prog
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 08:50 PM