Like us on Facebook to get the latest links daily

Friday, January 27, 2012

Union and Files in C




Unions are like structures except they use less memory. If you create a structure with a double that is 8 bytes and an integer that is 4 bytes then the total size will still only be 8 bytes. This is because they are on top of each other. If you change the value of the double then the value of the integer will change and if you change the value of the integer then the value of the double will change. Here is an example of how to declare and use a union:
#include<stdio.h>
 
typedef union num
{
   double d;
   int i;
} NUM;
 
int main()
{
   NUM n;
   n.d = 3.14;
   n.i = 5;
   return 0;
}

*      files

Files let you store data on secondary storage such as a hard disk so that your programs can later retrieve that data again. There are 2 types of files which are text files and data files.

Text files

Text files are used for storing character strings in a file. To create a text file you must first declare a file pointer.
#include<stdio.h>
 
int main()
{
   FILE *f;
   return 0;
}

You must then open the file using the fopen function. fopen takes 2 parameters which are the file name and the open mode. fopen returns a file pointer which we will assign to the file pointer called f.
#include<stdio.h>
 
int main()
{
   FILE *f;
   f = fopen("test.txt","w");
   return 0;
}

The above example will create a file called test.txt. The "w" means that the file is being opened for writing and if the file does not exist then it will be created. Here are some other open modes:
r
Open for reading
r+
Open for reading and writing
w
Open for writing and create the file if it does not exist. If the file exists then make it blank.
w+
Open for reading and writing and create the file if it does not exist. If the file exists then make it blank.
a
Open for appending(writing at the end of file) and create the file if it does not exist.
a+
Open for reading and appending and create the file if it does not exist.
To write a string to the file you must use the fprintf command. fprintf is just like printf except that you must use the file pointer as the first parameter.
#include<stdio.h>
 
int main()
{
   FILE *f;
   f = fopen("test.txt","w");
   fprintf(f,"Hello");
   return 0;
}

When you are finished using a file you must always close it. If you do not close a file then some of the data might not be written to it. Use the fclose commmand to close a file.
#include<stdio.h>
 
int main()
{
   FILE *f;
   f = fopen("test.txt","w");
   fprintf(f,"Hello");
   fclose(f);
   return 0;
}

The fgets command is used when reading from a text file. You must first declare a character array as a buffer to store the string that is read from the file. The 3 parameters for fgets are the buffer variable, the size of the buffer and the file pointer.
#include<stdio.h>
 
int main()
{
   FILE *f;
   char buf[100];
   f = fopen("test.txt","r");
   fgets(buf,sizeof(buf),f);
   fclose(f);
   printf("%s\n",buf);
   return 0;
}

Data files

A data file is used to store types of data such as integers. When you open a data file you must add the letter b to the open mode parameter of fopen.
#include<stdio.h>
 
int main()
{
   FILE *f;
   f = fopen("test.dat","wb");
   fclose(f);
   return 0;
}

fwrite is used to write to a file. The first parameter of fwrite is a pointer to the variable that you want to write to the file. The second parameter is the size of the variable that must e written. The third parameter is the number of variables to be written. The fourth parameter is the file pointer of the file you want to write to.
#include<stdio.h>
 
int main()
{
   FILE *f;
   int buf;
   f = fopen("test.dat","wb");
   buf = 100;
   fwrite(&buf,sizeof(buf),1,f);
   fclose(f);
   return 0;
}

fread is used to read from a file and is the same as fwrite except that the data is read into the variable instead of writing from it. You must remember to change the file mode to read.
#include<stdio.h>
 
int main()
{
   FILE *f;
   int buf;
   f = fopen("test.dat","rb");
   fread(&buf,sizeof(buf),1,f);
   printf("%d\n",buf);
   fclose(f);
   return 0;
}

Data files using structures

You can read and write structures to data files in the same way as you do with any other data type. Here is an example:
#include<stdio.h>
 
struct
{
   char name[100];
   int age;
} p;
 
int main()
{
   FILE *f;
   strcpy(p.name,"John");
   p.age = 25;
   f = fopen("test.dat","wb");
   fwrite(&p,1,sizeof(p),f);
   fclose(f);
   return 0;
}




Get E-mail Subscription for Latest Updates


Don't forget to Click on activation link , sent to your E-mail ID

No comments:

Post a Comment

You might also like:

Related Posts Plugin for WordPress, Blogger...