Showing posts with label File handling. Show all posts
Showing posts with label File handling. Show all posts

Sunday, 31 August 2014

,

C++ program to count record in binary file

C++ program to count record in binary file:A simple file handling program which count the record in binary file.
Binary file:file stored in binary format. A binary file is computer -readable but not human-readable. All executable programs are stored in binary files, as are most numeric data files. In contrast, text files are stored in a form (usually ASCII) that is human-readable.

C++ program to count record in binary file



C++ program to count record in binary file


#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<stdio.h>
#include<fstream.h>


struct stud
{
char name[50];
int rno;
};

void main()
{


clrscr();
stud st;
int i;
ofstream gpj("abcabc.dat",ios::binary|ios::trunc);
if(!gpj)
{cout<<"File Cannot Be Created"<<endl;
}
else
{
int n;


cout<<"How many Students U Want to Enter ???? "<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter Name"<<endl;
gets(st.name);
cout<<"Enter Roll No"<<endl;
cin>>st.rno;
gpj.write((char*)&st,sizeof(st));
}gpj.close();
}
int a=0;
ifstream gp("abcabc.dat",ios::binary);
if(!gp)
{
cout<<"File Error"<<endl;
}
else
{
gp.seekg(0);
while(gp.read((char*)&st,sizeof(st)))
{
a++;
}
cout<<"No = "<<a;
gp.close();
}
getch();

}

Publisher: Anand - 03:28

Monday, 25 August 2014

C Program to Copy File into Another File

Here is C Program to Copy File into Another File is given below which copy the data of first file into second file.First it open both file in read and write mode simultaneously.For performing this task it uses two file handling function fgetc() and fputc().

C Program to Copy File into Another File

C Program to Copy File into Another File



#include<stdio.h>
#include<process.h>
void main() {
   FILE *fp1, *fp2;
   char a;
   clrscr();
   fp1 = fopen("test.txt", "r");
   if (fp1 == NULL) {
      puts("cannot open this file");
      exit(1);
   }
   fp2 = fopen("test1.txt", "w");
   if (fp2 == NULL) {
      puts("Not able to open this file");
      fclose(fp1);
      exit(1);
   }
   do {
      a = fgetc(fp1);
      fputc(a, fp2);
   } while (a != EOF);
   fcloseall();
printf("\n Content will be written successfully to file");
   getch();
}

Output

Content will be written successfully to file


Explanation :


The idea behind copy the content of one file into another is very simple,the file which contains the content will be open in read mode and the file in which we want to copy the contents will be open in write mode.Then by using fgetc() function get the character from first file and simultaneously write this character into second file by using fputc() until the end of the file.

  




Publisher: Anand - 08:13

Sunday, 24 August 2014

C Program to Capitalize First Letter of every Word in a File

This C program to capitalize first letter of every word in a file , first open the file in read mode and call a user defined function which use some build in file handling function such as fseek(),fgetc() and fputc() and  capitalize first letter of every word in a file.Here is source code of the C Program to capitalize first letter of every word in a file. The C program is successfully compiled and run on a Linux system as well as windows system. 
C Program to Capitalize First Letter of every Word in a File

C Program to Capitalize First Letter of every Word in a File


#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
int capitalizeFile(FILE *); 

void main(int argc, char * argv[])
{
    FILE *fp1;
char fp[10];
int p;

    fp1 = fopen(argv[1], "r+");
if (fp1 == NULL)
{
printf("cannot open the file ");
exit(0);
}
    p = capitalizeFile(fp1);
if (p == 1)    
{    
printf("success");
}
else
{
printf("failure");
}
fclose(fp1);
}

/* capitalizes first letter of every word */
int capitalizeFile(FILE *fp)
{
char c;

    c = fgetc(fp);
if (c >= 'a' && c <= 'z')
{
fseek(fp, -1L, 1);
fputc(c - 32, fp);
}
while(c != EOF)    
{
if (c == ' ' || c == '\n')
{
            c = fgetc(fp);
if (c >= 'a' && c <= 'z')
{
fseek(fp, -1L, 1);
fputc(c - 32, fp);
}
}
else
{
            c = fgetc(fp);
}
}
return 1;
}
Publisher: Anand - 20:17