944,110 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1988
  • C RSS
Sep 30th, 2009
0

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

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
DARK_BYTE is offline Offline
50 posts
since Sep 2009
Sep 30th, 2009
0

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

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.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Sep 30th, 2009
0

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

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. }
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is offline Offline
2,197 posts
since Jan 2008
Sep 30th, 2009
-7

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Sep 30th, 2009
0

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

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.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Sep 30th, 2009
0

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

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
DARK_BYTE is offline Offline
50 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: i need help
Next Thread in C Forum Timeline: Solve the problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC