More files Handling

August 5, 2008

Write a program that opens a file called “input.dat”. If the file does not exist, the programme creates it. If it exists already, the programme will simply overwrite it. Your programme should then read data for 5 students from the terminal and write them into the file. Your data should consist of the names of students (20 characters), a student id (10 characters), date of birth (8 characters), gender (1 character) and marital status (1 character).

*/
        #include <stdio.h>
        #include <sys/types.h>
        #include <sys/stat.h>
        #include <fcntl.h>
        #include <unistd.h>
        #include <string.h>

        struct Student{

            char snames[20];
            int  sid[10];
            char sdob[8];
            char sgender;
             char sstatus;
         };

          void inputstud(struct Student *student);
          void printdetails(struct Student *student);
          void writetofile(struct Student student[]);

       int main(){
             const int max=5;
             struct Student s[max];

             int fil=open("input.dat",O_RDWR | O_CREAT);

              inputstud(&s[0]);
              printf("\n");
              printdetails(&s[0]);

            // writetofile(&s[0],1);
              writetofile(s);
            return 0;
         }

void inputstud(struct Student *student){
          printf("Enter Name:\t");
          scanf("%s",student->snames);
          
          printf("Enter Id:\t");
          scanf("%d",student->sid);
          printf("Enter Date of Birth:\t");
          scanf("%s",student->sdob);
          printf("Enter Gender:\t");
          scanf("%s",&student->sgender);
          printf("Enter Status:\t");
          scanf("%s",&student->sstatus);

}

void printdetails(struct Student *student){
          printf("Name:is %s\n",student->snames);
          printf("Id:is %s\n",student->sid);
          printf("Dateof Birth:is %s\n",student->sdob);
          printf("Gender:is %c\n",student->sgender);
          printf("Status:is %c\n",student->sstatus);

}

void writetofile(struct Student student[]){

    int fil=open("input.dat",O_RDWR | O_CREAT);

   if (fil==-1)
    {
        printf("error has occures");
    }
 else

   {
       printf("Name written is%d  ",write(fil,student->snames,strlen(student->snames)));
       printf("Id written is%d  ",write(fil,student->sid,10));
       printf("Date written is%d  ",write(fil,student->sdob,strlen(student->sdob)));
// printf("Gender written is%d  ",write(fil,student->sgender,1));
//printf("Status written is%d  ",write(fil,student->sstatus,strlen(student.sstatus)));

}

}

pipes example1

August 5, 2008
        #include <stdio.h>
        #include <stdlib.h>

      int main()
            {
               int fd[2];
               int p;
               int n;
               int x;
               char line[20];

               x=pipe(fd);
                p=fork();

           if (p==0)
            {
              printf("Hello- I'm the child\n");
              close(fd[1]);
              n=read(fd[0],line,12);
              printf("Child - Line read was %s \n", line); }
          else
               {
                 close(fd[0]);
                 write(fd[1],"hello world \n",12);
               }

                return 0;
          }//main

Link to linux Resources

July 4, 2008

A very nice place for tutorial :

http://linux.die.net/man/3/readdir

Special thanks for all thse codes published goes to sydney,kooshal, yannick, vanessen and vikesh and all those who contributed .


using structures loops etc

July 4, 2008

Write a program that performs the following:
It creates an array of a structure student (What the structure contains is your choice).
It executes a loop that displays a menu as follows:
1.Add new student
2.Modify a student information
3.Display Array
4.Exit
It allows the user to choose one of the above menu options.
If the user chooses option 1, the program allows the input of the student information, builds a structure, create a thread passing the created structure as argument. The thread simply adds the structure to the array.
If the user chooses option 2, the program allows the entry of a key (such as student id), creates a thread passing the key as argument. The thread requests for the field to be modified and the new value and makes the required modification.
If the user chooses option 3, the program creates a thread that displays all filled elements of the array.


In each of the above cases the thread exits after having performed its task.

————————————————————————————

# include<stdio.h>
//# include<pthread.h>

struct student{
char name[15];
int id;
}//end structure

void printMenu();
void * funct1(void* arg);
void * funct2(void * arg);
void * funct3(void* k1);

