I have 3 ".c" files called pre, sort and pipe. Pre takes a user input of names and GPA from a console. If the GPA is greater than or equal to 3.0, the name is stored into a struct.
Here's the pre.c file:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student{
char temp_name[50];
char names[50];
};
int main()
{
int read_index = 0;
float check_gpa;
struct student data[read_index];
printf("Enter student name and GPA: \n");
scanf("%s %f\n", data[read_index].temp_name, &check_gpa);
read_index++;
while(scanf("%s %f\n", data[read_index].temp_name, &check_gpa) != EOF)
{
if (check_gpa >= 3.0)
{
strcpy(data[read_index].names, data[read_index].temp_name);
read_index++;
}
}
return 0;
}
The pipe file links the pre and sort files so that data from pre is sent to sort.
Here is the pipe.c file:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>
int main()
{
char *args[] = {"./sort", NULL};
char *argv[] = {"./pre", NULL};
int pipe_end[2];
int pipe_id;
pipe(pipe_end);
if (pipe(pipe_end)==-1) // Check for pipe functionality
{
perror("Pipe Failed");
return 1;
}
pipe_id = fork();
if (pipe_id < 0) //Check for fork functionality
{
printf("Fork failed");
return 1;
}
else if(pipe_id == 0)//Child
{
close(pipe_end[0]);
dup(pipe_end[0]);
execvp(argv[0], argv);
}
else //Parent
{
wait(NULL);
close(pipe_end[1]);
dup2(pipe_end[1], 0);
close(pipe_end[0]);
execvp(args[0], args);
}
return 0;
}
The sort file takes names from a struct array and sorts them alphabetically and prints them to the console. And this is where the problem starts, because when I run the pipe file i get to enter names and GPA's But when ever I initiate an EOF (which is required and cannot change) by pressing Ctrl+D, I'm expecting the strings to be sent over to sort and displayed alphabetically, however this doesn't happen.
Here's the sort.c file:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>
struct student{
char temp_name[50];
char names[50];
};
int main()
{
int print_index1 = 0,
print_index2,
read_index,
SIZE;
struct student data[print_index1];
SIZE = print_index1;
printf("Students with GPA's greater than or equal to 3.0, listed in alphabetical order:\n ");
for(print_index1 = 0; print_index1 < SIZE; print_index1++)
{
for(print_index2 = print_index1 + 1; print_index2 < SIZE; print_index2++)
{
if(strcmp(data[print_index1].names, data[print_index2].names) > 0)
{
strcpy(data[print_index1].temp_name, data[print_index2].names);
strcpy(data[print_index2].names, data[print_index1].names);
strcpy(data[print_index1].names, data[print_index1].temp_name);
}
}
printf("%s\n", data[print_index1].names);
}
return 0;
}
I've tested both files independently with user input and they worked. But a new problem has showed up, if you look at the sort file and notice I have a while loop that takes a for loop condition that I thought made sense> It worked a week ago but now it doesn't (unless it was a fluke). But that's my dilemma, I can't seem to get user input from "pre.c" over to "sort.c" I would really appreciate some help. I also think the while loop in the "sort.c" file is causing an issue with printing the names.