I'm trying to read from a text file and put them into different members of structure

1
2
3
4
5
6
typedef struct
{
    int isbn_code, year_published, quantity, rack, level_no;
    char author[50], title[100], publisher[50];
    double price;
}DATA;


Shown above is my typedef struct.
And when i debug, it only shows "1. ".
So here is my code.

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
int file_reader()
{
	DATA d;
	ifstream infile("books.txt", ios::in);
	if (infile.is_open())
	{
		while (!infile.eof())
		{
			for (int row = 1; row < 100; row++)
			{
				cout << row << ". ";
				char line[200];
				cin.ignore();
				cin.getline(line, 200, '\n');

				char* column = strtok(line, ",");

				while(column)
				{
					cin >> d.isbn_code;
					column = strtok(NULL, ",");
					cin.getline(column, 50);
					strcpy(d.author,column);
					column = strtok(NULL, ",");
					cin.getline(column, 100);
					strcpy(d.title, column);
					column = strtok(NULL, ",");
					cin.getline(column, 50);
					strcpy(d.publisher, column);
					column = strtok(NULL, ",");
					cin >> d.year_published;
					column = strtok(NULL, ",");
					cin >> d.quantity;
					column = strtok(NULL, ",");
					cin >> d.price;
					column = strtok(NULL, ",");
					cin >> d.rack;
					column = strtok(NULL, ",");
					cin >> d.level_no;
				}
				cout << d.isbn_code << "," << d.author << "," << d.title << "," 
<< d.publisher << "," << d.year_published << "," << d.quantity << "," 
<< d.price << "," << d.rack << "," << d.level_no;
			} cout << endl;
		} infile.close();
	}
	else
		cout << "File is not open\n";
	return 0;
}
Last edited on
Please help out and tell me what's wrong!!
My first suggestion would be that you decide whether you're programming in C or C++. If C use printf(), FILE, and fgets(). If C++ use C++ strings, file streams, stringstreams and cout to print to the console.

Next you open a file for reading but you never read from that file, why?

What exactly are you entering for the input string inside that "read" loop?

I've corrected some of the parts. But there is still some error.
Here is an example from one of my data : 9789671539842,Zedeck Siew / Sharon Chin,Creatures of Near Kingdom,Vinlin Press Sdn Bhd,2018,1,24.00,2,2.
When I debug, the first data which is the isbn code will not be shown properly, for the first line, it would show 0, while for the rest of the lines, it would show numbers that were not from my text file, how can I fix that?

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
int file_reader()
{
    DATA d;
    ifstream infile("books.txt", ios::in);
    if (infile.is_open())
    {
        while (!infile.eof())
        {
            for (int row = 1; row < 42; row++)
            {
                cout << row << ". ";
                char line[200];
                infile.getline(line, 200, '\n');
                char* column;
                do
                {
                    column = strtok(line, ",");
                    d.isbn_code = atoi(column);
                    column = strtok(NULL, ",");
                    strcpy(d.author,column);
                    column = strtok(NULL, ",");
                    strcpy(d.title, column);
                    column = strtok(NULL, ",");
                    strcpy(d.publisher, column);
                    column = strtok(NULL, ",");
                    d.year_published = atoi(column);
                    column = strtok(NULL, ",");
                    d.quantity = atoi(column);
                    column = strtok(NULL, ",");
                    d.price = atof(column);
                    column = strtok(NULL, ",");
                    d.rack = atoi(column);
                    column = strtok(NULL, ",");
                    d.level_no = atoi(column);

                    cout << d.isbn_code << "," << d.author << "," << d.title << "," << d.publisher << "," 
<< d.year_published << ","  << d.quantity << "," << fixed << setprecision(2) << d.price << "," 
<< d.rack << "," << d.level_no << endl;

                } while (column=="\0");
            } 
        } infile.close();
    }
    else
        cout << "File is not open\n";
    return 0;
}

Last edited on
Please read https://www.cplusplus.com/articles/jEywvCM9/ and edit your posts for readability.
oh ok thanks!
How many loops does it take to read a file just once?
1
2
3
4
5
6
7
8
9
        while (!infile.eof())
        {
            for (int row = 1; row < 42; row++)
            {
                infile.getline(line, 200, '\n');
                char* column;
                do
                {
                } while (column=="\0");

You have 3 nested loops which keep going over the same things.


Try something like
1
2
3
4
5
6
7
8
char line[200];
while ( infile.getline(line, 200, '\n') ) {
  column = strtok(line, ",");
  d.isbn_code = atoi(column);
  column = strtok(NULL, ",");
  strcpy(d.author,column);
  // etc
}

Oh yeah! Thank you so much for helping out!!
Topic archived. No new replies allowed.