struct student s[10];
int num=0;
// main
int main(){
int x=0;

//——–menu
printMenu();
while(scanf(“%d”,x)){

//———————first choice
if(x==1){
struct student s1;
printf(“enter name:\n”);
scanf(“%s”,s1.name);
printf(“enter id:\n”);
scanf(“%d”,s1.id);
pthread_t p;
pthread_create(&p,NULL,funct1,(void*)&s1);
num++;
printMenu();
}//endif

//——– ———–second choise

else if(x==2){
int k=0;
printf(“enter the key\n”);
scanf(“%d”,k);
pthread_t p1;
pthread_create(&p1,NULL,funct2,(void*)&k);
printmenu();

}//end elseif

//——————–third choise

else if(x==3){
pthread_t p3;
pthread_create(&p3,NULL,funct3,(void*)&x);
printMenu();

}//ending else if
else
exit(0);
}//while
}//main

//——————————– function 1———————-
void * funct1(void* arg){

char *nm;
int *i1;
nm=(*char)arg.name;
i1=(*int)arg.id;
s[num-1].name=(*nm);
s[num-1].id=(*i1);

}//end function

//——————————–function 2———————–
void * funct2(void* k1){

printf(“which modification to make:/n1.id/n2.name/nchoice:”);
scanf(“%d”,x);

int *ids;
ids=(*int)k1;
int idd=ids;
int i=0;
int break_while=0;

if(x==1){
printf(“enter new id:”);
int idi;
scanf(“%d”,idi);

while(break_while==0){

if(s[i].id==idd){
s[i].id=idi;
break_while=1;}//if
else
i++;

}//while

}//if x=1

else{
printf(“enter new name:”);
char nm[15];
scanf(“%s”,nm);

while(break_while==0){

if(s[i].id==idd){
s[i].name=nm;
break_while=1;}//if
else
i++;

}//while

}//else;

}//end function 2
//—————————-function 3———————
void * funct3(void* k1){
int i=0;

for(i;i<num;i++){
printf(“\nstudent:”,i);
printf(“\nstudent id:”,s[i].id);
printf(“\nstudent name:”,s[i].name);

}//end i

}//end funct3

//—————————- function printMenu
void printMenu(){
printf(“1. Add new student\n”);
printf(“2.Modify a student Information\n”);
printf(“3.Display array\n”);
printf(“4.exit\n”);

}//


using signals,along with pipes and files

July 4, 2008

#include <stdio.h>
#include <stdlib.h>
# include<fcntl.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>

struct itimerval timer;
int which = ITIMER_REAL;
int fd[2];
char line [100];
int timex;

void  f0();
void f1();

int main(){
pipe(fd);
int p=fork();

int t=rand();
t=(t % 1000);
printf(“time interval:%d”,t);

struct itimerval v;
v.it_interval.tv_sec = 0;
v.it_interval.tv_usec = t;  /* random milliseconds */
v.it_value.tv_sec = 0;           /* Zero seconds */
v.it_value.tv_usec = t;

if (p==0){

int f=open(“intext.txt”,O_RDONLY);
int i=0;

//–  the loop —
for(i;i< 5;i++){
int t=rand();
t=(t % 1000);
timex=* t;
read(f,line,5);
line[6]=i;

signal(SIGALRM,f0);
setitimer( which, &v, NULL );
int k=0;
for(k;k<5;k++) pause();

}//for
}//if

else{
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec =500000;  /* five hundred milliseconds */
timer.it_value.tv_sec = 0;           /* Zero seconds */
timer.it_value.tv_usec =500000;

signal(SIGALRM,f1);
setitimer( which, &timer, NULL );
int j=0;
for(j;j<5;j++){ pause();}

}//else
return 1;
}//main

void  f0(){
close (fd[0]);
write(fd[1],line,6);
printf(“writing in pipe at random time %d\n”,timex);

}

void  f1(){
int z= open(“outext.txt”,O_WRONLY|O_APPEND);

char l[50];
close (fd[1]);
int s=read(fd[0],l,6);
write(z,l,6);
if(s>0){
printf(“writing in output file \n”);}

else{ printf(“not read\n”);}

}


