≡ Menu

3 C Program Examples to Create a File with Data

This tutorial explains how to create a file from a C program.

In these examples, we’ll create new HTML files and write some content to it.

The content of the file will be different, but these three C example program should explain you how to use the c file functions like fopen, fprintf, etc., to create and manipulate files.

Example 1 – Create a File

This is the first example, which creates a new file called index.html with some HTML content to it.

Create f1.c with the following example code.

#include <stdio.h> 
#include <stdlib.h> 

int main( void ) 
{ 
  FILE      *ptrFile = fopen( "index.html", "w"); 
  int        i; 

  fprintf( ptrFile, "<HTML>\n "); 
  fprintf( ptrFile, "<HEAD> <TITLE> The Geek Stuff </TITLE> </HEAD>\n" ); 
  fprintf( ptrFile, "<BODY BGCOLOR=\"#110022\" TEXT=\"#FFBBAA\"> \n"); 
  fprintf( ptrFile, "<p>Example 1: This file was created from a C program</p>\n"); 
 
  for( i = 0; i< 5; i++) 
  { 
    fprintf( ptrFile, "<p>%d. line</p>\n", i); 
  } 

  fprintf( ptrFile, "</BODY>\n"); 
  fprintf( ptrFile, "</HTML>"); 
  fclose( ptrFile ); 
  
  return EXIT_SUCCESS; 
}

In the above, the fopen function takes two parameter. First, the name of the file, which in this case is index.html. Second, the file operation, which in this example, is “w”, which is “write” to create new file.

This function will return a FILE pointer, which is stored in the variable ptrFile.

Using fprintf function, we can write content to the file that we just created. This function takes two parameter. First, the FILE pointer of the file where we should write the content. Second, the content that should be written. This is a string content. Please note that fprintf will append this content if the file already has some existing content.

Finally, after finish writing the content to the file, use fclose function to close the FILE pointer.

The following is the output of the above c program. First compile the f1.c, which will create a.out executable. Execute a.out, which will create the index.html file with some HTML content as shown below.

# cc f1.c

# ./a.out

# cat index.html
<HTML>
 <HEAD> <TITLE> The Geek Stuff </TITLE> </HEAD>
<BODY BGCOLOR="#110022" TEXT="#FFBBAA"> 
<p>Example 1: This file was created from a C program</p>
<p>0. line</p>
<p>1. line</p>
<p>2. line</p>
<p>3. line</p>
<p>4. line</p>
</BODY>

For more details on all the individual functions, please refer to our earlier article on this topic: File Handling in C with Examples (fopen, fread, fwrite, fseek)

Example 2 – Create a File

This is similar to the previous example, but here only the content of the program is different.

We are using rand function here to determine the total number of lines to be written.

We’ll also use rand function to create a pattern of star characters by randomly figuring out how many spaces and starts should be used in a line.

Create f2.c program with the following example code.

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h>

void printBlanks(  int,      FILE* ); 
void printStars (  int, int, FILE* ); 

int main( void ) 
{ 
  FILE      *ptrFile = fopen( "index.html", "w"); 

  int       iNumberOfLines, iNumberOfBlanks, iNumberOfStars; 
  int       iCounter =  1; 
  int       iColor   = -1; 

  iNumberOfLines = iNumberOfBlanks = iNumberOfStars = 0; 

  fprintf( ptrFile, "<HTML>\n "); 
  fprintf( ptrFile, "<HEAD> <TITLE>Example 2</TITLE> </HEAD>\n" );               
  fprintf( ptrFile, "<BODY BGCOLOR=\"#FFFFFF\" > \n");
  fprintf( ptrFile, "<p>Example 2: This file was created from a C program</p>\n");
 
  srand( ( unsigned) time ( NULL ) ); 
  iNumberOfLines = (rand() % 20) + 10; 

  do{ 
    iNumberOfBlanks = ( rand() % 10 ); 
    iNumberOfStars  = ( rand() % 30 ); 
    iColor          = ( rand() % 4  ); 
       
    printBlanks( iNumberOfBlanks, ptrFile ); 
    printStars(  iNumberOfStars, iColor, ptrFile  ); 
       
    iCounter++; 
  } while ( iCounter <= iNumberOfLines ); 

  fprintf( ptrFile, "</BODY>\n"); 
  fprintf( ptrFile, "</HTML>"); 
  fclose( ptrFile ); 
  
  return EXIT_SUCCESS; 
} 

void printBlanks( int   iNumberOfBlanks,  
                  FILE* ptrFile ) 
{ 
  int i; 
  fprintf( ptrFile," <p> " ); 
  for( i = 0; i < iNumberOfBlanks; i++ ) 
  fprintf( ptrFile, " "); 
} 
       
void printStars(  int   iNumberOfStars, 
                  int   iColor, 
                  FILE* ptrFile  ) 
{ 
  int i; 
  switch ( iColor ) 
  { 
    case 0: fprintf( ptrFile,"<font color=\"FF0000\">"); break; 
    case 1: fprintf( ptrFile,"<font color=\"00FF00\">"); break; 
    case 2: fprintf( ptrFile,"<font color=\"0000FF\">"); break; 
    case 3: fprintf( ptrFile,"<font color=\"FFFF00\">"); break; 
  } 
  for( i = 0; i < iNumberOfStars; i++ ) 
    fprintf( ptrFile, "*"); 
     
  fprintf( ptrFile," </font></p>"); 
}

