Can't fill in 2d array from comma-delimited text file

Oct 26, 2021 at 11:13am
So basically my text file looks like this:
1
2
3
4
Rico, Suave
Erden, Alperen
Mac, Crip
Migos, Quavo



I'm just trying to fill up a 2d array with the names.
When I run it, it compiles, but doesn't display anything.
So I think the 2d array isn't being filled up.


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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
  string names[4][2];

  string line;

  ifstream readFile("Line.txt");

  int i, j;

  for (i = 0; i < 4; i++)
  {
    for (j = 0; j < 2; j++)
    {
      if (getline(readFile, line, ','))
      {
        names[i][j] = line;
        cout << names[i][j] << endl;
      }
    }
  }

  readFile.close();

  return 0;
}
Last edited on Oct 26, 2021 at 2:34pm
Oct 26, 2021 at 11:39am
> if (getline(readFile, line, ','))
Your first field ends with a comma.
But your second field ends with a newline.
Oct 26, 2021 at 11:44am
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    string names[4][2];
    
    string fname,lname;
    
    ifstream readFile("line.txt");
    
    if(!readFile)
        cout << "Failed\n";
    
    for (int i = 0; i < 4; i++)
    {
        getline(readFile, fname, ',');
        getline(readFile, lname);
        names[i][0] = fname;
        names[i][1] = lname;
        
        for (int j = 0; j < 2; j++)
        {
            cout << i << ' ' << j << ' ' << names[i][j] << endl;
        }
    }
    readFile.close();
    
    return 0;
}


0 0 Rico
0 1 Suave
1 0 Erden
1 1 Alperen
2 0 Mac
2 1 Crip
3 0 Migos
3 1 Quavo
Program ended with exit code: 0

Oct 26, 2021 at 2:34pm
Thanks a lot for the answers.
I made a mistake in the text file.

They are actually delimited with comma and then a space.
How would I amend the getline code to separate that?
Last edited on Oct 26, 2021 at 2:39pm
Oct 26, 2021 at 2:50pm
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

struct Person
{
   string first, last;
   friend istream &operator >> ( istream &in, Person &p )
   {
      getline( in, p.last, ',' );
      getline( in >> ws, p.first );
      return in;
   }
};


int main()
{
// ifstream readFile( "Line.txt" );
   istringstream readFile( "Rico, Suave\n"
                           "Erden, Alperen\n"
                           "Mac, Crip\n"
                           "Migos, Quavo\n" );

   vector<Person> people;
   for ( Person p; readFile >> p; people.push_back( p ) );

   for ( Person p : people ) cout << p.first << " " << p.last << '\n';
}


Suave Rico
Alperen Erden
Crip Mac
Quavo Migos
Oct 26, 2021 at 3:15pm
The correct way to read CSV is actually very, very obnoxiously complicated. But if you can restrict it enough (as you have here) then life gets a whole lot simpler.

Trimming leading and trailing whitespace belongs to a function set variously named “trim” something something... Here is the simplest of the simple version of trim:

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
#include <ciso646>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

string trim(string s)
{
  s.erase(s.find_last_not_of(" ")+1);
  s.erase(0, s.find_first_not_of(" "));
  return s;
}

int main()
{
  const int max_names = 100;
  string names[max_names][2];
  int num_names = 0;
  {
    ifstream f("Line.txt");
    string line;
    while (getline(f, line) and (num_names < max_names))
    {
      istringstream line_stream(line);
      string field;
      
      getline(line_stream, field, ',');
      names[num_names][0] = trim(field);
      
      getline(line_stream, field);
      names[num_names][1] = trim(field);
      
      num_names += 1;
    }
  }
  
  for (int n = 0; n < num_names; n++)
  {
    cout << (n+1) << ": \"" << names[n][0] << " " << names[n][1] << "\"\n";
  }
}

Notice how I put the fstream stuff in braces {}.
This makes all the names used there local to those braces, and does nice things like automatically close the file when we’re done with it.

Hope this helps.
Oct 26, 2021 at 11:19pm
They are actually delimited with comma and then a space.

@OP
How is that different from the sample you gave originally?
Topic archived. No new replies allowed.