pzozulka
asked on
C Programming: Code question
When I run this code, the print command runs fine on line 15, but no other print command our any kind of output happens after that line (including line 18).
I have a feeling it has something to do with the READ code on line 74.
This is for a project that requires me to use open(2), write(2), read(2), etc.
I have a feeling it has something to do with the READ code on line 74.
This is for a project that requires me to use open(2), write(2), read(2), etc.
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <pwd.h>
static void writeLine(char *b, int filedes);
static void readInput(const char *u, char *b);
int main( int argc, char *argv[] ) {
printf("test\n");
char *t = "This is a test";
printf("%s",t);
struct passwd *mypasswd;
int fd;
char *usr = malloc(sizeof(char) * 1024);
char *buf = malloc(sizeof(char) * 1024);
char *mytty;
//printf("test2\n");
// get username
if( (mypasswd = getpwuid(geteuid())) != NULL ) {
char *user = mypasswd->pw_name;
if(( mytty = ttyname(STDIN_FILENO)) == NULL)
perror("ttyname() error");
strcpy(usr, user);
strcat(usr, "@");
strcat(usr, mytty);
strcat(usr, ": ");
}
printf("a");
//open file
fd = open(argv[1], O_RDWR);
printf("0");
while(!feof(stdin)) {
printf("1");
readInput(usr, buf);
printf("2");
writeLine(buf, fd);
}
close(fd);
}
static void writeLine(char *b, int filedes) {
size_t nbytes;
ssize_t bytes_written;
nbytes = strlen(b);
if(( bytes_written = write(filedes, b, nbytes)) == -1)
perror("write() error");
}
static void readInput(const char *u, char *b) {
printf("1.1");
ssize_t bytes_written;
size_t count = 200;
int max_input = 200;
char buffer[max_input];
strcpy(b, "");
printf("%s", u);
// read
if(( bytes_written = read(STDIN_FILENO, buffer, count)) == -1)
perror("read() error");
// append user input to *b string
strcat(b, u);
strcat(b, buffer);
}
ASKER
Thanks, but that didn't help. When I comment out the READ line of code, everything works as expected.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I meant to say that I'm either using READ or FGETS. When I use FGETS everything works as expected, but when I use READ it makes all of my PRINTF functions stop working -- even before I get to the point where I call READ.
Example:
// Application works fine when:
Example:
// Application works fine when:
static void readInput(const char *u, char *b) {
ssize_t bytes_read;
size_t count = 200;
int max_input = 200;
char buffer[max_input];
buffer[0] = '\0';
strcpy(b, "");
printf("%s", u);
// read
fgets(buffer, count, stdin);
// append user input to *b string
//strcat(b, u);
strcat(b, buffer);
}
Application behaving incorrectly when using:static void readInput(const char *u, char *b) {
ssize_t bytes_read;
size_t count = 200;
int max_input = 200;
char buffer[max_input];
buffer[0] = '\0';
strcpy(b, "");
printf("%s", u);
// read
if(( bytes_read = read(STDIN_FILENO, buffer, count)) == -1)
perror("read() error");
// append user input to *b string
//strcat(b, u);
strcat(b, buffer);
}
There are variations in how printf() is implemented. The standards have changed over the years.
Try calling fflush (stdout). That may solve your problem.
Kent