Thread: Using a function to print to a file

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    5

    Using a function to print to a file

    I just started out with C programming. I've read a few basic tutorials.OK, so I nabbed some code from this very website which prints a file.
    Code:
    void printfile() {     int c;     FILE *file;     file = fopen("text/README.txt", "r");     if (file) {	  while ((c = getc(file)) != EOF)	       putchar(c);	  fclose(file);     }     printf("\n");}
    I put this in a function called "void printfile()".I want two things from this thread. Firstly, I wanna know what this function does step-by-step. Secondly, how do I make it so that every time I call this function, I could pick which file it prints?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by yabobay View Post
    I just started out with C programming. I've read a few basic tutorials.OK, so I nabbed some code from this very website which prints a file.
    I put this in a function called "void printfile()".I want two things from this thread. Firstly, I wanna know what this function does step-by-step. Secondly, how do I make it so that every time I call this function, I could pick which file it prints?
    First of all, format your code properly. Functions are not one liners.
    Compile and run this version of the function. What do you think will happen, providing the file does exist?

    You will need to add error checking.

    Code:
    #include <stdio.h>
    
    void printfile()
    {
       int c = 0;         // Initialize all local variables
       FILE *file = NULL; // Ditto
    
       file = fopen("text/README.txt", "r");
    
       if (file)
       {
          while ((c = getc(file)) != EOF)
             putchar(c);
    
          fclose(file);
       }
    
       printf("\n");
    }
    
    int main(void)
    {
       printfile();
    
       return 0;
    }
    You need to study a good book on the C Programming Language, cover to cover, and do all the exercises at the end of each chapter! Choose one of the three listed below:

    C Programming, A Modern Approach
    Author: K. N. King

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e
    Deitel & Deitel

    You need a thorough understanding of the language before I would add "Command Line Arguments" to the code to answer your last question.
    Last edited by rstanley; 01-01-2020 at 08:15 PM.

  4. #4
    Registered User
    Join Date
    Jan 2020
    Posts
    5
    About the formatting, it's actually formatted right in my editor. Not sure what's wrong with the forum post.I definitely will read these. But until I'm done with them, what does writing main(void) instead of main() do?

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by yabobay View Post
    About the formatting, it's actually formatted right in my editor. Not sure what's wrong with the forum post.
    You might be copying from an IDE. Make sure you are using plain text, not with IDE formatting.

    Quote Originally Posted by yabobay View Post
    But until I'm done with them, what does writing main(void) instead of main() do?
    For main(), the two are effectively equivalent. For all other functions you create, there is a difference. You need to study the language to fully understand.

    If your main(), or any other function, does not use any formal parameters to the function, always use "void". In other words, use int main(void), rather than int main().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-13-2013, 07:10 PM
  2. Replies: 15
    Last Post: 06-13-2012, 12:34 PM
  3. print values of function to file
    By threahdead in forum C Programming
    Replies: 3
    Last Post: 10-14-2002, 05:17 PM
  4. Replies: 13
    Last Post: 04-29-2002, 11:06 PM
  5. print first and print last function
    By RawleyMacias in forum C Programming
    Replies: 6
    Last Post: 02-14-2002, 10:28 PM

Tags for this Thread