Open file for read/write operation in binary mode

# include <stdio.h> # include <conio.h> # include <process.h>   void main() { FILE *fp; char c=’ ‘; clrscr(); fp=fopen("mydata.dat","wb"); if (fp==NULL) { printf("Can not open file"); exit(1); } printf("Press ‘.’ to exit:"); while(c!=’.’) { c=getche(); fputc(c,fp); } fclose(fp); fp=fopen("mydata.dat","rb");   printf("\n Contents read :"); while(!feof(fp)) printf("%c",getc(fp)); }
Continue reading…

Enjoyed this post? Share it!

 
 

Open a file in append mode in C

# include <stdio.h> # include <conio.h> # include <process.h>   void main() { FILE *fp; char c=’ ‘; clrscr(); fp=fopen("mydata.txt","a+"); if(fp==NULL) { printf("Can not open file"); exit(1); } printf("Press ‘.’ to exit:"); while(c!=’.’) { c=getche(); fputc(c,fp); } printf("\n Contents read :"); rewind(fp); while(!feof(fp)) printf("%c",getc(fp)); }
Continue reading…

Enjoyed this post? Share it!

 
 

Use w+ mode for writing and reading a file in C

# include <stdio.h> # include <conio.h> # include <process.h> void main() { FILE *fp; char c=’ ‘; clrscr(); fp=fopen("mydata.txt","w+"); if(fp==NULL) { printf("Can not open file"); exit(1); } printf("press ‘.’ to exit :"); while(c!=’.’) { c=getche(); fputc(c,fp); } rewind(fp); printf("\n Contents read :");   while(!feof(fp)) printf("%c",getc(fp)); }
Continue reading…

Enjoyed this post? Share it!

 
 

Open pre-existing file and append string to file in C

# include <stdio.h> # include <conio.h> # include <process.h> void main() { FILE *fp; char c; clrscr();   printf("File before appending :\n");   fp=fopen("mydata.txt","r"); while(!feof(fp)) { c=fgetc(fp); printf("%c",c); } fp=fopen("mydata.txt","a");   if(fp==NULL) { printf("File can not appended"); exit(1); } printf("\n Enter string to append :");   while(c!=’.’) { c=getche(); fputc(c,fp); } fclose(fp);   printf("\n File […]
Continue reading…

Enjoyed this post? Share it!

 
 

program to write data to text file and read in C

# include <stdio.h> # include <conio.h> # include <process.h>   void main() { FILE *fp; char c=’ ‘; clrscr(); fp=fopen("mydata.txt","w"); if(fp==NULL) { printf("Can not open file mydata.txt"); exit(1); } printf("Write data, press ‘-‘ to exit:"); while(c!=’-‘) { c=getche(); fputc(c,fp); } fclose(fp); printf("\n Contents read :"); fp=fopen("mydata.txt","r"); while(!feof(fp)) printf("%c",getc(fp)); }
Continue reading…

Enjoyed this post? Share it!

 
 

Insertion sort example in C

/* */ # define n 6 int x[]={4, 3, 6, 2, 8, 9}; main( ) { int I, t, j,. printf(“\n\n Input data: \n \n”); for(I=0; I<n; I++) printf(*%3d*, x[I]); printf(*\n*); for I=1; I<n; I++) for(j=1, j>0 && x[j]<x[j-1]; j — —) { t=x[j]; x[j]=x[j-1]; x[j-1]=t; } printf(“\n\n Output data: \n \n”); for(I=0; I<6; I++) […]
Continue reading…

Enjoyed this post? Share it!

 
 

Size of array in C

#include <stdio.h> #define N 5 /* Size of array */ main( ) { int A[N]; /* Space for A[0]………A[4] is allocated */ int j,sum=0; for(i=0; i<N; i++) A[i]=i*i; /* Initialize the array */ for(i=0;i<N; i++) /* Display array elements */ printf(“ A[%d]=%d”, i, A [i] ); for(i =0; i <N; ++i) /* Find total */ […]
Continue reading…

Enjoyed this post? Share it!

 
 

Auto variables in different blocks in C

void main() { int p=100; clrscr(); printf("\n P=%d",p); { int p=50; printf("\n P=%d",p); } { int p=40; printf("\n P=%d",p); } printf("\n P=%d",p); } Output P=100 P=50 P=40 P=100 In the above program, the declaration of ‘p’ is made at three places with different values. In the first block, the value of ‘p’ is 100 and […]
Continue reading…

Enjoyed this post? Share it!

 
 

Automatic Variables in C

auto variables are defined inside a function. A variable declared inside the function without storage class name is, by default, an auto variable. These functions are declared on the stack. The stack provides temporary storage. The scope of the variables is local to the block in which they are defined. These variables are available only […]
Continue reading…

Enjoyed this post? Share it!

 
 

Access individual elements of array by pointers

Access individual elements of array by pointers In the below program, an integer array num is declared and initialized. ptr is an integer pointer. Pointer ptr points to the num by the statement ptr=num;. We can access every element of an array with the pointer. void main() { int i,num[5]={10,20,30,40,50}; int * ptr; clrscr(); ptr=num; […]
Continue reading…

Enjoyed this post? Share it!

 
 

Stack implementation with operator overloading in C++

#include <conio.h> #include <iostream.h> #include <dos.h> #include <stdio.h> #include <conio.h>   int top=0;   //class definition   class stack { public: int a[]; int n;   stack (); stack operator << (int); stack operator + (int); stack operator –(void); }r;   //definig the constructor to initialize   stack :: stack () { clrscr(); cout <<"Enter […]
Continue reading…

Enjoyed this post? Share it!

 
 

Quicksort of array in c++

#include <iostream.h> #include <conio.h> #define SIZE 10   template <class T> class QSORT { private: T Array[SIZE]; int len; public: QSORT(int); //constructor void getArray(void); void quickSort(int,int); int partition(int, int); void printArray(void); }; //End of QSORT class   template <class T> QSORT <T> :: QSORT(int length) { len = length;   //Initialize all array elements to […]
Continue reading…

Enjoyed this post? Share it!

 
 

Operations on Linked Lists in C++

#include <iostream.h> #include <conio.h>   class listNode { public: int data; listNode *ptr; };   class linkedList { private: listNode *head;   public: linkedList() //constructor { head = NULL; } void insertFront(void); int deleteFront(void); void showList(void);   ~linkedList() //destructor { delete head; } }; //End of linkedList   void linkedList::insertFront(void) { listNode *newNode; int item; […]
Continue reading…

Enjoyed this post? Share it!

 
 

Add two complex objects and add real part to a complex object in C++

#include <iostream.h> #include <conio.h>   class COMPLEX { private: int realp; int imgp; public: void readValues(void) { cout<<"Enter the real part and imaginary part"<<endl; cin>>realp>>imgp; }   friend COMPLEX ADD(COMPLEX,COMPLEX); friend COMPLEX ADD(int,COMPLEX); friend ostream& operator << (ostream&, COMPLEX&); };   COMPLEX ADD(COMPLEX s1, COMPLEX s2) //To add two COMPLEX objects { COMPLEX c; c.realp […]
Continue reading…

Enjoyed this post? Share it!