• C

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.

#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);
}

Open in new window

LVL 8
pzozulkaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenDBACommented:
Howdy....

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
0
pzozulkaAuthor Commented:
Thanks, but that didn't help. When I comment out the READ line of code, everything works as expected.
0
Kent OlsenDBACommented:
Ah.  Ok.

You're mixing buffered and raw I/O.  The application should consistently use either streamed or raw I/O on any given object.  (It's fine to read STDIN and perform raw input on another file.)


Good Luck,
kent
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pzozulkaAuthor Commented:
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:
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);
}

Open in new window

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);
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.