Thread: reading files

  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.

  2. #2
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Did I make you guys mad?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I don't see anything wrong with the code you supplied. However your problem is probably related to all the spaces you are throwing into your output file:

    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.
    Your input file appears to be a space delimited file with each entry on it's own line, why not use this fact to your advantage to get rid of that error prone if statement? Consider reading the entire line into a string then parsing that string with a string stream.

    Why are you writing this information to a file instead of just filling in your class variables when you read the input file, instead of writing the file to an output file then opening and reading that file? File reading and writing is orders of magnitude slower than in memory operations.

    My guess is the actual problem is located elsewhere in code you have again failed to provide. But this is only a guess because you have again failed to provide the smallest possible complete program that can be compiled and ran to verify the problem.

    By the way your definition of "Whole Code" must be different from most other people on the planet. My definition of "Whole Code" would be a program that can be compiled and ran, to be able to reproduce the stated problem. The code you provided is no where near a complete program, or the "Whole Code".


    Jim

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    jocdrew21, do yourself a favor and use
    Code:
    using namespace std;

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    do yourself a favor and use
    Why? There is nothing wrong with using the scope resolution operator.

    Jim

  6. #6
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    My definition of "Whole Code" would be a program that can be compiled and ran,
    I did that because I assumed no one would want to compile the code, I just showed the important code that pertains to the question. I thought I would annoy you guys by posting an entire program rather then narrowing down the issue and seeking advice. However I will post the "WHOLE CODE" below:
    Code:
    #include <iostream>
    #include "Calculate.h"
    
    int main(int argc, const char * argv[]) {
        
        Calculate cal;
        
        cal.readFile();
       
        return 0;
    }
    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 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(){
        
    
    }
    Code:
    #ifndef __Vehicle_Speed__Calculate__
    #define __Vehicle_Speed__Calculate__
    
    #include <stdio.h>
    #include <string>
    #include <vector>
    #include "Vehicle.h"
    
    class Calculate {
    private:
        std::string line;
        std::vector<std::string> readerVector;
        Vehicle vehicle;
    public:
        Calculate();
        void readFile();
        void calculateSpeed();
        
    };
    
    #endif
    Code:
    #include <iostream>
    #include "Vehicle.h"
    
    Vehicle::Vehicle(){
    
    }
    bool Vehicle::isSpeeding(){
        
        return true;
    
    }
    
    void Vehicle::setLicensePlate(std::string plates){
        
        this->plates=plates;
    
    }
    
    std::string Vehicle::getLicensePlate(){
        
        return plates;
    }
    
    void Vehicle::setCamera(std::string camera){
        
        this->camera = camera;
    
    }
    
    std::string Vehicle::getCamera(){
        
        return camera;
    
    }
    
    void Vehicle::setSpeed(std::string speed){
    
    }
    
    double Vehicle::getSpeed(){
        return speed;
    }
    
    void Vehicle::setTime(std::string vtime){
        
        this->vtime=vtime;
    
    }
    
    std::string Vehicle::getTime(){
        
        return vtime;
    }
    Code:
    #ifndef __Vehicle_Speed__Vehicle__
    #define __Vehicle_Speed__Vehicle__
    
    #include <stdio.h>
    #include <string>
    
    class Vehicle {
    private:
        double speed;
        std::string camera;
        std::string plates;
        std::string vtime;
        std::string licensePlate;
        
    public:
        Vehicle();
        
        bool isSpeeding();
        void setLicensePlate(std::string);
        std::string getLicensePlate();
        void setCamera(std::string);
        std::string getCamera();
        void setSpeed(std::string);
        double getSpeed();
        void setTime(std::string);
        std::string getTime();
    };
    
    #endif
    Why are you writing this information to a file instead of just filling in your class variables when you read the input file, instead of writing the file to an output file then opening and reading that file? File reading and writing is orders of magnitude slower than in memory operations.
    The format of the file I am reading is:
    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.
    I wanted to filter the information and have a way to check what was going wrong. So I made an output file checking if it was filtering correctly then just decided to read from that file too to see if I was still having the issue.

    Which worked and the output file had:

    Code:
    
    
    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


    ocdrew21, do yourself a favor and use


    That could create its own problems...
    Last edited by jocdrew21; 11-08-2014 at 04:57 AM.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I did that because I assumed no one would want to compile the code, I just showed the important code that pertains to the question. I thought I would annoy you guys by posting an entire program rather then narrowing down the issue and seeking advice.
    No what annoys me is that you only seem to read parts of the posts. Your "whole" program is not always necessary.
    But this is only a guess because you have again failed to provide the smallest possible complete program that can be compiled and ran to verify the problem.
    Note the highlighted section. The smallest possible complete program in this case could be something like:

    Code:
    #include <iostream>
    #include <fstream>
    
    void readFile()
    {
        // The following line is the only line added to your function. Why is this a member variable?
        std::string line;
    
        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 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<<" ";
            std::cout<<camera<<" ";
            std::cout<<time<<"\n";
            // Removed your setter functions.
        }
    }
    
    
    int main(int argc, const char * argv[]) {
        readFile();
    
        return 0;
    }
    This is assuming that the above snippet actually illustrates your problem. Now if this code doesn't illustrate the problem then you would have posted more of your code, which seems to be the case here. The code you provided doesn't produce the console output you reported in your first post. So I must assume that the problem is elsewhere, otherwise you would have posted different output. Does the code provided above exhibit the problems you stated in your first post? Also as I alluded to earlier your readFile() function can/should be greatly simplified.


    Is it possible that your actual input file differs from what you provided? The reason I ask is that there is not enough information in that file to calculate the speed of the vehicle.



    Jim

  8. #8
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    The reason I ask is that there is not enough information in that file to calculate the speed of the vehicle.
    The file has 72,000 vehicles, and 5 cameras. I just have to calculate the distance it traveled in seconds and convert it to MPH or KPH.

    Also as I alluded to earlier your readFile() function can/should be greatly simplified.
    I will simplify the readFile, I was just debugging...

    The code you provided doesn't produce the console output you reported in your first post. So I must assume that the problem is elsewhere, otherwise you would have posted different output. Does the code provided above exhibit the problems you stated in your first post?
    I have the same code and getting the same output. I will work this issue, sorry for your inconvenience Jim.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    [Providing] the smallest possible complete program that can be compiled and ran to verify the problem.
    I have found the above to be hard to do; but, about half the time it results in me finding the problem, myself.
    Because to do the above requires you to spend time thinking about the problem; this tends to help me help myself.

    I have wondered if "Troubling Shooting" is no longer or never was taught in schools.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by stahta01
    I have found the above to be hard to do; but, about half the time it results in me finding the problem, myself.
    Because to do the above requires you to spend time thinking about the problem; this tends to help me help myself.
    Several years ago, I started recommending that after reading an interview:
    Quote Originally Posted by Bjarne Stroustrup (October, 14 2000)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exibit the problem and send me that. Mostly, they then find the error themselves. "finding the smallest program that demonstrates the error" is a powerful debugging tool.

    I try to be helpful, but I don't do (other people's) homework, and I often recommend people to read their C++ textbook more carefully before mailing me. Or to find local help. There is only one of me and *many* C++ programmers and students.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I have the same code and getting the same output.
    What exactly was your output? The console output I get when trying your code from the original post, with the input you supplied is:

    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.
    Is that the output you're talking about? If not please show the output that you're talking about.

    Jim

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    When I said the readFile() function could be simplified this is about what I had in mind. Although I would open and check the files in the calling function instead.
    Code:
    #include <iostream>
    #include <fstream>
    #include <sstream>
    
    void readFile()
    {
       std::string line;
       // The opening the streams in this function is actually a bad idea
       // because there is no good way to inform the calling function that the
       // file openings failed, since exceptions are probably too advanced for you
       // at this time.
    
       // You need to check that the files opened correctly!
       std::ifstream reader("carSpeeds.txt");
       std::ofstream writer("outFile.txt");
       std::string plates, camera, camera_time;
    
       while (getline(reader, line))
       {
          std::istringstream sin(line);
          std::string trash;
          sin >> trash >> plates >> trash >> trash >> camera >> trash >> camera_time;
          // Erase the trailing period.
          camera_time.erase(camera_time.length() - 1);
          writer << plates << " " << camera << " " << camera_time << '\n';
       }
    }
    
    int verifyOutFile(std::istream &reader)
    {
       std::cout << "Verifing the output file. \n\n";
       std::string line;
       while(getline(reader, line))
          std::cout << line << '\n';
    
       return(0);
    }
    
    int main()
    {
       readFile();
    
       std::ifstream reader("outFile.txt");
    
       if(!reader)
       {
          std::cerr << "Couldn't open the outFile.txt file for verification.\n";
          return(1);
       }
    
       verifyOutFile(reader);
    
       return 0;
    }
    Jim

  13. #13
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Jim..

    Thank you, your way was a lot faster than my method of choice and easier to read! That example just changed how I will treat unwanted data in a file forever.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Just a note, in a "real" program I probably wouldn't be writing the intermediate file (writer) but would instead insert the information into a vector or some other container instead. And if these are the only files being used in the program I would probably open them very early in the program, check that they open correctly and if not abort the program.

    Also please study the program I supplied and notice the added vertical whitespace, IMO this added whitespace makes the program easier to read. And note how I used the class constructor to open the file instead of the open() function, and you always need to check to see that your files opened correctly. Lastly, for now, you should also strive to make your functions do as little as possible. This will make them much easier to test troubleshoot, and reuse.

    Lastly you still haven't said whether or not any of this solved your problem.

    Jim

  15. #15
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    file (writer) but would instead insert the information into a vector or some other container instead.
    I agree, I already created this program in java and used the following. The junk data was handled before this but you brought some unique light on how to handle that.

    Code:
    String line,time,plates;
      String cam;
      String[] info;
      
      while((line=in.readLine()) != null){
       Vehicle vehicle=new Vehicle();
       //break up each space and place the input into a variable
       info = line.trim().split(" ");
       plates = info[0];
       cam = info[1];
       time = info[2];
      
       //send data to vehicle class
       vehicle.setLicensePlates(plates);
       vehicle.setTime(time);
       vehicle.setCameraNumber(cam);
         
       vehicleList.add(vehicle);
    you should also strive to make your functions do as little as possible.
    I completely agree, all the added code was for debugging proposes. I should have added more comments on my thought process.

    Lastly you still haven't said whether or not any of this solved your problem.
    Yes it did and more. Thank you again...

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