ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include "hotel.h"
void getname();
int
addguest(void)
{
int roomno;
off_t offset;
if ((roomno = findfree()) >= 1) {
getname();
offset = (roomno - 1) * NAMELENGTH;
if (lseek(infile, offset, SEEK_SET) == -1) {
perror("lseek");
exit(1);
}
write(infile,namebuf,NAMELENGTH);
} else
return -1;
return roomno;
}
void
getname()
{
int length = NAMELENGTH + 1;
while (length >= NAMELENGTH) {
printf("Enter Resident: ");
scanf("s", namebuf);
length = strlen(namebuf);
if (length >= NAMELENGTH) {
puts("Name is too long");
sleep(3);
}
}
/* fill buffer w/ zeros */
for (length; length < NAMELENGTH; length++)
namebuf[length] = ' ';
namebuf[NAMELENGTH-1] = '\0';
return;
}
#include "hotel.h"
const char *empty = "EMPTY ";
char namebuf[NAMELENGTH];
int
main(int argc, char **argv)
{
int j;
char *p;
infile = openfile();
/* printf("Get Occupier 1:\nFree Room 2:\nAddGuest 3:\nFindFree 4:\n\n\nChoose: */
if ( (j = addguest()) == -1)
;
else
printf("%s was added to room %d\n", namebuf, j);
return 0;
}
adduser is supposed to write over the text in a line that reads EMPTY. The problem is it writes the text from namebuf pushing the EMPTY to the right. I wrote a test program that didn't do that; it wrote over the text.
Actually each line is 21 bytes. So it appears that the name is being tacked on to the end but actually the line feed is being deleted. write(infile,namebuf,NAMELENGTH); writes 21 bytes, namebuf[21] is never set and the 21 byte of the line is a \n.
I will give you a hint that the getname function has a syntax error somewhere in the code you posted.
it is being written to file but pushes what was on that line to the write extending the limit of 20 chars
the scanf("s",namebuf) function was a typo. the program reads the way it should be: scanf("s",namebuf);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.