file parser.... thing

Discussion in 'Programming General' started by rogue poser, Apr 11, 2008.

file parser.... thing
  1. Unread #1 - Apr 11, 2008 at 12:09 PM
  2. rogue poser
    Joined:
    Jul 10, 2005
    Posts:
    51
    Referrals:
    0
    Sythe Gold:
    0

    rogue poser Member

    file parser.... thing

    alright so, i have a file with 3 columns and 10,000 lines of information. the first column is a time, the second is a volt reading, and the third is an amp reading. what i need to do is parse the file and find the time when the highest voltage occurred, and the time when the highest amperage occurred...

    after i open the file im stuck... ive been looking all over the internet for tuts, and have found some information.... its just not what im looking for. does anyone have and idea of what i could do? i was thinking of making a dynamic array and storing all of the info in there, and searching every 3rd slot, and comparing it with the highest running value.... ideas?
     
  3. Unread #2 - Apr 11, 2008 at 1:17 PM
  4. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    file parser.... thing

    Do you need:
    1) 2 Times, the one with the highest voltage and the one with the highest amperage.
    OR
    2) The time with the highest voltage and amperage.

    I've already got a function I've used to parse an 80,000+ line file, I suppose I could modify it somehow to help you out. PM me your MSN.
     
  5. Unread #3 - Apr 11, 2008 at 1:27 PM
  6. rogue poser
    Joined:
    Jul 10, 2005
    Posts:
    51
    Referrals:
    0
    Sythe Gold:
    0

    rogue poser Member

    file parser.... thing

    well i the three colums are set up like this

    TIME VOLTAGE AMPERAGE
    .0040 9.3454 .034
    .0045 9.1234 .029




    and so on.....

    i need to find the highest and lowest voltage, and the highest and lowest amperage, at any given time.... so pretty much the highest amp (volts dont matter) and time, and the highest volts (amps dont matter) and time....

    so to answer ur question, i need number 1

    i dont have msn, but ive got aim, and im on at the moment "dirty l3um"
     
  7. Unread #4 - Apr 11, 2008 at 1:43 PM
  8. Faskist
    Joined:
    Apr 25, 2005
    Posts:
    1,869
    Referrals:
    0
    Sythe Gold:
    0

    Faskist Tuxhead
    Banned

    file parser.... thing

    Nully, read the file into a file pointer, and use the for loop you used for the ABC project to check for whitespace.
     
  9. Unread #5 - Apr 15, 2008 at 3:19 PM
  10. rogue poser
    Joined:
    Jul 10, 2005
    Posts:
    51
    Referrals:
    0
    Sythe Gold:
    0

    rogue poser Member

    file parser.... thing

    if anyone cares i got my first working copy of the code. here she is

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    ///////////////////////
    // Declare Variables //
    ///////////////////////
    
    int main()
    {
      float info,amps,time,arraynum,avga,avgv;
      float vlow = 10;
      float alow = 10;
      float ahigh = 0;
      float vhigh = 0;
      char doc[12]="E:/volt.txt";
      int start = 0;
      int total,x,z;
     
    /////////////////////
    //  Error Message  //
    /////////////////////
    ifstream infile(doc, ios::in);
    fstream outfile(doc, ios::in);
       if (!infile)
    	{
    	   cout << "cannot open the file...\n";
    	}
       if(infile)
    	{
       	   cout << "file opened \n";
    	}
    
    /////////////////////////////////////
    // find the highest and the lowest //
    /////////////////////////////////////
    
    x = 1;
    cout << "time: .0001 "; 
    for(start = 0; start<2749770; start++)
    	{
    	   arraynum = info;								// searches every byte in the file
    	   total = start;
    	   outfile.seekp(total);
    	   outfile >> time >> info >> amps;
    	   
    
    	    if(arraynum != info)
    		{
    
    		  if(x%3 == 1)								// displays the voltage
    				{
    				  cout << " volts: " << info ;
    				   if(info > vhigh)
    					   vhigh = info;
    				   if (info < vlow)
    					   vlow = info;
    				   avgv = avgv + info;
    				}
    
    		  if(x%3 == 2)								// displays the amperage
    				{
    			      cout << " amps: " << info << endl;
    
    				  if(info > ahigh)
    					  ahigh = info;
    				  if(info < alow)
    					  alow = info;
    				  avga = avga + info;
    			    }
    
    
    		  if(x%3 == 0)								// displays the time
    			{
    			    cout << "time: " << info;
    			}
    			x++;
    					
    		}// end if
    	
    	}//end for	
    //avga = avga/10000;
    //avgv = avgv/10000;
    												// output the information
    cout << endl<< endl;
    cout << "Highest Voltage: "<< vhigh << "   Highest Amperage: " << ahigh << endl << endl;
    cout << "Lowest Voltage: " << vlow <<"   Lowest Amperage: " << alow << endl << endl;
    cout << "Average Voltage: " << avgv << "   Average Amperage: " << avga << endl <<endl;
    
    
      return 0;
    }//end main
    im not completely done, i need to get the average working, but thats all i have left....

    seemed much harder than it was, once i hit the "grindstone" if you will it took about an hour to make. if your wondering why my for loop is so large its because im checking every byte in the entire file

    :p
     
  11. Unread #6 - Apr 15, 2008 at 4:42 PM
  12. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    file parser.... thing

    It's really nice to see that you did it yourself, great work. I modified your code some myself to make a few minor improvements and also fixed the averaging.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    ///////////////////////
    // Declare Variables //
    ///////////////////////
    
    int main()
    {
      float info,amps,time,arraynum;
      float vlow = 10^99;
      float alow = 10^99;
      float ahigh = 0;
      float vhigh = 0;
      float vtotal = 0, atotal = 0;
      int vnum = 0, anum = 0;
      char doc[12]="volt.txt";
      int total,z;
      int x = 1;
     
    /////////////////////
    //  Error Message  //
    /////////////////////
    ifstream infile(doc, ios::in);
    fstream outfile(doc, ios::in);
       if (!infile)
    	{
    	   cout << "cannot open the file...\n";
    	}
       if(infile)
    	{
       	   cout << "file opened \n";
    	}
    
    /////////////////////////////////////
    // find the highest and the lowest //
    /////////////////////////////////////
    
    
    cout << "time: .0001 "; 
    for(int start = 0; start<2749770; start++)
    	{
    	   arraynum = info;								// searches every byte in the file
    	   total = start;
    	   outfile.seekp(total);
    	   outfile >> time >> info >> amps;
    	   
    
    	    if(arraynum != info)
    		{
    
    		  if(x%3 == 1)								// displays the voltage
    				{
    				  cout << " volts: " << info ;
    				    vtotal += info;
    				    vnum++;
    				   if(info > vhigh) {
    					   vhigh = info; }
    				   if (info < vlow) {
    					   vlow = info; }
    				}
    
    		  else if(x%3 == 2)								// displays the amperage
    				{
    			      cout << " amps: " << info << endl;
    				   atotal += info;
    				   anum++;
    				  if(info > ahigh) {
    					  ahigh = info; }
    				  if(info < alow) {
    					  alow = info; }
    			    }
    
    
    		  else if(x%3 == 0)								// displays the time
    			{
    			    cout << "time: " << info;
    			}
    			x++;
    					
    		}// end if
    	
    	}//end for	
    
    												// output the information
    cout << endl<< endl;
    cout << "Highest Voltage: "<< vhigh << "   Highest Amperage: " << ahigh << endl << endl;
    cout << "Lowest Voltage: " << vlow <<"   Lowest Amperage: " << alow << endl << endl;
    cout << "Average Voltage: " << vtotal / vnum << "   Average Amperage: " << atotal / anum << endl <<endl;
    
      getchar();
      return 0;
    }//end main
    
     
  13. Unread #7 - Apr 17, 2008 at 5:08 PM
  14. rogue poser
    Joined:
    Jul 10, 2005
    Posts:
    51
    Referrals:
    0
    Sythe Gold:
    0

    rogue poser Member

    file parser.... thing

    lol apparently i went a bit overkill.... i have recently found out that the fstream library has a function that will read every piece of information in the file. so instread of going byte by byte, i could save everything into a buffer and have it be seperated by the blank spaces.... so that makes the program run in 10 seconds instead of 3 minuites :p. nothing wrong with re-inventing the wheel i suppose?
     
  15. Unread #8 - Apr 17, 2008 at 5:51 PM
  16. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    file parser.... thing

    Yeah, I was going to mention that there was a better way but figured that since this did the trick and you thought it up yourself why bother. Great work getting it done on your own for the most part. :)
     
< A Few Questions (Focusing, Adding and Decimals) | Released RS [VB6] Projects >

Users viewing this thread
1 guest


 
 
Adblock breaks this site