pipes reading from a file and writing to another one

July 4, 2008

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>

#define MAX 20

int main(int argc, char *argv[])
{
int fd[2];
int p;
int n;
int x;
char line[MAX];

int r = open(argv[0], O_RDONLY);
read(r, line, MAX);

int w = open(argv[1], O_WRONLY | O_CREAT);

x=pipe(fd);
p=fork();

if (p==0)
{
close(fd[1]);
n=read(fd[0],line,MAX);
write(w, fd[0], MAX);
}
else
{ close(fd[0]);
write(fd[1], line ,MAX);
}

return 0;
}


pipes to read and write to a file

July 4, 2008

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>

#define MAX 20

int main()
{
int fd[2];
int p;
int n;
int x;
char line[MAX];

int r = open(“myfile.txt”, O_RDONLY);
read(r, line, MAX);

x=pipe(fd);
p=fork();

if (p==0)
{ printf(“Hello- I’m the child\n”);
close(fd[1]);
n=read(fd[0],line,MAX);
printf(“Child – Line read was %s \n”, line); }
else
{ close(fd[0]);
write(fd[1], line ,MAX);
}

return 0;
}


pipes example1

July 4, 2008

#include #include int main() { int fd[2]; int p; int n; int x; char line[20]; x=pipe(fd); p=fork(); if (p==0) { printf(“Hello- I’m the child\n”); close(fd[1]); n=read(fd[0],line,12); printf(“Child – Line read was %s \n”, line); } else { close(fd[0]); write(fd[1],”hello world \n”,12); } return 0; }


use_of _library

July 4, 2008

# include<stdio.h>
# include “libfunction.h”
int main(){
int ans;
int r;
int b;
int p;

//hello();

printf(“\nWhat do you want to do?\n1.power\n2.Factorial”);

scanf(“%d”,&ans);

if(ans==1){

printf(“Enter base:”);
scanf(“%d”,&b);
printf(“Enter power:”);
scanf(“%d”,&p);

r=powerOf(b,p);
printf(“result is:%d”,r);

}//endng 1

else{
printf(“Enter number:”);
scanf(“%d”,&b);
r=fact(b);
printf(“factorial is:”,r);

}//ending else
}//ending main

—————————————————————————————

// this is code for libfunction.h
void hello(){
printf(“hello how are you?”);

}

int powerOf(int base,int power ){
int i=0;
int result=1;
for(i;i<power;i++)
result=result*base;

return result;
}

int fact(int x){

if(x>1)
return(x * fact(x-1));

else
return 1;
}


another example

July 4, 2008

#include <stdio.h>
#include <pthread.h>

#define MAX    10

struct Student
{
int id;
char name[50];
};

int cursor = 0;
struct Student student[MAX];

int displayMenu();
void addNewStudent(struct Student * stu);
void * copyStudent(void *arg);
void display();

int main()
{
pthread_t threadid;
int c = 1;
struct Student  stu;
while((c >= 0) && (c <= 4))
{
c = displayMenu();
switch(c)
{
case 1:
addNewStudent(&stu);
pthread_create(&threadid, NULL, copyStudent, (void *) &stu);

break;
case 3:
display();
break;
default:
return 0;
}
}
return 0;
}

int displayMenu()
{
printf(“1: Add new student\n”);
printf(“2: Modify a student\n”);
printf(“3: Display Array\n”);
printf(“4: Exit\n\n”);

int c;
printf(“Enter choice: “);
scanf(“%d”, &c);
return c;
}

void addNewStudent(struct Student * stu)
{
if(cursor < MAX)
{
printf(“name:”);
scanf(“%s”, stu->name);
stu->id = cursor;
}
}

void * copyStudent(void *arg)
{
struct Student * stu = (struct Student *)arg;
student[cursor++] = *stu;
//thr_exit((void *) 0);
}

void display()
{
int i;
for(i = 0; i < cursor; i++)
{
printf(“id: %d\n”, student[i].id);
printf(“name: %s\n\n”, student[i].name);
}
}


Follow

Get every new post delivered to your Inbox.