fstream file read limiting

Hello, im designing a class where a user inputs are saved onto a file, then later retrieved to the program for other purposes, here is my relevant storing code:
1
2
3
4
5
6
7
8
9
10
11
fstream& AmaProduct::store(fstream& file, bool addNewLine)const{
		if (addNewLine == true){
			file << fileTag_ << "," << sku() << "," << name() << "," << price() << "," << taxed() << "," << quantity() << "," << unit() << "," << qtyNeeded() << endl;
			return file;
		}
		else {
			file << fileTag_ << "," << sku() << "," << name() << "," << price() << "," << taxed() << "," << quantity() << "," << unit() << "," << qtyNeeded();
			return file;
		}

	}


and here is my relevant loading/reading code from my file. Between line 43-44 is when im reading my Unit and storing it. However it reads too much. for example, if my stored data was
 
N,1234,box,123.45,1,1,kg,5

unit would end up reading kg,5 instead of just kg. How do i stop it and let it ONLY read kg?

my unit_ is declared in my header file as so:

1
2
 
char unit_[11];


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
fstream& AmaProduct::load(std::fstream& file){
		char sku2[25];
		char name2[25];
		double price2;
		int taxed2;
		int quantity2;
		
		int qtyneed2;

		file.getline(sku2, 25, ',');
		sku(sku2);

		// -----------------------------
		file.getline(name2, 25, ',');
		name(name2);
		// -----------------------------
		file << fixed;
		file << setprecision(2);
		file >> price2;
		price(price2);
		file.ignore(1);
		// -----------------------------
		file >> taxed2;
		taxed(taxed2);
		if (taxed2 == '1'){
			taxed3 = false;
		}
		else if (taxed2 == '0') {
			taxed3 = true;
		}
		file.ignore(1);
		cout << taxed2 << endl;
		// -----------------------------
	
		file >> quantity2;

		quantity(quantity2);
		



		file.ignore(1);
		file >> unit_;
		unit(unit_);
		
		
		file.ignore(1);
	
		file >> qtyneed2;
		qtyNeeded(qtyneed2);
		
		return file;
	}


my unit() setter:
1
2
3
4
5
6
7
8
9
const char* AmaProduct::unit()const{

		return unit_;
	}
	void AmaProduct::unit(const char* value){

		strncpy(unit_, value, strlen(unit_ + 1));

	}

Last edited on
First why is unit a C-string instead of a std::string?

Now to answer the question you can, and should, limit the number of characters that the extraction operator>> will try to retrieve into a C-string by using the setw() manipulator.
 
file >> setw(11) >> unit_; // Always limit the number of characters being retrieved into a C-string. 


But in this situation you should be using getline() with the optional third parameter, the ','. Remember that there are two different getline() functions, one that works with std::string, and another that works with a C-string, make sure you use the correct version for the data type you're using.

Please don't create multiple topics for the same problem. http://www.cplusplus.com/forum/beginner/188048/
Last edited on
Also a tip on store:
1
2
3
4
5
6
7
8
fstream& AmaProduct::store(fstream& file, bool addNewLine)const{
	file << fileTag_ << "," << sku() << "," << name() << "," << price() << ","
	      << taxed() << "," << quantity() << "," << unit() << "," << qtyNeeded();
	if (addNewLine) {
		file << '\n';  // endl is slow because it flushes the stream. Use '\n' instead
	}
	return file;
}
Last edited on
Also you may want to consider using an ostream& instead of the fstream&, then you can use this function with any output stream.



Topic archived. No new replies allowed.