How to put conditions on parsed data

Can someone tell me why this will not work

#include <iostream>
#include <fstream>
#include <sstream>
#include <array>
#include <cmath>
using namespace std;

int row = 0;
int col = 0;

int main()
{

ofstream myoutfile;
myoutfile.open("results.txt");
myoutfile << "Writing this to a file.\n";

string Hostname;
string IPaddress;
string Patched;
string OSversion;
string Notes;

fstream file("sample.csv");

int array[100][100] = { { 0 } };

if (file.is_open())
{

while (file.good())
{
array[row][col] = 0;

getline(file, Hostname, ',');
Hostname = array[row][0];

getline(file, IPaddress, ',');
IPaddress = array[row][1];

getline(file, Patched, ',');
Patched = array[row][2];

getline(file, OSversion, ',');
OSversion = array[row][3];

getline(file, Notes);
Notes = array[row][4];

row++;


// Is the router already patched?
if (col = 2)
{
if (Patched != yes)
{
myoutfile << Hostname << ", ";
myoutfile << IPaddress << ", ";
myoutfile << Patched << ", ";
myoutfile << OSversion << ", ";
myoutfile << Notes << endl;
}
}
}
}
}
Last edited on
Your code doesn't compile:
Error C2065 'yes': undeclared identifier ConsoleApp c:\users\vc\desktop\forum 2015\consoleapp\main.cpp 57

if (col = 2)
I guess you meant if (col == 2), but it will never be true.

Your code makes no sense.
1
2
getline(file, Hostname, ',');
Hostname = array[row][0];


First you read Hostname into a string variable and then you assign an int to it which will always be 0.

Maybe you can tell us in plain English what you want to do.
I'm trying to read in from an excel file, it is supposed to be a router check. I have attempted to parse it. I then want to go to the column where it says whether or not it has already been patched as I only want to print out to results.txt the ones that have not been patched.
Your code is far too complicated. You basically need to read the data and only if the conditions is right to write it to the output. No need for an array.
I would do it like 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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>

using namespace std;

string trim(const string& s)
{
  if (s.empty())
    return "";

  size_t i = 0;
  while (s[i] == ' ' && i < s.size())
    i++;

  return s.substr(i);
}

int main()
{
  ifstream src("sample.csv");
  if (!src)
  {
    perror("Error opening file sample.csv: ");
    return -1;
  }
  ofstream dest("results.txt");
  if (!dest)
  {
    perror("Error creatting results.txt: ");
    return -1;
  }
  string Hostname;
  string IPaddress;
  string Patched;
  string OSversion;
  string Notes;

  cout << "Start reading the data from sample.csv\n";
  while (getline(src, Hostname, ','))
  {
    getline(src, IPaddress, ',');
    getline(src, Patched, ',');
    Patched = trim(Patched);
    getline(src, OSversion, ',');
    getline(src, Notes);
    if (Patched == "true") // TODO check condition like "1" or whatever the field contains
    {  
      dest << Hostname << ", " << IPaddress << ", " << Patched << ", "
           << OSversion << ", " << Notes << endl;
    }
  }  
}
Topic archived. No new replies allowed.