Help with this program that uses Unix I/O system calls

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 16
Reputation: DARK_BYTE is an unknown quantity at this point 
Solved Threads: 0
DARK_BYTE DARK_BYTE is offline Offline
Newbie Poster

Help with this program that uses Unix I/O system calls

 
0
  #1
Sep 30th, 2009
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:
  1. #include<stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include<unistd.h>
  6. #include<string.h>
  7.  
  8. struct Student
  9. {
  10. char name[21];
  11. char id[11];
  12. char DOB[9];
  13. char gender[2];
  14. char status[2];
  15. };
  16.  
  17. struct Student myArr[5];//An array of Struct Student
  18. int main()
  19. {
  20. int x, y, z;
  21. char newline[2] = { "\n" };
  22. char sep[5] = { " " };
  23. char buf1[] = { "Please enter the name of the student's:" };
  24. char buf2[] = { "Please enter the student's id:" };
  25. char buf3[] = { "Please enter the student's DOB:" };
  26. char buf4[] = { "Please enter the student's gender:" };
  27. char buf5[] = { "Please enter the student's marital status:" };
  28.  
  29. //O_CREAT requires a third argument the mode which specifies the access permission bits
  30. //S_IRWXU indicates that the user has read, write and execute permissions
  31. x = open( "/home/user/Desktop/Labsheets/Labsheet4/input.dat", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU );
  32. //fd 0 is standard input and 1 is standard output
  33. //will read from standard input and place data in the buffer
  34. //read return number of bytes read
  35. int i;
  36. for( i = 0; i < 5; i++ )
  37. {
  38. write( 1, buf1, strlen(buf1) );
  39. read( 0, myArr[i].name, 20 );//read from standard input and place data in buffer
  40. write( 1, buf2, strlen(buf2) );;
  41. read( 0, myArr[i].id, 10 );
  42. write( 1, buf3, strlen(buf3) );
  43. read( 0, myArr[i].DOB, 9 );
  44. write( 1, buf4, strlen(buf4) );
  45. read( 0, myArr[i].gender, 1 );
  46. write( 1, buf5, strlen(buf5) );
  47. read( 0, myArr[i].status, 1 );
  48. }
  49. for( i = 0; i < 5; i++ )
  50. {
  51. write( x, myArr[i].name, strlen(myArr[i].name) );
  52. write( x, sep, 4 );
  53. write( x, myArr[i].id, strlen(myArr[i].id) );
  54. write( x, sep, 4 );
  55. write( x, myArr[i].DOB, strlen(myArr[i].DOB) );
  56. write( x, sep, 4 );
  57. write( x, myArr[i].gender, strlen(myArr[i].gender) );
  58. write( x, sep, 4 );
  59. write( x, myArr[i].status, strlen(myArr[i].status) );
  60. write( x, newline, 4 );
  61.  
  62. }
  63.  
  64. return 0;
  65. }
Last edited by DARK_BYTE; Sep 30th, 2009 at 7:32 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz

Re: Help with this program that uses Unix I/O system calls

 
0
  #2
Sep 30th, 2009
its because u r taking only one character in the read for the gender. After u enter m/f when u hit enter key it actually takes the '\n' as an input for the next read statement. Hence the next read() is skipped.

  1. write( 1, buf4, strlen(buf4) );
  2. read( 0, myArr[i].gender, 1 );
  3. write( 1, buf5, strlen(buf5) );
  4. read( 0, myArr[i].status, 1 );

correct your code as
  1. write( 1, buf4, strlen(buf4) );
  2. read( 0, myArr[i].gender, 2 );// replaced 1 with 2
  3. write( 1, buf5, strlen(buf5) );
  4. read( 0, myArr[i].status, 2 );// replaced 1 with 2

Also increase the buffer size for gender and status by 1.

it will work...........
Last edited by dkalita; Sep 30th, 2009 at 9:18 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 421
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 48
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Pro in Training

Re: Help with this program that uses Unix I/O system calls

 
0
  #3
