Important program in C

, , No Comments
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct emp{

char name[10];
int age;

};

File handling concept


void NameAndAgeStoring(){

struct emp e;
FILE *p, *q;
p= fopen("list.txt","a");
q= fopen("list.txt","r");

printf("enter the name and age");
scanf("%s %d",e.name,e.age);
fprintf(p,"%s %d",e.name,e.age);
fclose(p);

do{

fscanf(q,"%s %d",e.name,e.age);
printf("%s %d",e.name,e.age);
}
while(!feof(q));
getch();
}

File handling concept - Create and store value

void file()
{
 FILE *fp;
 char ch;
 fp = fopen("names.txt", "w");
  while( ( ch= getchar())!=EOF){

 putc(ch,fp);
  }
  fclose(fp);
}

string length without strlen()

void stringCal(){

int i;
char str[1000];
printf("enter string value \n");
scanf("%s",str);
for(i=0; str[i] != '\0';++i){

}
printf(" number of char entered %d",i);
getch();
}

string reverse using pointer concept

int stringReverse(){
char str[1000], *ptr;
int i, len;
printf("enter string \n");

gets(str);

ptr = str;
for(i=0; i <1000;i++)
{
if(*ptr == '\0'){
break;


}
ptr++;
}

len=i;
ptr--;
printf("revered string: ");

for(i=len; i>0; i--){

printf("%c",*ptr--);

}
getch();
return 0;

}

search name in file - file concept

void SearchName(){

FILE *fp;
char ch[10],t[10], rep[10];
int flag=0;
printf("enter the value to search \n");
gets(t);
fp = fopen("names.dat","a+");
//tp =fopen("names.dat","w+");
do{

fscanf(fp,"%s\n",ch);
if(strcmp(ch,t) == 0){
//printf("%s\n",ch);
printf("name found, enter the replacement value \n");
gets(rep);

flag=1;
fseek(fp,0,SEEK_SET);
fprintf(fp,"%s\n",rep);
fclose(fp);

break;
}else{
//printf("not found \n");
}
}
while(!feof(fp));
fclose(fp);
if(flag==0)
{
printf("name not matched \n");
}else
{
printf("name replaced successfully \n");
}
getch();
}

0 टिप्पणियाँ:

Post a Comment