The following is the partial output of the above program.

# cc f2.c

# ./a.out

# cat index.html 
<HTML>
 <HEAD> <TITLE>Example 2</TITLE> </HEAD>
<BODY BGCOLOR="#FFFFFF" > 
<p>Example 2: This file was created from a C program</p>
...

In the above example, in each row we added few blank spaces in the front of the line. We also added few color characters. The color of the character is one of four basic ones: blue, green, yellow or red. This pattern is repeated few times. We are using nbsp tag to create the blank space.

Example 3 – Create a File

This is also similar to previous example in terms of how to create a new file from C.

But, this will also take two existing files (original.txt and compared.txt), open them up and compare.

Then, this will highlight the differences in an output html file.

Create f3.c program with the following example code.

#include <stdio.h> 
#include <stdlib.h> 

#define EOLN '\n' 

int main( void ) 
{ 
  FILE      *ptrFile           = fopen( "index.html" , "w" ); 
  FILE      *ptrFileOriginal   = fopen( "original.txt"  , "r" ); 
  FILE      *ptrFileCompared   = fopen( "compared.txt" , "r" ); 

  char       cOriginal, cCompared; 
  char       cColor     = 'b'; 

  fprintf( ptrFile, "<HTML>\n "); 
  fprintf( ptrFile, "<HEAD> <TITLE>Example 3: This file was created from a C program</TITLE> </HEAD>\n" ); 
  fprintf( ptrFile, "<BODY TEXT =\"#FFFFFF\" BGCOLOR=\"#000000\" > \n"); 
  fprintf( ptrFile, "<p> <center>COMPARING TWO SAME SIZE FILES</center></p>\n"); 
  fprintf( ptrFile, "<p> \n"); 

  while( (cOriginal = fgetc( ptrFileOriginal ) ) != EOF ) 
  { 
    cCompared = fgetc( ptrFileCompared ); 

    if( ( cOriginal == cCompared ) && ( cColor == 'r' ) ) 
    { 
      fprintf( ptrFile, "</FONT>"); 
      fprintf( ptrFile, "<FONT COLOR=\"00FF00\">"); 
      cColor = 'g'; 
    } 
    else if( ( cOriginal != cCompared ) && ( cColor == 'g' ) ) 
    { 
      fprintf( ptrFile, "</FONT>"); 
      fprintf( ptrFile, "<FONT COLOR=\"FF0000\">"); 
      cColor = 'r'; 
    } 
            
    if( cColor == 'b' ) 
    { 
      if( cOriginal == cCompared) 
      { 
        cColor = 'g'; 
        fprintf( ptrFile, "<FONT COLOR=\"#00FF00\">"); 
                    
      } 
      else 
      { 
        cColor = 'r'; 
        fprintf( ptrFile, "<FONT COLOR=\"#FF0000\">"); 
      } 
      fputc( cOriginal, ptrFile ); 
      continue; 
    } 

    if( cOriginal == EOLN ) 
      fprintf( ptrFile, " <BR> "); 
    else 
      fputc( cOriginal, ptrFile ); 
        
  } 
    
  fprintf( ptrFile, "</FONT></p>" ); 
  fprintf( ptrFile, "</BODY>\n"); 
  fprintf( ptrFile, "</HTML>\n"); 

  fclose( ptrFile );
  fclose( ptrFileOriginal ); fclose( ptrFileCompared ); 
  
  return EXIT_SUCCESS; 
}

For this, I used the following original.txt and compared.txt file as the input files. Make sure you create this sample files before you execute the c program.

# cat original.txt 
line 1
line 2
line 3
# 
# 
# cat compared.txt 
line 4
line 5
line 6

Now, compile and execute the f3.c program. The following shows a partial output of the above code.

# cc f3.c

# ./a.out

# cat index.html 
<HTML>
 <HEAD> <TITLE>Example 3: This file was created from a C program</TITLE> </HEAD>
<BODY TEXT ="#FFFFFF" BGCOLOR="#000000" > 
<p> <center>COMPARING TWO SAME SIZE FILES</center></p>
<p> 
<FONT COLOR="#00FF00">line </FONT><FONT COLOR="FF0000">1</FONT><FONT COLOR="00FF00"> <BR> line </FONT><FONT COLOR="FF0000">2</FONT><FONT COLOR="00FF00"> <BR> line </FONT><FONT COLOR="FF0000">3</FONT><FONT COLOR="00FF00"> <BR> </FONT></p></BODY>
</HTML>
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • DuskoKoscica June 29, 2017, 2:51 am

    In order to see the created file, … try to use some program to open the file… I hope that I don’t need to talk too much about it…

    In another words, cat will present the file created, but some browser will be able to present the file…

    Yo can also use gcc that comes within Linux,…