Sep 30th, 2009
This works..maybe you can figure out why

  1. #include<stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include<unistd.h>
  6. #include<string.h>
  7.  
  8. struct Student
  9. {
  10. char name[21];
  11. char id[11];
  12. char DOB[9];
  13. char gender[2];
  14. char status[2];
  15. };
  16.  
  17. struct Student myArr[5];//An array of Struct Student
  18. int main()
  19. {
  20. int x, y, z;
  21. char dumb[1];
  22. char newline[2] = { "\n" };
  23. char sep[5] = { " " };
  24. char buf1[] = { "Please enter the name of the student's:" };
  25. char buf2[] = { "Please enter the student's id:" };
  26. char buf3[] = { "Please enter the student's DOB:" };
  27. char buf4[] = { "Please enter the student's gender:" };
  28. char buf5[] = { "Please enter the student's marital status:" };
  29.  
  30. //O_CREAT requires a third argument the mode which specifies the access permission bits
  31. //S_IRWXU indicates that the user has read, write and execute permissions
  32. x = open( "/home/user/Desktop/Labsheets/Labsheet4/input.dat", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU );
  33. //fd 0 is standard input and 1 is standard output
  34. //will read from standard input and place data in the buffer
  35. //read return number of bytes read
  36. int i;
  37. for( i = 0; i < 5; i++ )
  38. {
  39. write( 1, buf1, strlen(buf1) );
  40. read( 0, myArr[i].name, 20 );//read from standard input and place data in buffer
  41. write( 1, buf2, strlen(buf2) );;
  42. read( 0, myArr[i].id, 10 );
  43. write( 1, buf3, strlen(buf3) );
  44. read( 0, myArr[i].DOB, 9 );
  45. write( 1, buf4, strlen(buf4) );
  46. read( 0, myArr[i].gender, 1 );
  47. read( 0, dumb, 1 );
  48. write( 1, buf5, strlen(buf5) );
  49. read( 0, myArr[i].status, 1 );
  50. read( 0, dumb, 1 );
  51. }
  52. for( i = 0; i < 5; i++ )
  53. {
  54. write( x, myArr[i].name, strlen(myArr[i].name) );
  55. write( x, sep, 4 );
  56. write( x, myArr[i].id, strlen(myArr[i].id) );
  57. write( x, sep, 4 );
  58. write( x, myArr[i].DOB, strlen(myArr[i].DOB) );
  59. write( x, sep, 4 );
  60. write( x, myArr[i].gender, strlen(myArr[i].gender) );
  61. write( x, sep, 4 );
  62. write( x, myArr[i].status, strlen(myArr[i].status) );
  63. write( x, newline, 4 );
  64.  
  65. }
  66.  
  67. return 0;
  68. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,620
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Help with this program that uses Unix I/O system calls

 
-7
  #4
Sep 30th, 2009
1) suggest you define macros STDIN and STDOU instead of hardcoding 0 and 1 in the read/write statements
  1. #define STDOUT 1
  2. #define STDIN 0

2) use sizeof operator to get the size of data
  1. write( STDOUT, buf4, strlen(buf4) );
  2. read( STDIN, myArr[i].gender, sizeof(myArr[i].gender) );
  3. write( STDOUT, buf5, strlen(buf5) );
  4. read( STDIN, myArr[i].status, sizeof(myArr[i].status) );


[edit] And what they said above ^^^^ [/edit]
Last edited by Ancient Dragon; Sep 30th, 2009 at 9:22 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz

Re: Help with this program that uses Unix I/O system calls

 
0
  #5
Sep 30th, 2009
Originally Posted by Ancient Dragon View Post
1) suggest you define macros STDIN and STDOU instead of hardcoding 0 and 1 in the read/write statements
  1. #define STDOUT 1
  2. #define STDIN 0

2) use sizeof operator to get the size of data
  1. write( STDOUT, buf4, strlen(buf4) );
  2. read( STDIN, myArr[i].gender, sizeof(myArr[i].gender) );
  3. write( STDOUT, buf5, strlen(buf5) );
  4. read( STDIN, myArr[i].status, sizeof(myArr[i].status) );


[edit] And what they said above ^^^^ [/edit]
Hi,
Please consider the following example code
  1. char a, c;
  2. scanf("%c", &c);
  3. scanf("%c", &a);
  4. //printf("[c:%c, a:%c]\n", c, a);

when we run it the second scanf() statement takes the '\n' that is entered while entering a character for the first scanf().
It may be because the input buffer is not reset after the first scanf().
But I tried doing an fflush(stdin) before the second scanf() and it still doesn't take the second input.
Can u put some light on this issue.

Thanks in advance
Last edited by dkalita; Sep 30th, 2009 at 9:39 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 16
Reputation: DARK_BYTE is an unknown quantity at this point 
Solved Threads: 0
DARK_BYTE DARK_BYTE is offline Offline
Newbie Poster

Re: Help with this program that uses Unix I/O system calls

 
0
  #6
Sep 30th, 2009
Ok I understand the newline thingy since it is the same thing as with C++ when we use cin.get in C++ but then I am lost again do I need to cater for the null terminator lets say as in C++ I want to input a string of 20 characters in an array then I will have to use an array whose size is 21; that is I need to cater for the null terminator
C++:
char name[21]
cin.get( name, 21 );//cater for 20 characters plus null terminator
cin.get()//read newline left in input queue

What about read()?
with read() if i want to read a name of 20 characters do i need to write it like this read( 0, myArr[i].name, 21 ) - in this case catering only for the newline

or
read( myArr[i], name, 22 ) - in this case catering for both the newline and the null terminator
Last edited by DARK_BYTE; Sep 30th, 2009 at 10:46 am.
Reply With Quote Quick reply to this message  
Reply

Tags
i/o, unix

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for i/o, unix
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC