I'm writing a program that needs to use only the Unix system calls open, read and write to get and print data!The problem is that the program doesn't allow me to input the marital status of the first student!
Am I missing something?
Here is the code:
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<unistd.h>
#include<string.h>
struct Student
{
char name[21];
char id[11];
char DOB[9];
char gender[2];
char status[2];
};
struct Student myArr[5];//An array of Struct Student
int main()
{
int x, y, z;
char newline[2] = { "\n" };
char sep[5] = { " " };
char buf1[] = { "Please enter the name of the student's:" };
char buf2[] = { "Please enter the student's id:" };
char buf3[] = { "Please enter the student's DOB:" };
char buf4[] = { "Please enter the student's gender:" };
char buf5[] = { "Please enter the student's marital status:" };
//O_CREAT requires a third argument the mode which specifies the access permission bits
//S_IRWXU indicates that the user has read, write and execute permissions
x = open( "/home/user/Desktop/Labsheets/Labsheet4/input.dat", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU );
//fd 0 is standard input and 1 is standard output
//will read from standard input and place data in the buffer
//read return number of bytes read
int i;
for( i = 0; i < 5; i++ )
{
write( 1, buf1, strlen(buf1) );
read( 0, myArr[i].name, 20 );//read from standard input and place data in buffer
write( 1, buf2, strlen(buf2) );;
read( 0, myArr[i].id, 10 );
write( 1, buf3, strlen(buf3) );
read( 0, myArr[i].DOB, 9 );
write( 1, buf4, strlen(buf4) );
read( 0, myArr[i].gender, 1 );
write( 1, buf5, strlen(buf5) );
read( 0, myArr[i].status, 1 );
}
for( i = 0; i < 5; i++ )
{
write( x, myArr[i].name, strlen(myArr[i].name) );
write( x, sep, 4 );
write( x, myArr[i].id, strlen(myArr[i].id) );
write( x, sep, 4 );
write( x, myArr[i].DOB, strlen(myArr[i].DOB) );
write( x, sep, 4 );
write( x, myArr[i].gender, strlen(myArr[i].gender) );
write( x, sep, 4 );
write( x, myArr[i].status, strlen(myArr[i].status) );
write( x, newline, 4 );
}
return 0;
}