So I'm writing a practice program that will take integers as input from stdin, load them into an array, sort the array, and then output the results.
I've been struggling trying to figure out how IO in C works. Here's what I have so far, please let me know if you see any issues/have any suggestions. Ideally I'd like to avoid the use of a buffer, but I can't seem to find another way to allow variably sized input
Input format: "10 20 30 11 666 1 235" ... etc
// check if an input file is specified
// If one is specified, then sort those numbers, otherwise pull from stdin
if(inputFile == "stdin"){
// Load stdin into a buffer
char buffer[100];
if(fgets(buffer, sizeof(buffer), stdin) == NULL) {
Handle_EOForIOError();
}
// Get a count of the numbers to create the array
int numIntegers = 0;
int num;
while(sscanf(&buffer[0], "%d ", &num) == 1){
numIntegers++;
}
// Initialize the array with the proper size
numbers = (int*)malloc(numIntegers*sizeof(int));
// Load the integers into the array
int i = 0;
while(sscanf(&buffer[0], "%d ", &numbers[i]) == 1){
i++;
}
}