Monthly Archives: February 2013

File I/O in C – Part 2

Part 2:

1. Explain File I/O.

2. Code for writing and reading values:

a. Character values. [fgetc & fputc]

b. String values. [fprintf & fscanf]

c. Integer value. [putw & getw]

d. Structure value. [fread & fwrite]


/*
Developed By:Prof. Vinod Pillai
vinodthebest@gmail.com
File in C - Part 2.
*/

#include

struct Student
{
int id;
int marks;
};

int main()
{
FILE *fp;

int ivalue,irel;
float fvalue,frel;
char cvalue,crel;
char strvalue[20],strrel[20];
struct Student svalue,srel;

//-------------------------------------------------------------------
fp=fopen("one.txt","w");

//Character Value Read & Write [fputc & fgetc]
//Write:
printf("\n Enter the Character Value:");
scanf("%c",&cvalue);

fputc(cvalue,fp);

fclose(fp);

//Read:
fp=fopen("one.txt","r");

crel=fgetc(fp);

printf("\n The Character value is :%c\n",crel);

fclose(fp);
//-------------------------------------------------------------------
//String Value Read & Write [fprintf & fscanf]
//Write:
fp=fopen("one.txt","w");

printf("\n Enter the String Value:");
scanf("%s",strvalue);

fprintf(fp,"%s",strvalue);

fclose(fp);

//Read:
fp=fopen("one.txt","r");

fscanf(fp,"%s",strrel);

printf("\n The String value is :%s\n",strrel);

fclose(fp);

//-------------------------------------------------------------------
//Structure Value Read & Write [fwrite & fread]
//Write:
fp=fopen("one.dat","wb");

printf("\nEnter the ID:");
scanf("%d",&svalue.id);

printf("\nEnter the Marks:");
scanf("%d",&svalue.marks);

fwrite(&svalue,sizeof(svalue),1,fp);

fclose(fp);

//Read:
fp=fopen("one.dat","rb");

fread(&srel,sizeof(srel),1,fp);

printf("\nThe ID is:%d\n",srel.id);
printf("\nThe Marks is:%d\n",srel.marks);

fclose(fp);
return 0;
}

Leave a comment

Filed under C Programming, File I/O

File I/O in C – Part 1

File I/O: We have been using the functions such as printf and scanf to read and write data. This works fine as long as data is small. The entire data is lost when either the program is terminated or the computer is turned off. So it is necessary to have more flexible approach where data can be stored on the disks and read whenever necessary, without destroying data. This concept is called files.

FILE is a structure declared in stdio.h . We have to use file pointer, a pointer variable that points to a structure FILE.

Part 1:

1. Explain File I/O & its need.

2. Major operations with Files in C.

a. Creating a File.

b. Opening a File.

c. Closing a File.

d. Reading & Writing data to and from a File.

3. Modes for File.

4. Code for writing and reading integer values from Files in C.


/*
Developed By:Prof. Vinod Pillai
vinodthebest@gmail.com
File in C - Part 1.
*/

#include

int main()
{
FILE *fp;
int value,rel;

//Creating new file with Write Mode
fp=fopen("one.txt","w");

printf("\n Enter the Value to write:");
scanf("%d",&value);

//Writing value to File.
putw(value,fp);

//Closing the File from Write Mode.
fclose(fp);

//Opening file with Read Mode
fp=fopen("one.txt","r");

//Reading value from File.
rel=getw(fp);

printf("\n The result is :%d",rel);

//Closing the File from Write Mode.
fclose(fp);

return 0;
}

3 Comments

Filed under C Programming, File I/O

Structure in C

Structure: Structure is user defined data type which is used to store heterogeneous data under unique name. Keyword ‘struct’ is used to declare structure. The variables which are declared inside the structure are called as ‘members of structure’.

I have explained about Structure in C:

1. Explain Structure & its need.

2. Code for creating simple Structure in C.

3. Code for creating variables of structure.

4. Nested structure.


/*
Developed By:Prof. Vinod Pillai
vinodthebest@gmail.com
Structure in C
*/

#include

//--------Structure Declaration---------
struct Student
{
int id;
int marks;
};

int main()
{
//Structure variables.
struct Student s1,s2;

//First Student Data
printf("\nEnter the ID:");
scanf("%d",&s1.id);

printf("\nEnter the Marks:");
scanf("%d",&s1.marks);

//Display Student Data
printf("\nThe ID is:%d",s1.id);
printf("\nThe Marks is:%d",s1.marks);

//--------Similarly Second Student Data---------

printf("\nEnter the ID:");
scanf("%d",&s2.id);

printf("\nEnter the Marks:");
scanf("%d",&s2.marks);

printf("\nThe ID is:%d",s2.id);
printf("\nThe Marks is:%d",s2.marks);

return 0;
}

