Hey all. I'm new to the forum and the programming world. I have a uni project where I have to open a text file, grab certain lines from the file, mainly a column with names and a column with numbers. Any duplicate names are to be removed and their corresponding values be summed into one.
I've developed the main structure I believe it's structured right?
But alas I'm really just looking for some guidance on how structure the bit that organises the columns and condenses them down. I'm not seeking a solution but just not sure how to tackle it. Where I need to implement the code is near the while fscanf bit.
Thanks!
Code:
#include < stdio.h > #include < stdbool.h > #include < stdlib.h > #include < string.h >
// Initialise all characters
char macsend;
char macreceive;
float time1 = 0;
int byte[500][20];
//Load in input arguments
int main(int argc, char * argv[]) {
char * loc;
loc = argv[2];
char * meth = argv[1];
//Open and load the input text file.
FILE * inputfile = fopen(loc, "r");
if (inputfile == NULL) {
printf("\nFailed to open the file!.\n");
exit(1);
}
//Create the output file.
FILE * output = fopen("./output.txt", "w");
// If its a transmit request, we only need &macsend and &byte.
if ( * meth == 't') {
while (fscanf(inputfile, "%f\t%s\t%s\t%d", & time1, & macsend, & macreceive, & byte) != EOF)
printf("\nSuccess Te! \t %d", byte);
fclose(inputfile);
fclose(output);
exit(EXIT_SUCCESS);
}
// If its a receive request, we only need ¯eceive and &byte.
else if ( * meth == 'r') {
char transmission[]
while (fscanf(inputfile, "%f\t%s\t%s\t%d", & time1, & macsend, & macreceive, & byte) != EOF)
printf("Success R!\n");
fclose(inputfile);
fclose(output);
exit(EXIT_SUCCESS);
}
// If t or r is not used, print error.
else {
printf("Please use r or t for transmission!\n");
exit(EXIT_FAILURE);
}
}