Thread: reading files

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    reading files

    I am reading a file then printing the data onto the other file. It is working, however when I check to see if each variable is being properly set after reading the file a issue arrises.

    Example of the file being read
    Code:
    Vehicle PV50CAN passed camera 1 at 05:33:26.
    Vehicle W867BRO passed camera 1 at 05:33:29.
    Vehicle KQ63ARU passed camera 1 at 05:33:38.
    Vehicle K954ITQ passed camera 1 at 05:33:40.
    Vehicle V220MXB passed camera 1 at 05:33:42.
    Vehicle Q762YIV passed camera 1 at 05:34:10.
    Vehicle O858FGA passed camera 1 at 05:34:10.
    Vehicle V445ECN passed camera 1 at 05:35:14.
    Vehicle V646ADL passed camera 1 at 05:35:15.
    Example of the output
    Code:
     PV50CAN  1 05:33:26.
     W867BRO  1 05:33:29.
     KQ63ARU  1 05:33:38.
     K954ITQ  1 05:33:40.
     V220MXB  1 05:33:42.
     Q762YIV  1 05:34:10.
     O858FGA  1 05:34:10.
     V445ECN  1 05:35:14.
     V646ADL  1 05:35:15.
     C840BAK  1 05:35:16.
    
    [/code]

    In the console I am getting:

    PV5
    05:33:26.\
    1
    3ARU
    K95
    05:33:40.\
    1
    2YIV
    O85
    05:34:10.\
    1
    6ADL
    C840
    05:35:16.\
    1
    RKG



    I read the file and send the information I need to the output file. I then read it doing the following

    Code:
    std::ifstream readSortedFile;
        std::string plates;
        std::string camera;
        std::string time;
    
        readSortedFile.open("outFile.txt",std::ios::in);
        
        while(readSortedFile>>plates>>camera>>time){

    I then check the values that is being places in the variables.

    Code:
    //std::cout<<plates<<"\n";
            std::cout<<camera<<"\n";
            //std::cout<<time<<"\n";
            vehicle.setLicensePlate(plates);
            vehicle.setCamera(camera);
            vehicle.setTime(time);
    //  std::cout<<vehicle.getCamera()<<"\n";
    I check each one and they are all getting the same values. I need each variable to to be set properly. Not all of them having identical information.

    Whole Code
    Code:
    #include <iostream>
    #include "Calculate.h"
    #include "Vehicle.h"
    #include <fstream>
    
    
    Calculate::Calculate(){
        
    }
    
    void Calculate::readFile(){
        std::ifstream reader;
        std::ofstream writer;
        writer.open("outFile.txt");
        reader.open("carSpeeds.txt",std::ios::in);
        std::cout << "Reading from the file" << std::endl;
        
        while (reader>>line) {
            
            if (line=="Vehicle"||line=="Speed"||line=="limit"||line=="is"
                ||line=="passed"||line=="camera"||line=="at"||line==".\\") {
                
                //erase the string if it matches
                line=" "; //create a space so there is not a mega string
            }
            
            //write string to outfile
            writer<<line<<std::endl;
        }
        reader.close();
        writer.close();
        
        //read file after it has been sorted and all unnessary strings have been removed
        std::ifstream readSortedFile;
        std::string plates;
        std::string camera;
        std::string time;
    
        readSortedFile.open("outFile.txt",std::ios::in);
        
        while(readSortedFile>>plates>>camera>>time){
            
            //std::cout<<plates<<"\n";
            std::cout<<camera<<"\n";
            //std::cout<<time<<"\n";
            vehicle.setLicensePlate(plates);
            vehicle.setCamera(camera);
            vehicle.setTime(time);
        
          //  std::cout<<vehicle.getCamera()<<"\n";
        }
    }
    void Calculate::calculateSpeed(){
        
    
    }
    Last edited by jocdrew21; 11-07-2014 at 06:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-29-2012, 02:55 PM
  2. Help with reading in files!!! please
    By mrsmoss3791 in forum C Programming
    Replies: 1
    Last Post: 10-10-2011, 07:15 PM
  3. Reading xls files
    By dlwlsdn in forum C Programming
    Replies: 20
    Last Post: 12-04-2008, 07:38 AM
  4. reading files
    By stevew2607 in forum C++ Programming
    Replies: 5
    Last Post: 06-07-2002, 04:31 PM
  5. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 10:43 AM