Thread: Input file handling

  1. #1
    Registered User
    Join Date
    Nov 2008

    Input file handling

    I'm trying to figure out how to handle the format of this file to be used with Dijkstra - type algorithm.

    It is a simple text file, with an unknown number of lines (easily created function to solve this). This is how it looks

    city destination:weight destination:weight ....

    1 2:10
    2 3:50 1:70
    3 5:10 1:50 2: 20 4:30
    4 1:20
    This basically says 1 can go to 2 with weight of 10. 2 can go to 3 with weight 50, or to 1 with weight 70...
    There can be any number (numCities - 1) of destinations and weights per line. I'm having trouble thinking of how to store the cities with their destinations and weights. I thought about trying to use structs / linked lists but with the unknown amounts (particularly for pointers, city->weight1 city->weight I don't think this would be feasible. Any suggestions?

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    why not a structure or class with the following members:
    std::vector<int> destinations;
    std::vector<int> weights;
    so that it's easy to add an arbitrary number without having to worry about memory.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 11:43 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM