OldSchoolCode

.com



This tutorial has to do with files
This tutorial will be on creating your own text file. To write and read from files we will use a set of function calls that exist in the stdio.h file. These are tried and true C function calls that are used in almost every program.

Remember we are only dealing with straight text files today, not binary files (although the differences are minor).

I'm going to draw out each function, and then I encourage you to also read the help file (f1 fopen):)

the first thing you do is tell the computer to open a file. We are going to cover 3 basic choices today. You can tell it to open a file to "write", "read" or "append".

If you open a file to "write" - and that file already exists - it will COMPLETELY ERASE that file and give you a blank file with the same name. If the file doesn't exist it will create it for you.

If you open a file to "read", you can only read the contents of that file - if the file doesn't exist the open call will fail.

If you open a file to "append", it opens at the end of the file and any writing you do will happen at the end of the file. If the file doesn't exist it will create it for you.

so far so good?

Ok to open a file you use the fopen function call. You give it the filename you want to open and the mode you want to open it with. fopen will return a pointer to the file.

example:
FILE *pFile = fopen( "c:\\myFile.txt", "w" );
ok first off, notice the double back slash - this is because a single backslash is referred to as the "escape" character. It tells the compiler that the letter coming directly after it might have a special case. With "\n" it knows to treat the n as a newline, with "\0" it knows to treat the 0 as a null. So when we do "\\" it sees the next backslash and knows there is no special case for that - so it just uses a single backslash....that can be kind of confusing, i hope it made sense.

Next thing to notice is the "w" that means open it for write mode. Remember, if the file myFile.txt located at the root of your c drive already exists, it will wipe it out when you open it.

A couple more examples
FILE *pFile = fopen( "C:\\Program Files\\Tools\\doc1.txt", "r" );
this will open the file doc1.txt for reading. It will not affect the file on disk. If fopen returns 0 (NULL) it means the file doc1.txt did not exist.
FILE *pFile = fopen( "C:\\Program Files\\Tools\\doc1.txt", "a" );
this will open the file doc1.txt so you can append to the end of it. It will not affect text currently in the document.

So after you have opened a file, the first thing you do is check and make sure it returned a valid pointer to a file - if it returns 0 (NULL) it means it could not open the file. (Most likely because the file didn't exist).

you'll want to handle it something like this...
if ( pFile == 0 )
{
    printf( "the file could not be opened!" );
    return;
}
so lets say you have a valid file pointer, how do you write text to it? Use fprintf ! fprintf works exactly like printf except it takes a pointer of a file to write to.
fprintf( pFile, "this is the first line in the file" );
You can also use fputs to put strings to a file. You can only use fputs for char arrays, not for integers. If you use fputs you'll have to build the string yourself via sprintf...(what is sprintf??? sprintf is just like printf except it puts your data in another string array instead of printing it to the screen)
char string[ 128 ];
int myAge = 5;
 
sprintf( string, "Age: %d", myAge );
fputs( string, pFile );
 
//alternatively you can use
fprintf( pFile, "Age %d", myAge );
after you are all done writing to the file, use fclose to close it
fclose( pFile );
if you don't call fclose, you will lose most or all of the data you have written to the file. so make sure you call fclose!!!

I haven't shown you how to read from a file yet, but that is for the next tutorial.

__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE   
   
#include <stdio.h>
#include <conio.h>
#include <string.h>
   
void main(void)
{
   //no more than 128 chars!
   char name[ 128 ]; 
   char fileName[ 128 ];
	
   printf( "\n Enter your first name :" );

   scanf( "%s", name );

   //take the name entered, add C:\\%s.txt to
   //it and save the name with path to "filename"
   //for example: the name Dave would now be
   //C:\Dave.txt
   sprintf( fileName, "C:\\%s.txt", name );
	
   //create the file with the name Dave.txt
   FILE * pFile = fopen( fileName, "a" );

   //print these strings into the new file
   fprintf( pFile, "this is the first line in the file\n" );
   fprintf( pFile, "this is the second line in the file" );
   
   fputs( "\nthis is the third line in the file", pFile );
   fputs( "\nthis is the fourth line in the file", pFile );

   if ( pFile == 0 ) 
   {
      printf( "\n the file could not be opened!" );
   }

   else 
   {
      //close the file!
      fclose( pFile );
      printf( "\n finished\n\n" );
   }

   getch( );
}