Thread: Detecting and classifying information with a txt file.

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    2

    Detecting and classifying information with a txt file.

    Hey all, I have some work that I'm stuck on.

    I've been tasked with creating a basic system that classifies hurricane speed into one of 5 categories. I can pre set the information onto a txt file then read it from the program. However, The information that is preset must be categorized into categories.

    For instance the in the code I read a txt file that I have already set. Down below. In the txt file Identification (Picture attached) I've written down 3 identification numbers that have 3 different peak winds (the identification number does not matter but the peak wind does). I'd like the code to go through the file and organize the information into the different categories which are.

    If peak wind > 50 and < 70 = category 1

    else if peak wind > 70 and < 100 = category 2

    else if peak wind > 100 and < 130 = category 3


    In this case the identification.txt contains one of which I'd like the file to be read and a category to be displayed next to each identification number

    Imgur: The magic of the Internet



    Identification || Peak Wind (mph) || Category

    121 || 47 || 0

    41 || 93 || 1

    60 ||120 || 2




    Code:
    void HurricaneIdentification()
    
    {
    
        printf("The following values are the registered hurricanes, along with a unique identification number for each.\n");
    
    
    
    
        FILE *fileIdentification;
    
        fileIdentification = fopen("Identification.txt", "r");
    
        char singleLine[150];
    
    
    
    
        while (!feof(fileIdentification))
    
        {
    
            fgets(singleLine, 150, fileIdentification);
    
            puts(singleLine);
    
        }
    
        fclose(fileIdentification);
    
        main();
    
        return;
    
    }

    Any help is greatly appreciated.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Calling main is a very bad idea!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Also, feof doesn't work like that.
    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    Having read the line, you need to parse the line to extract the information you're interested in.

    Some functions to consider using:
    sscanf
    strstr
    strtok
    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.

  4. #4
    Registered User
    Join Date
    Jul 2018
    Posts
    2
    The issue is I'm not quite sure where to start and what I should be doing, I'll look into feof, as for the functions you've I'm mentioned I'm not exaclty sure how they will be able to help I read a description of each on tutorial point.

    For instance how will I read the identification number on the txt file, once I do that, how will I be able to compare the peak wind numbers that are located on the txt file?

    I drew out a basic flow chart that'll help clarify a bit on the task at hand Imgur: The magic of the Internet

    Could you recommend any video or website that does something similar ?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well presumably, you need to get from a text string of
    1) Identification Number (121) || Peak Wind (47)

    And arrive at two integer variables containing 121 and 47 respectively.

    Your flow chart looks OK, once you have extracted information from the file.

    > Could you recommend any video or website that does something similar ?
    I'd first say that 99% of the "how to program" videos on YouTube are crap.

    Also, there is a difference between learning how to program, and programming itself.

    It's like learning how to drive vs choosing a car.
    The former sets you up for being able to use most vehicles.
    The latter you choose depending on what you want to achieve. Do you need to be somewhere fast, or do you need to move a lot of stuff.
    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. Detecting and separating single words in a text file
    By mrafay in forum C Programming
    Replies: 12
    Last Post: 01-05-2011, 09:08 AM
  2. Detecting empty file situation.
    By xIcyx in forum C Programming
    Replies: 9
    Last Post: 06-18-2008, 10:37 PM
  3. Classifying characters (was struct:)
    By asteroidv2 in forum C++ Programming
    Replies: 18
    Last Post: 07-16-2006, 04:20 AM
  4. detecting end of file
    By abrege in forum C++ Programming
    Replies: 3
    Last Post: 12-15-2002, 11:07 PM
  5. Detecting the current instance of exe file
    By Unregistered in forum Windows Programming
    Replies: 3
    Last Post: 09-03-2001, 08:22 PM

Tags for this Thread