Reading in a file

Hi all,
I'm having issues reading in a file correctly. Yes, this is a school project but I only need help with a portion of it. I am only allowed iostream and fstream as libraries. I know exactly where the issue is but cannot seem to fix it.

Here is the file format :

Enterprise 89502
2014 Toyota Tacoma 115.12 {gps} 1
2012 Honda CRV 85.10 {camera lidar} 0 Mike
2011 Toyota Rav4 65.02 {} 0 Oscar
2009 Dodge Neon 45.25 {camera lidar radar} 1
2015 Ford Fusion 90.89 {lidar} 0 Juliet


Here is the output I receive when I read it in:
2014 Toyota Tacoma 115.12 2012 0 0\356\277\357\376
2014 Toyota Tacoma 115.12 2012 0 0\356\277\357\376
2014 Toyota Tacoma 115.12 2012 0 0\356\277\357\376
2014 Toyota Tacoma 115.12 2012 0 0\356\277\357\376
2014 Toyota Tacoma 115.12 2012 0 0\356\277\357\376


The issue is that the program tries to read in t_sensor as a boolean when there is a space between gps and lidar i.e. {gps lidar}

I need to create a catch for this.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  
    char charCheck;
    
    char fileName[256];
    ifstream infile;
    infile.open("HighTechAgency.txt");
    
    int t_year;
    char t_make[256], t_model[256];
    float t_price;
    char t_sensor[256];
    bool t_available;
    char t_owner[256];
    char t_agency[256];
    int t_zip;
    int sensorCount;
    int loopCount;
    char * sensorSpot;
  
       
     
    infile >> t_agency >> t_zip;
    
    cout << t_agency  << " " << t_zip << endl;
    
    for(int i = 0; i < 5; i++) {
        infile >> t_year >> t_make >> t_model >> t_price;
        
        charCheck = infile.peek();
        
        sensorCount = 0;
        
        for(int i = 0; i < 3; i++) {
            
            if(charCheck != 125) {
                sensorCount = 1;
                
                infile >> t_sensor;
                
                loopCount = 0;
                
                sensorSpot = t_sensor;
                
                if(*sensorSpot != 125) {
                    for(sensorSpot = t_sensor; *sensorSpot != 125 && *sensorSpot != '\0'; sensorSpot++) {
                        loopCount = 1;
                    }
                }
            }
        }
        
        
        
        infile >> t_available;
                if(!t_available) {
                    infile >> t_owner;
                }

                  cout << t_year << " " << t_make << " " << t_model << " " << t_price << " " << t_sensor << " " << t_available << " ";
            
             if(!t_available) {
                    cout << t_owner << endl;
                }
                else {
                    cout << endl;
                }
        }
   
        
    
}



Last edited on
kwilson7 wrote:
Enterprise 89502
2014 Toyota Tacoma 115.12 {gps} 1
2012 Honda CRV 85.10 {camera lidar} 0 Mike
2011 Toyota Rav4 65.02 {} 0 Oscar
2009 Dodge Neon 45.25 {camera lidar radar} 1
2015 Ford Fusion 90.89 {lidar} 0 Juliet

Do you have control over the data format (are you allowed to change it)? The bulk of the data, from the second line onwards, have inconsistencies which will make things a lot more complicated. For example, the Toyota Tacoma and Dodge Neon lines are missing people's names. It'd be a lot simpler of all data was space-separated, and for lines like "{camera lidar radar}" instead write it as "camera,lidar,radar" for example.

If detection or names are missing, you can write "none" or "unknown" so that the table stays at a consistent width at all times.
Last edited on
Since you're not allowed to use std::string or std::vector this much harder than it needs to be.

First you need to insure you limit the number of characters you will try to retrieve into a C-string to avoid buffer overflows. So you will either need to use the <iomanip> class or istream.getline() to retrieve your C-strings.

Second since you can't use std::vector you will need to dynamically allocate an array of C-strings to hold the options. The {} signifies the start and the end of the option block, the options are things like gps, camera, lidar, radar, etc., and note that these options may not exist in every line and that there may be a different number of options for each line.

Third since you can't use stringstreams finding the "missing" owner gets much harder as well.

This could be made easier if you can alter the structure of the file to use something other than a space to delimit the fields, like a comma perhaps.

1
2
3
4
Enterprise 89502
2014,Toyota,Tacoma,115.12,{gps},1,
2012,Honda,CRV,85.10,{camera,lidar},0,Mike
2015,,Fusion,90.89,{lidar},0,Juliet


Note that the first line ends with a comma instead of a name, this can be used to help identify the missing name.

Also note that in my example the last line has two commas next to each other, this signifies a missing field as well.


Last edited on
Topic archived. No new replies allowed.