I'm trying to complete an excercise to learn unix system calls and one of the functions in the program was giving me only partial output. So I wrote a test program that is almost identical to the one linked with the rest of the program that works fine. I've gone over this for a solid day. Anyway, here's the code to what works:
Code:
#include "hotel.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define LNAME 22
#define FNAME 19
char buf[NLINES];
void gname(int, int *);
int
main()
{
int index = 0;
printf("Enter last name: ");
gname(LNAME, &index);
printf("Enter first name: ");
gname(FNAME, &index);
printf("%s", buf);
}
gname(int i, int *index)
{
int j, count = *index;
char c;
if (i == LNAME)
j = i - 2;
else if (i == FNAME)
j = (LNAME + FNAME) - 1;
while ( (c = getchar()) != '\n' && c != EOF && count < j) {
buf[count] = c;
count++;
}
if (i == LNAME) {
buf[count] = ',';
buf[++count] = ' ';
*index = ++count;
}
if (i == FNAME) {
for (count; count < j; ++count)
buf[count] = ' ';
buf[count] = '\n';
}
}
gname(int i, int *index)
{
int j, count = *index;
char c;
if (i == LNAME)
j = i - 2;
else if (i == FNAME)
j = (LNAME + FNAME) - 1;
while ( (c = getchar()) != '\n' && c != EOF && count < j) {
buf[count] = c;
count++;
}
if (i == LNAME) {
buf[count] = ',';
buf[++count] = ' ';
*index = ++count;
}
if (i == FNAME) {
for (count; count < j; ++count)
buf[count] = ' ';
buf[count] = '\n';
}
}
This works just fine.
it will output and print the following
Enter last name: ted
Enter first name: danson
danson, ted
The hotel.h file just contains two extern variables:
extern int fd; /*a file descriptor for global use*/
#define NLINES 41
#define MAXROWS 10
extern char buf[NLINES];
Then I copy this file to the directory with the rest of the files, make a few modifacations and don't get what I expect.
Code:
#include "hotel.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdbool.h>
#define LNAME 22
#define FNAME 19
char buf[NLINES];
int fd;
void gname(int, int *);
bool
addguest(int *roomnum)
{
int room, index = 0;
off_t offset;
if (!findfree(&room))
return false;
else
*roomnum = room;
offset = (room - 1) * NLINES;
if (lseek(fd, offset, SEEK_SET) < 0) {
perror("problem with lseek in addguest");
exit(-1);
}
printf("Enter last name: ");
gname(LNAME, &index);
clearstdin();
printf("Enter first name: ");
gname(FNAME, &index);
printf("%s", buf);
}
void
gname(int i, int *index)
{
int j, count = *index;
char c;
if (i == LNAME)
j = i - 2;
else if (i == FNAME)
j = (LNAME + FNAME) - 1;
while ( (c = getchar()) != '\n' && c != EOF && count < j) {
buf[count] = c;
count++;
}
if (i == LNAME) {
buf[count] = ',';
buf[++count] = ' ';
*index = ++count;
}
if (i == FNAME) {
for (count; count < j; ++count)
buf[count] = ' ';
buf[count] = '\n';
}
}
this will prompt the same, with the use of the clearstdin function, otherwise it will output
Enter last name: Enter first name:
the clearstdin function is:
Code:
void
clearstdin()
{
char c;
while ((c = getchar()) != NULL && c != '\n' && c != EOF)
}
so with this function I get the proper prompts but the output is:
, ted
Maybe it's the clearstdin function that's causing the problem but if it is I don't see how.
Thanks in advance.