Leave a comment

Filed under C Programming

Creating Simple Function in C++

Function: A function is a group of statements that together perform a task.

There are two major types of functions:

1. System inbuilt function.

2. User defined function.

System built in functions are the one which are by default being provided to us like the Math.sqrt() function is built in function. User defined functions are the one which are created by the users as per their requirement. In this session we are going to learn how to create the user defined functions.

    Different types of User defined Simple function in C++.

  • No argument and no return type function.
  • Argument and no return type function.
  • Argument and return type function.


/*
Developed By:Prof. Vinod Pillai
vinodthebest@gmail.com
Simple Function in C++
*/

#include

using namespace std;

//--------Function Declaration---------
void msgFunction();
void sumFunction(int val1,int val2);
int subFunction(int val1,int val2);

int main()
{

//--------Function Calling---------

msgFunction();

sumFunction(100,200);

int ans=subFunction(2000,1000);

cout<<"\n The return value from subFunction(2000,1000) is:"<<ans<<"\n";

return 0;
}

//--------Function Definition---------

//Simple Function with No Argument & No Return Type
void msgFunction()
{
cout<<"\n Welcome Message with Simple Function with no Argument & no Return type\n";
}

//Simple Function with Argument & No Return Type
void sumFunction(int val1,int val2)
{
int rel;
rel=val1+val2;
cout<<"\n The result of sumFunction(int,int) the result is:"<<rel<<"\n";
}

//Simple Function with Argument & Return Type
int subFunction(int val1,int val2)
{
int rel;
rel=val1-val2;
cout<<"\n The result of int subFunction(int,int) the result is:"<<rel<<"\n";
return rel;
}

Leave a comment

Filed under C++, Function

Creating Java Project in Eclipse IDE

Website for Eclipse IDE [http://eclipse.org/downloads/]

1. Creating Java Project in Eclipse.

2. Creating Package in Eclipse.

3. Creating class in Eclipse.

4. Running the java project in Eclipse.

Leave a comment

Filed under Java

Friend Class in C++

Friend Class:

C++ provides the friend keyword for major two purpose one for friend function and another for creating friend class. In this session we will talking about friend class that is inside a class, we can indicate that other classes will have direct access to protected and private members of the class. When granting access to a class, you must specify that the access is granted for a class using the class keyword:

friend class Faculty;

Note that friend declarations can go in either the public, private, or protected section of a class–it doesn’t matter where they appear. A class can also be declared to be the friend of some other class. When we create a friend class then all the member functions of the friend class also become the friend of the other class.


/*
Developed By: Prof. Vinod Pillai
vinodthebest@gmail.com
Friend Class.
*/

#include

using namespace std;

class A
{
int vala;

public:

void set(int tvala)
{
vala=tvala;
}
void display()
{
cout<<"\n The Value of a is:"<<vala;
}
friend class B;
};

class B
{
int valb;

public:

void set(int tvalb)
{
valb=tvalb;
}
void display()
{
cout<<"\n The Value of b is:"<<valb;
}

void sum(A ta)
{
valb=valb+ta.vala;
}
};

int main()
{
A aobj;

B bobj;

aobj.set(100);
bobj.set(200);

aobj.display();
bobj.display();

bobj.sum(aobj);
bobj.display();

return 0;
}

Leave a comment

Filed under C++

Constructor & Destructor in C++

Constructor:

A class constructor is a special member function of a class that is executed whenever we create new objects of that class its special because its name is same as class name.

A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.

Destructor:

A destructor is a special member function of a class that is executed whenever an object of it’s class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.

A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters. Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc.

#include

using namespace std;

class Student
{
int id;
int marks;

public:

Student()
{
id=0;
marks=0;
}

Student(int tid,int tmarks)

void display()
{
cout<<"\n ID is:"<<id;
cout<<"\n Marks is:"<<marks;
}

~Student()
{
cout<<"\n Destructor is Called - Object Destroyed \n";
}
};

Student::Student(int tid,int tmarks)
{
id=tid;
marks=tmarks;
}

int main()
{
Student s1;

s1.display();

Student s2(10,20);

s2.display();

return 0;
}

Leave a comment

Filed under C++