Problem of reading in a txt file

I'm working on a project and need to read in txt file having format as below:
.row 12
.col 12

.blk 3
2 3 3 8
5 3 10 8
0 10 10 11

.net 2
Net1 3 9 11 11
Net2 6 2 6 9


Data that I need are:
1. the number after .row, .col, .blk, and .net, which means the amount of that object
2. lines consist of four numbers, which is the coordinate of that object(eg, in Net1 3 9 11 11, (3,9) is the coordinate of the net source and (11,11) is the coordinate of the net destination)

My code so far is as below:
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
ifstream infile;

int blk_x1, blk_x2, blk_y1, blk_y2;

vector<int> blk_x1_list;
vector<int> blk_x2_list;
vector<int> blk_y1_list;
vector<int> blk_y2_list;

infile >> line >> row_num;
cout << line << endl << row_num << endl;
	
infile >> line >> col_num;
cout << line << endl << col_num << endl; 
	
infile >> line >> blk_num;
cout << line << endl << blk_num << endl; 
	
while(getline(infile, line)) 
{
	ss << line;
	while(ss >> blk_x1 >> blk_x2 >> blk_y1 >> blk_y2)
	{
		blk_x1_list.push_back(blk_x1);
		blk_x2_list.push_back(blk_x2);
		blk_y1_list.push_back(blk_y1);
		blk_y2_list.push_back(blk_y2);
	}
	ss.str("");
	ss.clear();
} 


How should I complete it to read in the desired data?
Thx in advance!
Last edited on
If you want to read this kind of information in from a file, I would suggest you use a binary file instead; as they have the advantage of being able to store structures.

This would allow you to have multiple "objects" and store information on each object and then access the info on each individual object, instead of having to try and iterate through a text file.

Best,
max
I don't know how you need the required read data, but as a starter perhaps consider (as C++17):

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <utility>

struct Coord {
	unsigned x {};
	unsigned y {};
};

using SrcDst = std::pair<Coord, Coord>;

SrcDst getCoords(std::ifstream& fdata, bool hasname)
{
	std::string line;

	std::getline(fdata, line);

	std::istringstream iss(line);
	SrcDst sd;

	if (hasname) {
		std::string name;

		iss >> name;
	}

	iss >> sd.first.x >> sd.first.y >> sd.second.x >> sd.second.y;

	return sd;
}

int main()
{
	std::ifstream fdata("data.txt");

	if (!fdata)
		return (std::cout << "Cannot open input file\n"), 1;

	size_t row {}, col {};
	std::vector<SrcDst> srceDest;

	for (std::string line; std::getline(fdata, line); ) {
		std::istringstream iss(line);
		std::string type;

		iss >> type;
		if (type == ".row")
			iss >> row;
		else if (type == ".col")
			iss >> col;
		else if (type == ".blk" || type == ".net") {
			size_t elems {};

			iss >> elems;

			for (size_t e = 0; e < elems; ++e)
				srceDest.push_back(getCoords(fdata, type == ".net"));
		}
	}

	std::cout << "Row " << row << '\n';
	std::cout << "Col " << col << '\n';
	std::cout << "\nCoordinates\n";

	for (const auto& [srce, dest] : srceDest)
		std::cout << '(' << srce.x << ',' << srce.y << ")  (" << dest.x << ',' << dest.y << ")\n";

	std::cout << '\n';
}


which for the given data file displays:


Row 12
Col 12

Coordinates
(2,3)  (3,8)
(5,3)  (10,8)
(0,10)  (10,11)
(3,9)  (11,11)
(6,2)  (6,9)

Last edited on
Topic archived. No new replies allowed.