Reading a text file that contains words and numbers

I have a xlsx file that I saved in tab delimited format. The first 9 lines looks like this.

Line 1
Line 2
Line 3
Line 4
Line 5
2 assess xxxxxxx RCRR L1L 1234.3 1234.3 1234.3 1234.3 1234.3 1234.3
2 assess xxxxxxx RCRR L1L 1234.3 1234.3 1234.3 1234.3 1234.3 1234.3
2 assess xxxxxxx RCRR L1L 1234.3 1234.3 1234.3 1234.3 1234.3 1234.3
2 assess xxxxxxx RCRR L1L 1234.3 1234.3 1234.3 1234.3 1234.3 1234.3
.
.
.

What I've done so far:
1. open the file successfully
2. count the number of lines the file contains.

What I want to do:
-I want to print the first 4 lines
-skip the fifth line
-store the last 6 columns of the remaining lines in arrays so I can use them for processing.

Like I said the file is originaly in xlsx format so I used the word "columns".Can anyone help me? Thanks in advance
use getline to read the entire line:

http://cplusplus.com/reference/string/string/getline/?kw=getline


further use 2 [for] loops to read the lines that should be printed. within the second loop store the line in the array. And the line that should be skipped, well, read it and do nothing with it.

use stringstream to get the columns from the read lines (rows):

http://cplusplus.com/reference/sstream/stringstream/?kw=stringstream
The best way is to read one line at a time, That way if one line is missing data or has corrupt data, then it shouldn't affect the other lines.

If however you know that your input file will contain a certain format, such as xyz 123.0

The easy solution is to simply read in each value.
1
2
3
4
string stringin;
double numberin=0;
   // while not EOF loop goes here
inputfile >> string in >> numberin;

I'm still lost at this point. Here is what I have right now.

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <math.h>


using namespace std;



int main(void)
{ //opening the file
char filename[100];
ifstream inputfile;

cout<<"Enter the name of the file to be opened: ";
cin>>filename;

inputfile.open(filename, ios::in);

if(inputfile.fail())
{ cout<<"Opening"<<filename<<"for reading\n";
cout<<"--------------------------------\n";
cout<<filename<<" file cannot be opened!\n";
cout<<"Possible Errors:\n";
cout<<"1. The file does not exist.\n";
cout<<"2. The path was not found.\n";
}
else
{ cout<<"Opening"<<filename<<"for reading\n";
cout<<"--------------------------------\n";
cout<<filename<<" was opened successfully!\n";
cout<<"Do processing..Please wait...\n";
}

int number_of_lines=0;
std::string line;


while(std::getline(inputfile, line))
{ ++number_of_lines;
cout<<line<<"\n";
}

cout<<"number of Lines: "<<number_of_lines<<"\n";

double start_distance[number_of_lines-5];
double end_distance[number_of_lines-5];
double average_iri[number_of_lines-5];
double speed[number_of_lines-5];
double left_wheel_path[number_of_lines-5];
double right_wheel_path[number_of_lines-5];


//closing the file
inputfile.close();

if(inputfile.fail())
{ cout<<"The file cannot be closed:\n";
}
else
{ cout<<"\nThe file was successfully closed\n";
}

system("pause");
return 0;
}


please help. Thanks. :)
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
    const int array_size = 100;
    int array_count = 0;
    double start_distance[array_size];
    double end_distance[array_size];
    double average_iri[array_size];
    double speed[array_size];
    double left_wheel_path[array_size];
    double right_wheel_path[array_size];

    int number_of_lines = 0;
    std::string line;
    std::string dummy;
    char tab = '\t'; // items are delimied by a tab character

    while (std::getline(inputfile, line))
    {
        ++number_of_lines;
        if (number_of_lines < 5)
        {
            cout << line << "\n";
        }
        else if (number_of_lines > 5)
        {
            istringstream ss(line);
            // ignore first five columns
            getline(ss,dummy, tab);
            getline(ss,dummy, tab);
            getline(ss,dummy, tab);
            getline(ss,dummy, tab);
            getline(ss,dummy, tab);

            ss >> start_distance[array_count];
            ss >> end_distance[array_count];
            ss >> average_iri[array_count];
            ss >> speed[array_count];
            ss >> left_wheel_path[array_count];
            ss >> right_wheel_path[array_count];

            array_count++;
        }
    }
thanks. I'll try it now. :))))
it works. :) Thanks. :))

Can I open another file using the same format in the same program? :))
What do you want to do exactly? Add the data from the new file after the data from the first file, or start again with everything empty?

I'd see two main possibilities.
Either
1. Just put a while loop around the existing code
or
2. Put all the detailed code in a separate function and just call it as many times as required.
Last edited on
Thanks for the help buddies!! :))))))))
Topic archived. No new replies allowed.