How to read in information from a file and store them in 2D arrays

Hi, I am quite new to C++ and well, I need help with an assignment. I am supposed to read in a file containing a list of ISBN and book titles:
9780743486224 Angels and Demons
9780684835501 The Case for Mars

I know how to read the ISBN and the Titles in if I place them in two different files and declare two ifstream objects. But I am not allowed to do that - I need to read the ISBN in as int type and the Titles as char type and store them in separate arrays for later usage.
We were told to use 2D arrays, but I can't see how can that help.

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
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
const int nISBN = 13;
const int nTITLE = 50;
const int TOTAL = 30;           // number of books to be read in
void read_in(char Title[][nTITLE], int ISBN[], ifstream &input);

int main()
{
  ifstream inFile;
  inFile.open("books.txt");
  char title[nTITLE];
  int isbn[nISBN];

  if(inFile.fail())
  {
    cerr << "File could not be opened." << endl;
    exit(-1);
  }

  while (inFile.good())
  {
     read_in(title, isbn, inFile);
  }

  return 0;
}

void read_in(char Title[][nTITLE], int ISBN[], ifstream &input)
{
  for (int i = 0; i < TOTAL; i++)
// this is where the problem starts - how do I exactly read it in as int and char
// and how to store them into separate arrays 


I have absolutely no idea how to read in the information without causing a type mismatch...and how to use 2D arrays to store them.It will be great if someone can also explain how exactly does a complier read in a file.
Hello Dan,

Let me ask you this: Are you intending to use a 2D array to store the ISBN code under 1 column and the book name under the other?

Well that won't work, because an array must have a single type. Now you can declare a 1D array of pair<int, char *> or pair<int, string> but that would be overdoing it.

My solution for you is continue storing code and name in 2 different arrays but at the same index, say:

isbn[0] would give you 9780743486224 and title[0] would give you Angels and Demons. (Here title is assumed to be of type string)

To do that, read file as string or lines using the getline function in C (not C++ though the latter would do it too). This will allow you to read the entire line as one sentence, then use the sscanf function to extract the integer into isbn and the string into title. Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>


int main()
{
    char * text[] = {"9780743486224 Angels_and_Demons", "9780684835501 The_Case_for_Mars"};
    char  title[2][32];
    char  isbn[2][32];
    for (int i = 0; i < 2; i++)
    {
        char  ttitle[32];
        char   tisbn[32];
        sscanf(text[i], "%s %s", tisbn, ttitle);
        strcpy(title[i], ttitle);
        strcpy(isbn[i], tisbn);
    }
    /* display results */
    for (int i = 0; i < 2; i++)
    {
        std::cout << isbn[i] << ": " << title[i] << std::endl;
    }
    return 0;
}


I would use underscores for names, as it is much easier to read them. You can freely extract them later. Plus, no such ISBN numbers fit in int, not even in an unsigned long. So it's better storing them as C strings. You also need to be careful when dealing with arrays of C strings as it appears to be dealign with 2D arrays (a single word/C string is a 1D array).
Last edited on
Toni - why do you suggest using C in C++? There is no reason to do that. Other than that, what is your problem with std::pair? (Well, other than the fact that a normal struct would do in this case, as you are using just one kind of combination anyways)
Thanks Toni,

I've managed to read them in using strings, like what you have suggested. There're supposed to be two separate 2D arrays, one for isbn and title. I need to maintain the isbn as int or other numerical types to be used with bubble sort afterwards.
Topic archived. No new replies allowed.