how to generate data from file

This is a discussion on how to generate data from file within the C++ Programming forums, part of the General Programming Boards category; Hello, i need help. How to generate output from file ? example input from file: SERVICE_NUMBER, TO_NUMBER, DURATION, CHARGE, SEIZE_TIME ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    29

    how to generate data from file

    Hello,
    i need help.
    How to generate output from file ?

    example input from file:
    SERVICE_NUMBER, TO_NUMBER, DURATION, CHARGE, SEIZE_TIME
    0060419076, 035026088, 101, 60, 01-Nov-2006 09:30:58 AM
    0820041729, 035026088, 43, 70, 01-Nov-2006 08:40:46 AM
    0040993004, 019406898, 4, 10, 01-Nov-2006 10:15:00 AM
    0040993004, 019406898, 15, 10, 01-Nov-2006 10:15:44 AM
    0040444177, 013394854, 313, 450, 01-Nov-2006 11:09:34 AM


    The report must be like so:
    SERVICE_NUMBER, TO_NUMBER, FREQUENCY, TOTAL_DURATION, TOTAL_CHARGE
    0060419076, 035026088, 1, 0 00:01:41, 0.60
    0820041729, 035026088, 1, 0 00:00:43, 0.70
    0040993004, 019406898, 2, 0 00:00:19, 0.20
    0040444177, 013394854, 1, 0 00:05:13, 4.50

    im going to use ifstream to read the input from file.
    the problem is :

    1.how to generate a report with frequency.
    2.how to convert duration from seconds to 00:00:00 type.
    Last edited by vearns; 12-19-2006 at 10:33 AM.

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    when you learn to use ofstream you'll see that you can output whatever information you want in whatever for myou want. as for the number format, if it is an integer you store it in an istringstream and it converts that number to a string.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    show me some codes plz.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    It helps if you know some OOP and how to use the STL. If you work with each line from the file as a record implemented as a class/struct object and store them within a STL container of some sort, i.e. vector, then things can be made much easier. Read a line from the file and parse the data into a record (struct/class). Try to find a matching record in the container (matching by SERVICE_NUMBER/TO_NUMBER fields). If you find a match, then you update the frequency counter for that record and also update the other necessary fields in the matching record with the data from the new record. If there is no match found, then insert the new record into the container. When it comes time to output the data to a new file, you iterate through the container and output the necessary data to the file. The I/O can be handled neatly by overloading the stream insertion/extraction operators for the given record type.

    You should store your total duration values as seconds and figure out a way to output that as an hour:minute:second value when its time to output. This can be done by some simple division. For example, your 313 seconds value must be displayed as 00:05:13. If you divide 313 by 60, you get 5 with a remainder of 13 which is where the 05:13 comes from. If the result of the first division is greater than 59, then you must divide that by 60 to get hours and the remainder will be the minutes left over. For example, if the total duration in seconds is 6727, then divide that by 60 and you get 112 with a remainder of 7. The 7 is the seconds and since the 112 is greater than 59, we must divide that by 60 to get 1 with a remainder of 52 so the 1 is the hour and the 52 is the minutes resulting in a final value of 01:52:07.

    Quote Originally Posted by vearns
    show me some codes plz.
    This is your duty. Show us your coding attempt in trying to solve this and we will assist as appropriate with suggestions, etc...
    Last edited by hk_mp5kpdw; 12-19-2006 at 12:35 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by vearns
    show me some codes plz.
    Show us some of your attemp please and describe the concrete problem you cannot solve, not the whole assignment text
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by vearns
    1.how to generate a report with frequency.
    2.how to convert duration from seconds to 00:00:00 type.
    1. I think hk_mp5kpdw has showed you the proper way to do it.

    2. Use ctime

    show me some codes plz.
    Try code it yourself first.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    29
    ok,

    i already did the coding but the question is why it show the same thing and keep looping ?

    here is the code

    Code:
    #include <iostream>
    #include <fstream>
    #include <conio.h>
    using namespace std;
    
    // SERVICE_NUMBER TO_NUMBER DURATION  CHARGE  SEIZE_TIME
    // 0060419076 035026088 101 60  01-Nov-2006 09:30:58 AM
    
    struct record {
      string src;
      string dst;
      int duration;
      int charge;
    };
    
    struct summary {
      string date;
      string time;
      string ampm;
    };
    
    bool readOneLine(ifstream &file, record &rec) {
      file >> rec.src;
      file >> rec.dst;
      file >> rec.duration;
      file >> rec.charge;
    
      if( file.eof() )
       return false;
    
      return true;
    }
    
    void showRecord(record &rec) {
      cout << "source='" << rec.src << "', ";
      cout << "destination='" << rec.dst << "', ";
      cout << "duration='" << rec.duration << "', ";
      cout << "charge='" << rec.charge << "'" << endl;
      getch();
    }
    
    int main() {
      ifstream myfile;
      record r;
    
      myfile.open("problem2-sample-data.txt");
    
      if ( !myfile.is_open() ) {
        cout << "Could not open the file 'problem2-sample-data.txt'" << endl;
        return 1;
      }
    
      string header;
      getline(myfile, header);
    
      bool gotOneLine;
    
      gotOneLine = readOneLine(myfile, r);
    
      int count = 0;
      while(gotOneLine) {
        count++;
        showRecord(r);
        gotOneLine = readOneLine(myfile, r);
      }
      cout << "Total records found: " << count << endl;
      getch();
    }


    please help me
    Attached Files Attached Files

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Well, you see the same thing looping over and over because your input stream goes into a fail state after attempting to read the first record's "charge" value because of how the input is structured. This causes all subsequent read attempts to automatically fail and the program goes into an infinite loop since the eof state is never reached. The read of the charge value fails because after the successfull read of the first record's duration value, the file is looking at the comma after that value. This comma is then the first thing that the read operation sees as it tries to convert that into an integer which is what causes the stream to go into the fail state. You need to determine how you want to deal with those commas. You are even reading them into the src and dst fields and displaying them in your output.

    Your example of the input in your code does not match the actual data. The sample in your code does not contain these commas that are screwing things up. The actual file however contains these commas. If you get rid of the commas in the file, then things might work a bit better. Your read function is still going to need to deal with the remaining input on the line however.
    Last edited by hk_mp5kpdw; 12-27-2006 at 08:02 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 10:47 AM

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