Thread: Printing ordered data from file

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    11

    Printing ordered data from file

    I have the following task:
    Implement a program that reads lines of data from a file specified as a command-line argument, having the following format:
    Code:
    {ana12 : mere}
    {andrew : -}
    {john     :     student}
    {michael  :   teacher}
    The fields are separated by an ':' character and might have an arbitrary number of spaces in between. The following constraints should be taken into consideration and validated:
    - key must be a word up to 50 characters, composed only of letters or digits;
    - value can be an arbitrarily long word, composed of letters or '_' character.


    Print the data from the file in descending order of the key length, breaking ties by ordering the keys in lexicographical order.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #define MAX 51
    #define CHUNK 300
    int main(int argc, char* argv[])
    {
        if(argc != 2)
        {
            printf("Invalid format.\n");
            return -1;
        }
        FILE* fin = fopen(argv[1], "rt");
        if(!fin)
        {
            printf("Error opening the file.\n");
            return 1;
        }
        char key[MAX];
        char* s = calloc(CHUNK, sizeof(char));
        while(fgets(s, CHUNK, fin))
        {
            char *token = strtok(s, ":");
            while(token)
            {
                while(sscanf(token, "%50s", key) == 1)
                {
                    int len = strlen(key), ch;
                    //if(key[0] ==
                    printf("Key : %s\n", key);
                }
            token = strtok(NULL, ":");
            }
        }
        fclose(fin);
        return 0;
    }
    I'm having trouble with these kind of tasks because I know I need to use structures and sort the data but I'm a bit lost. The code above is stuck in an infinite loop and prints:
    Code:
    {ana12
    Could just give me some steps to work on, like what structure should I implement and how to sort the data? Thanks in advance!

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    The best place to start is with your data structures.
    Here it's quite simple. You need a struct with a single key / value pair, both strings. It probably mkaes sense to make them both char *s and allocated by malloc because your maximum key length, 50 characters, is quite high.

    Then you need an array of those structs, and a count.

    Since we are not interested in performance, you can open the file, read in the records one by one, and call realloc() each time to grow the array. The tricky part is writing a robust record parser. scanf() is no good because your value strigns are unbounded, so you'll have to load a line, then parse it character by character.

    Get that working an test it.

    The rest is far easier. Set up a comparison function to pass to qsort, and sort your array.

    Then print it out. Outputting data in a format is far easier than inputting it, as you will find when you write this function.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data from a file and printing it
    By Linux Trojan in forum C Programming
    Replies: 27
    Last Post: 07-02-2011, 12:45 PM
  2. Printing a graph with data read in from a file
    By levitylek in forum C Programming
    Replies: 1
    Last Post: 11-17-2010, 05:21 PM
  3. extracting and printing data from a .dat file
    By z0diark in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 05:07 PM
  4. Reading from file and printing double data
    By hammari in forum C Programming
    Replies: 4
    Last Post: 07-14-2009, 07:02 AM
  5. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM

Tags for this Thread