Thread: MAC C Scanning text documents, merge and summing lines

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    2

    MAC C Scanning text documents, merge and summing lines

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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you need to ditch the global variables to begin with.

    Then perhaps go with
    Code:
    if ( * meth == 't' ) {
      doTransmit(inputfile,output);
    }
    else if ( * meth == 'r' ) {
      doReceive(inputfile,output);
    }
    else {
      // error
    }
    At least your main won't become a bloated monster.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    2
    So I guess, where I get confused (due to my lack of understanding) is should I be using fscanf to open up the file and isolate each column? So say I specifically want to use the macsend column and the bytes column, and I want to merge any duplicate macsend names, but then in the corresponding bytes column, the integers are summed. So say, KEVIN 3, JOHN 7, KEVIN 6, JOHN 6. And then in the new text file, KEVIN 9, JOHN 13.
    If I am using fscanf, is there a specific way I can make the program search through all the macsend lines and merge them? Could I somehow use the strstr() function to do this?
    Cheers

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, your approach so far is fine.

    But you need to break your problem down into small steps you can achieve and test.
    A development process

    So for example, write a program which stores the first "KEVIN" in an array somewhere (call it seenNames), and then prints out a message each time it sees another "KEVIN".


    Begin with picking better variable names than 'byte'.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble scanning and sorting lines from a file
    By cato in forum C Programming
    Replies: 3
    Last Post: 04-22-2016, 01:46 PM
  2. Is there windows function that merge all word documents into one?
    By kiracoder123 in forum Windows Programming
    Replies: 4
    Last Post: 05-28-2011, 03:43 PM
  3. Scanning a text file.
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-10-2001, 05:27 AM

Tags for this Thread