Programing C

File Handling In C

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

FILE *fp;

char ch;

int i=0;

char str[100]="";

fp=fopen("c:\\Answer.txt","r"); //Opening files in read only mode

while((ch=getc(fp))!=EOF) // Running while loop until EOF file

{

printf("%c",ch);

str[i]=ch;

i++;

}

str[i]='\0';

fclose(fp); //Closing the file Pointer

strcat(str,"Dutta");

fp=fopen("c:\\Answer.txt","w+"); // Opening file in write/append mode.

//while(1)

//{

fputs(str,fp); // Writing in the file

//}

fclose(fp); // Closing File pointer

}

//Function to generate random no

int randint(int i)

{

int rno;

srand((unsigned)(time(NULL)));

rno=rand()%i;

if (rno==0)

rno++;

fprintf(stderr,"Random No => %d \n",rno);

return rno;

}

PROCESS TO FIND THE DATE

#include<string.h>

#include<stdio.h>

#include<time.h>

#include <StdLib.h>

#include<windows.h>

void main()

{

struct tm *newtime;

char datebuf[10],

timebuf[10],

dd[3]="X",

mm[3]="X",

yy[5]="X",

hh[3]="X",

mi[3]="X",

ss[3]="X",

currentDate[15]="",

CurrentTime[15]="",

tempDate[15]="";

time_t long_time;

_strdate(datebuf);

_strtime(timebuf);

_strdate(CurrentDate);

_strtime(CurrentTime);

time( &long_time ); /* Get time as long integer. */

newtime = localtime( &long_time ); /* Convert to local time. */

mm[0]=CurrentDate[0];

mm[1]=CurrentDate[1];

mm[2]='\0';

dd[0]=CurrentDate[3];

dd[1]=CurrentDate[4];

dd[2]='\0';

yy[0]='2';

yy[1]='0';

yy[2]=CurrentDate[6];

yy[3]=CurrentDate[7];

yy[4]='\0';

hh[0]=CurrentTime[0];

hh[1]=CurrentTime[1];

hh[2]='\0';

mi[0]=CurrentTime[3];

mi[1]=CurrentTime[4];

mi[2]='\0';

ss[0]=CurrentTime[6];

ss[1]=CurrentTime[7];

ss[2]='\0';

strcpy(tempDate,"");

strcpy(tempDate,yy);

strcat(tempDate,mm);

strcat(tempDate,dd);

strcat(tempDate,hh);

strcat(tempDate,mi);

strcat(tempDate,ss);

printf("STARTDATE=> %s\n",tempDate);

}

////// PROCESS TO FIND DAY OF A WEEK

struct tm *newtime;

time_t long_time;

char DayName[15]="X";


int day=-1;

char datebuf[10],timebuf[10];

_strdate(datebuf);

_strtime(timebuf);

time( &long_time );

newtime = localtime( &long_time );

day=newtime->tm_wday;

printf("Day is = %d :: day = %d \n",newtime->tm_wday,day);

switch(day)

{

case 0: printf("Sunday");

break;

case 1: printf("Monday");

break;

case 2: printf("Tuesday");

break;

case 3: printf("Wednesday");

break;

case 4: printf("Thrusday");

break;

case 5: printf("Friday");

break;

case 6: printf("Saturday");

break;

}

printf("\n");

}

/// GetPrivateProfileString ////////////// Same as Windows API to read ini or cfg pattern file

Parameters are SECTION, KEY ,DEFAULTVALUE ( if key doesn't match), ReturnedString(value if key found), Path of the file.

void GetPrivateProfileString(char section[],char key[],char defaultval[],char *ReturnedString,char fPath[])

{

FILE *fp;

int j=0;

fp=fopen(fPath,"r");

fprintf(stderr,"section=>%s :: key=>%s,fPath=>%s\n",section,key,fPath);

if(fp!=NULL)

{

if(!feof(fp)) {

do {

int i = 0;

char ch;

ch = fgetc(fp);

if(ch == '[') {

char temp[300];

do {

ch = fgetc(fp);

if((ch != '[') && (ch != ']') && ch != '\n' && (int)ch!=32 && (int)ch!=13)

{

temp[i++] = ch;

//printf("%s\n",temp);

}

else

continue;

}while((ch!='\n') && (ch!='\t') && (ch!=' ') && (int)ch!=13 && (!feof(fp)));

temp[i] = '\0';

if(!strcmp(section,temp)) // checking the section

{

//printf("SECTION FOUND");

i = 0;

do {

ch = fgetc(fp);

if(ch != '\n' && ch != '[' && ch != '=' && (int)ch!=32 && (int)ch!=13)

temp[i++] = ch;

else {

temp[i] = '\0';

if(ch == '[' || feof(fp) || !strcmp(key,temp))

break;

else {

i = 0;

continue;

}

}

}while((!feof(fp)) && (ch != '['));

}else

continue;

//printf("KEY => %s\n",key);

if(!strcmp(key,temp)) {

i = 0;

do{

ch = fgetc(fp);

if(ch != '\n' && (int)ch!=32 && (int)ch!=13)

temp[i++] = ch;

//fprintf(stderr,"%s\n",temp);

}while(!feof(fp) && ch != '\n' && (int)ch!=32 && (int)ch!=13 );

temp[i] = '\0';

//printf("temp =>%d\n",strlen(temp));

strcpy(ReturnedString,temp);

}else

{

strcpy(ReturnedString,defaultval);

}

break;

}

}while(!feof(fp));

}

fclose(fp);

}

}