Stringstream malfunction

Hello everyone,

I have created a program to solve a mathematics problem. This file parses one file to eliminate certain possible answers, the possiblities of which are stored in another file.

The program is reading the first file just fine -- the problem comes when it's time to parse the line just read. I'm using a stringstream to store and interpret the line; there are several expected beginnings of a line and the program takes a different direction depending on whether the line starts with a letter or number. The first round works exactly as expected. However, during the second round, remainder and divisor are both being set to zero, despite the fact that buf is capturing the file data just fine. I conclude that there's a kink in the transfer between C string and stringstream, but I can't figure out what it is. Could you lend me a hand?

The relevant snippet of code is:
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
char buf[128];
	BigI remainder, divisor, number;	// classes of form rem mod div
	// number is the value from the sieve.dat file
	stringstream parse_res;			// stream for parsing the residue.dat file
	residue.getline(buf, 128);		// getline discards '\n'
	cerr << "First line of " << RESIDUE << " read!\n";
	while (!residue.eof())	
	{
		cerr << "Contents of buffer: " << buf << '\n';
		parse_res.write(buf, 128);	// \0 already inserted by getline
		// parse_res.flush();
		char is_cycle = parse_res.peek();
		cerr << "Contents of parse_res after peek: " << dec << parse_res << '\n';
		cerr << is_cycle << '\n';
		if (is_cycle == 'C')
		{
			to_temp << buf << '\n';	// just put the whole 'CYCLE' line in
			continue;	// next line!  nothing else to see here
		}
		if (is_cycle == 'D') break;	// 'Done.'
		cerr << "I just read a line of numbers from " << RESIDUE << "! \n";
		// parse_res.putback(is_cycle);
		parse_res >> remainder >> divisor;

		while (1)
		{
			from_sieve >> number;
			if (from_sieve.eof()) break;
			if (from_sieve.fail())	// trips over string for cycle
			{
				from_sieve.clear();
				
				char buf2[128];
				from_sieve.getline(buf2, 128);
				to_temp << buf2 << '\n';
				
				continue;	// get another number
			}
			from_sieve.ignore();	// ignore '\n' at end
			cerr << "number: " << number << "\nremainder: " << remainder
			<< " divisor: " << divisor << "\nresult: " << ((number - remainder) % divisor) << "\n\n";
 
			if ((number - remainder) % divisor != 0)
				to_temp << number << '\n';
			to_temp.flush();	// make sure file writing buffer flushes
		}
		from_sieve.clear();	// clear after EOF condition

		from_sieve.close();
		to_temp.close();
		from_sieve.open(TO_TEMP);	// the new sieve.dat
		if (from_sieve.fail()) { cerr << "Could not reopen " << TO_TEMP << " \n"; return 1; }
		
		remove(FROM_SIEVE);
		rename(TO_TEMP, FROM_SIEVE);
		
		to_temp.open(TO_TEMP);	// a new temp.dat
		if (to_temp.fail()) { cerr << "Could not create a new " << TO_TEMP << " \n"; return 1; }

		DEBUG();
		residue.getline(buf, 128);
	}


and on the command line I get:

All files open! 

First line of residue_test.dat read!

Contents of buffer: 0 4

Contents of parse_res after peek: 0x7fffcd177208
0

I just read a line of numbers from residue_test.dat! 

number: 0 remainder: 0 divisor: 4
 result: 0
 

number: 1 
remainder: 0 divisor: 4 
result: 1


number: 2
 remainder: 0 divisor: 4
 result: 2

 
number: 3 
remainder: 0 divisor: 4
 result: 3


number: 4
 remainder: 0 divisor: 4
 result: 0


number: 5 
remainder: 0 divisor: 4 
result: 1


number: 6 
remainder: 0 divisor: 4 
result: 2


number: 7
 remainder: 0 divisor: 4 
result: 3


number: 8 
remainder: 0 divisor: 4 
result: 0



Press any key to continue 

Contents of buffer: 2 4

Contents of parse_res after peek: 0x7fffcd177208


I just read a line of numbers from residue_test.dat! 

number: 1
 remainder: 0 divisor: 0
 result: 0


number: 2
 remainder: 0 divisor: 0
 result: 0

etc.

Topic archived. No new replies allowed.