Input file handling

This is a discussion on Input file handling within the C++ Programming forums, part of the General Programming Boards category; I'm trying to figure out how to handle the format of this file to be used with Dijkstra - type ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    54

    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
    Posts
    435
    why not a structure or class with the following members:
    Code:
    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, 12:43 PM
  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, 08:51 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21