REading from file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • electromania
    New Member
    • Mar 2008
    • 20

    REading from file

    I'm trying to read from a text file. The format of the text file is


    1999 78686
    2000 78999
    2001 80003
    ............... .....
    ............... .....
    ............... ....
    2002 45489 (so forth).

    I want to be able to count how many rows are there and also add all the values in the second column.

    I want to know how do I go about storing the values once i read the file? should I just increment each line or use Arrays?
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Are u using C or C++?
    If C you can go for arrays
    If C++ go for Vector as you can sort using the stl alogorithm.


    Raghuram

    Comment

    • romcab
      New Member
      • Sep 2007
      • 108

      #3
      HI,
      You can parse the file using strtok or you create your own by reading each line and separate it by space. You can determine how many rows by reading each line using fgets and use a counter.

      Comment

      • TKM
        New Member
        • Dec 2007
        • 11

        #4
        1) Get the data using the %d data type
        2) use counter to determine whether data is from the 1st or 2nd column and save them in arrays. Use a counter to count the number of items saved

        Comment

        Working...