Thread: Finding unique strings within a file

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    9

    Finding unique strings within a file

    Hello all,

    I have been trying to write this program that reads a file of courses and then stores only the unique courses in an array of strings.

    for some reason it crashes while reading the 2nd line.

    printf within the function are just for me trying to trace the problem.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    char** uniqueCourses(FILE *fp, int *wordCount);
    
    
    int main()
    {
        FILE* fp;
        char** in;
        int i =0;
        int n =0;
        int* w = &n;
        fp = fopen("data.txt","r");
        in = uniqueCourses(fp,w);
    
    
        for (i=0; i <3; i++)
        {
            printf("%s",in[i]);
        }
        return 0;
    }
    char** uniqueCourses(FILE *fp, int *wordCount)
    {
        char* del = ",";
        char* token;
        char buffer[100];
        char** myArray;
        myArray = malloc(50* sizeof(char*));
        int i = 0;
        int v=0;
        int flag =0;
        int count = 0;
    
    
        while(fgets(buffer,sizeof(buffer),fp) != NULL)
        {
    
    
            token = strtok(buffer,del);
            while(token != NULL)
            {
                if (count == 0)
                {
                    myArray[i] = malloc(10 * sizeof(char));
                    strcpy(myArray[i],token);
                }
    
    
    
    
                for (v =0; v<count; v++)
                {
                    if (strcmp(myArray[v],token) == 0)
                    {
                        flag = 1;
                        break;
                    }
                }
                printf("2");
    
    
                /* if not then store it in our main array */
                if (flag != 1 )
                {
                    i++;
                    myArray[i]= malloc(10 * sizeof(char));
                    strcpy(myArray[i],token);
                    printf("%s",myArray[i]);
    
    
                }
                printf("3");
                count++;
                token = strtok(NULL,del);
    
    
            }
            printf("1");
    
    
        }
    
    
        return myArray;
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the file format?

    It looks like you are doing too much in a single function.

    Write a function that reads the information for a single course from the file. Test that this function works correctly.

    Write another function that uses the previous function to read all courses from the file. Test that this function works correctly.

    Amend the previous function to check for duplicates before adding to course to the array. Or, write another function to remove duplicates.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    9
    file format is basically comma delimited course names and multiple functions will definitely make it easier but i'm required to do it all in one function.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by oceans
    file format is basically comma delimited course names and multiple functions will definitely make it easier but i'm required to do it all in one function.
    Since you are only reading course names, i.e., just single strings, you probably do not need a separate function that reads the information for a single course from the file. That said, I doubt that you are required to do it all in a single function, only that it is probably simple enough to do so. (Or if you really are required to do that, then it implies that your teacher is a bad programmer, or is trying to teach by negative example, but this simple scenario hardly qualifies as a negative example.)

    So, if you want, you can combine the first two functions into one, and if you amend this function instead of writing a separate function to eliminate duplicates, you really would end up with just one function. But do this step by step.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Program could be greatly simplified if you know the maximum length of the course.

    Otherwise, I advise you to look at this source code - SourceForge.net Repository - [cutest] Contents of /cutest/CuTest.c

    You will learn to make a very comfortable string operations. Your program become trivial with such framework.
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  6. #6
    Registered User
    Join Date
    Jan 2014
    Posts
    9
    Thank you everyone! I was able to fix my original code. I just had to to reset the flag counter, add "counter!=0" to if statement along with flag!= 0 and move counter++ and i++ within the conditionals.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding an array of unique values within a 2D array
    By saadashfaq in forum C Programming
    Replies: 6
    Last Post: 11-14-2011, 12:02 AM
  2. Finding unique strings in an array
    By knirirr in forum C Programming
    Replies: 3
    Last Post: 02-20-2008, 07:08 AM
  3. How can i make a unique copy of strings?
    By what3v3r in forum C++ Programming
    Replies: 9
    Last Post: 01-12-2006, 09:11 PM
  4. create unique file
    By crescen7 in forum C Programming
    Replies: 5
    Last Post: 06-17-2002, 12:51 AM