-2

The loop that reads from the file and prints the result is not printing the first 5 characters from all files read. If I print them 1 character at a time it works fine, but I'm reading floating point numbers from a file which I need to be processed later. The numbers are seperated by white space which is why I'm trying to print only when encountering white space.

here is my source code

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>

#define BUFFER_SIZE 100
#define READ_END 0
#define WRITE_END 1

//main function
int main(int argc, char *argv[])
{
    //process ids
    int processIds[argc - 1];
    int pipes[argc - 1][2];
    char *fileNames[argc - 1];
    int status; 

    //check if at least one data file
    if (argc == 1)
    {
        return -1;
        //end program    
    }

    //get file names and store in array fileNames
    int i;
    for (i = 0; i < (argc - 1); i++)
    {
        fileNames[i] = argv[i + 1];
    }    

    //create child processes
    for (i = 0; i < (argc - 1); i++)
    {
        //make a pipe for communication
        if (pipe(pipes[i]))
        {
            printf("bad pipe");
            return -1;
        }
        //make a child process
        processIds[i] = fork();

        //check if child or paren process
        if (processIds[i] == 0)
        {
            //child process
            //close unused end

            close(pipes[i][READ_END]);

            //make file
            FILE *dataset = fopen(fileNames[i], "r");

            //test if file opened
            if (dataset == 0)
            {
                printf("could not find/open file");
                return -1;
            }

            //read and process file
            char *x;
            char *num = "";
            int min, max;
            while ((*x = fgetc(dataset)) != EOF)
            {
                if ((x == " ") || (x == "\t") || (x == "\n"))
                {
                    //end of number
                    printf("A%s B%s", num, num);
                    num = "";
                }
                else
                {
                    strcat(num, x);
                }
                //printf("%c", x);    
            }
            printf("\n\n");
            char msg[BUFFER_SIZE];

            write(pipes[i][WRITE_END], msg, strlen(msg) + 1);
            fclose(dataset);
            exit(0);
        }
        else if (processIds[i] < 0)
        {
            //error
            return -1;    
        }
        else
        {
            //parent process closes write end of pipe
            close(pipes[i][WRITE_END]);
        }
    }

    //wait for children
    for (i = 0; i < (argc - 1); i++)
    {
        //create a read buffer
        char read_buf[BUFFER_SIZE];

        //wait and get pid of completed child
        int pid = wait(&status);

        //find which child completed
        int j = 0;
        while (pid != processIds[j] && j < (argc - 1))
        {
            //pid not recognized, should not happen
            if (j >= (argc - 1))
            {
                printf("bad pid");
                return -1;
            }
            j++;
        }
        //read from completed child
        read(pipes[j][READ_END], read_buf, BUFFER_SIZE);
    }

    return 0;
}
Carey Gregory
  • 6,836
  • 2
  • 26
  • 47
  • 2
    You're not allocating any memory for `num` so using `strcat` to append to it is undefined behavior. `msg` is also unitialized. – Retired Ninja Jun 17 '14 at 01:25
  • 2
    I know it's a pain in the butt that SO doesn't handle tabs properly as indentation characters, but when you post code you really need to make an effort to indent it properly and the only way to do that is to replace all tabs with 4 spaces. With a decent editor, that should be just seconds worth of work. I just did it for you but it took longer than it was worth. – Carey Gregory Jun 17 '14 at 01:32

1 Answers1

1

i can spot the following misses concerning your code:

1) Memory allocation

you need to allocate your num variable

example :

char *num = malloc(10);

2) returned type

'fgetc' doesn't return a pointer nor a char, so you should define

int x;

x = fgetc(dataset);

3) your condition is wrong

if ((x == ' ') || (x == '\t') || (x == '\n'))

note that ' ' is not " "


NOW as my suggestion to read those separate strings:

1) read all the file in buffer :

char buff[SIZE];
dataset= fopen("fileName.txt", "r");
   if( dataset!= NULL ){
      while ( !feof(fp ) ){
         memset(buff, '\0', sizeof( buff) );
         fgets(buff, SIZE, (FILE*)dataset);
      }
      fclose(dataset);
   }

2) use strchr to find next space and print your strings see example of use here

chouaib
  • 2,763
  • 5
  • 20
  • 35