Pages

Tuesday, November 28, 2017

Split file into 3 equal files.

C++ Program to Split file into 3 equal files.

This program will take input from data.txt file and split it into 3 equal size files (data1.txt, data2.txt, data3.txt).


#include<iostream>
#include<fstream>

using namespace std;
int main()
{
ifstream fin;
ofstream fout1, fout2, fout3;
char ch;
int i, cnt=0, equal_part=0, last_part=0;
fin >> std::noskipws;

fin.open("data.txt");
if(!fin){  
    cout<<"Error Open Error."; exit(1); 
}

//logic to count total character in input data file
while(fin.eof()==0){
fin >> ch;
cnt++;
}
cout << "Total characters in data.txt file = "<< cnt-1 << endl;
fin.close();
//logic to divide no. characters in each file
equal_part = (cnt-1)/3;
last_part = (cnt-1)%3;
cout <<"Total files to be created=3."<< endl;
cout << "Each output file contains: "<<equal_part <<" characters."<<endl;
cout << "Last output file contains: "<<equal_part+last_part<<" characters."<<endl;

fin.open("data.txt");
fin >> std::noskipws;

fout1.open("data1.txt");
if(!fout1){ 
cout<<"File data1.txt Open Error.";  exit(1);
}
for(i=0; i<equal_part; i++) {
fin >> ch;
fout1 << ch;
}

fout2.open("data2.txt");
if(!fout2){ 
cout<<"File data2.txt Open Error.";  exit(1);
}

for(i=0; i<equal_part; i++)
{
fin >> ch;
fout2 << ch;
}

fout3.open("data3.txt");
if(!fout3){ 
cout<<"File data3.txt Open Error.";  exit(1);
}
for(i=0; i<equal_part+last_part; i++)
{
fin >> ch;
fout3 << ch;
}

fin.close();
fout1.close();
fout2.close();
fout3.close();

return 0;
}

Output of program

Total characters in data.txt file = 10
Total files to be created=3.
Each output file contains: 3 characters.
Last output file contains: 4 characters.

Tuesday, November 21, 2017

C++ Program to copy files.

#include<iostream>
#include<fstream>

using namespace std;
int main()
{
ifstream fin;
ofstream fout;
char ch, fname1[20], fname2[20];

fin >> std::noskipws; //To copy whitespace characters

cout<<"Enter source file name: ";
gets(fname1);
fin.open(fname1);

if(!fin){
cout<<"Error Open Error.";
exit(1);
}

cout<<"Enter target file name: ";
gets(fname2);
fout.open(fname2);

if(!fout){
cout<<"File Open Error.";
exit(1);
}
//Logic to copy data from file1 to file2
while(fin.eof()==0){
fin >> ch;
fout << ch;
}
cout<<"File content copied successfully...";

fin.close();
fout.close();
return 0;
}

Output of program

Enter source file name: file1.txt
Enter target file name: file2.txt
File content copied successfully...

Friday, November 17, 2017

C++ File Operations on Binary Files

This C++ file management program will append data into a binary file.
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
class student
{
  int rollno;
  char name[20];
  char dept[10];

  public:
void getdata()
{
cout << "Rollno: ";
cin >> rollno;

cout << "Name: ";
cin >> name;

cout << "Branch: ";
cin >> dept;
}
};

int main()
{
  student s1;
  char ans='y';

  ofstream fout("student.txt", ios::app);

  while(ans=='y' || ans=='Y')
  {
s1.getdata();
fout.write((char *)&s1, sizeof(s1));
cout<<"Data appended in file successfully.\n";
cout<<"\nWant to add more data? (y/n)..";
cin>>ans;
  }

  fout.close();
  return 0;
}

Output of program:

Rollno: 101
Name: Amit
Branch: MCA
Data appended in file successfully.

Want to add more data? (y/n)..y
Rollno: 102
Name: Sunit
Branch: PhD
Data appended in file successfully.

Want to add more data? (y/n)..n

Tuesday, November 14, 2017

Use of fprintf and fscanf

File management: Program to demonstrate use of fprintf() and fscanf() function. This program will read a word from keyboard and write it into a data.txt file. After closing a file, program will again open it and get a word from file and display on screen.



#include <stdio.h>
#include <stdlib.h>
int main()
{
  FILE *fp;
  char str[80], str1[80];

  fp = fopen("data.txt","w");

  if(fp == NULL)
  {
printf("Cannot open file.\n");
exit(1);
  }
  printf("Enter string to be written in a file: ");
  fscanf(stdin, "%s", str); /*Read from keyboard */

  fprintf(fp, "%s", str); /*Write str to file */
  fclose(fp);

  fp = fopen("data.txt","r");

  if(fp == NULL) {
printf("Cannot open file.\n");
exit(1);
  }
  fscanf(fp, "%s", str1); /* read a word from file and copy into str1 */
  fprintf(stdout, "%s", str1); /* print str1 on screen */
  return 0;
}

Output of program

Enter string to be written in a file: Hello
Hello