Help reading data from a text file

I'm writing a program to read 2 polynomials from a plain text file, separated by a line that contains the string "XXX". The first column contains a coefficient and the second contains the exponent. So for example, this:
1
2
3
4
5
6
5 2
3 1
1 0
XXX
7 1
9 0

would represent the polynomials 5x^2 + 3x + 1 and 7x + 9. I can use a simple while loop to read in the first polynomial, but I'm not sure how to skip over the XXX line and read the remainder of the file into another polynomial.
Last edited on
This is how I would do it:
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
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;

struct num
{
	double coef;
	double exp;
};

int main()
{
	ifstream fin("input.txt");
	vector<num> poly1;
	vector<num> poly2;

	bool poly1_done = false;

	while (!fin.eof())
	{
		string line;
		getline(fin,line);
		
		if (line == "XXX")
		{
			poly1_done = true;
			continue;
		}

		stringstream ss(line);
		num temp;
		ss >> temp.coef;
		ss >> temp.exp;

		if (!poly1_done)   poly1.push_back(temp);
		else               poly2.push_back(temp);
	}
}
Last edited on
Thanks for the quick response so late (at least where I am), I need to represent these polynomials using a linked list though, so it'd be better to avoid vectors (or would it? I'm still a newbie). I'm trying to test it out, before starting in on the linked list, by storing the coefficients and exponents from the first polynomial with a while loop and keeping a line count, then using infile.seekg(lines*5+5, ios::beg) to skip to the location of the next coefficient (skip 5 bytes per line, plus 5 more for the XXX line), but it doesn't seem like seekg is moving the file pointer at all because both of my polynomials are showing the same value after I try that.
This is my first time trying linked lists. I think this would work. I'm not entirely sure why you are using seekg. The file object keeps track of everything that is going on so just don't go back to the front while reading.

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

struct num
{
	double coef;
	double exp;
	num* next;
	num* first;
	bool i_am_last;
	void push_back(num in)
	{
		next = new num;
		next->coef = in.coef;
		next->exp = in.exp;
		next->first = first;

		i_am_last = false;
		next->i_am_last = true;
	}
};

int main()
{
	ifstream fin("input.txt");
	num* poly1 = new num;
	num* poly2 = new num;
	poly1->first = poly1;
	poly2->first = poly2;

	bool poly1_done = false;

	string line;
	while (getline(fin,line)) // if .eof() didn't work, do this
	{
		if (line == "XXX")
		{
			poly1_done = true;
			continue;
		}

		stringstream ss(line);
		num temp;
		ss >> temp.coef;
		ss >> temp.exp;

		if (!poly1_done)   
		{
			poly1->push_back(temp);
			poly1 = poly1->next;
		}
		else
		{
			poly2->push_back(temp);
			poly2 = poly2->next;
		}
	}
}
Last edited on
Thanks, that
1
2
3
4
5
if (line == "XXX")
{
     poly1_done = true;
     continue;
}

combined with the stringstream is more or less what I needed.
I have another question now...after reading from the text file to the list, I need to combine the like terms and sort them in descending order. I combined the terms into a dynamic array sum = new int[degree+1] (the plus 1 is necessary to include exponents of 0), with the index ranging from 0 to the degree of the polynomial. The sum of coefficients is stored at sum[exponent]. Now I just need to convert this back to a linked list. At first I tried to reset the linked list then use a for loop to iterate through the array, but then I don't think it would be possible to set the *next node doing it that way.

So basically, how can I convert this dynamic array back to a linked list? The only idea I'm getting right now is to write them all to a new text file and then read it back into a list since I already have the code working for that, but this method seems extremely inefficient, there has to be a better way.
Do you absolutely need a linked list? Vectors are an easy/safe way to do this.

Also, if you are thinking that you need to write to a file for something temporary, use a stringstream. It's like a file, but stored in memory. It's fast and gets discarded when you're done with it. Much better than a file which may slow down your computer based on the speed of your hard drive.
Topic archived. No new replies allowed.