opening txt or dat files-not technical

My assignment says that I have to Put sales2.dat in the project folder that contains the program file. I'm having a hard time interperting that statement. I scoured my book and forums, but still am stumped.
Just create a file with no path
eg: ofstream("sales2.dat");
That file should be stored in the same folder of your program
Thanks bazzy, but what do you mean by storing it in the same folder? Here's some 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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
	const int NUM_DIVS = 3;           // Number of divisions
   const int NUM_QTRS = 4;           // Number of quarters
	double sales[NUM_DIVS][NUM_QTRS]; // 2D array with 3 rows & 4 columns
	double totalSales = 0;	          // Accumulates total sales
	int div, qtr;			             // Loop counters
	ifstream datafile;                // Used to read data from a file
	
   datafile.open("sales2.dat");
   if (!datafile)
      cout << "Error opening data file.\n";
   else
   {
      cout << fixed << showpoint << setprecision(2);
      cout << "Quarterly Sales by Division\n\n";

	   // Nested loops are used to fill the array with quarterly
		// sales figures for each division and to display the data
		for (div = 0; div < NUM_DIVS; div++)
	   {	for (qtr = 0; qtr < NUM_QTRS; qtr++)
		   {
			   cout << "Division "  << (div + 1) 
                 << ", Quarter " << (qtr + 1) << ": $";
            datafile >> sales[div][qtr];
            cout << sales[div][qtr] << endl;
		   }
		   cout << endl; // Print blank line.
	   }
      datafile.close();

	   // Nested loops are used to add all the elements
	   for (div = 0; div < NUM_DIVS; div++)
	   {	for (qtr = 0; qtr < NUM_QTRS; qtr++)
			   totalSales += sales[div][qtr];
	   }
      // Display the total
		cout << "The total sales for the company are: $";
	   cout << totalSales << endl;
   }
	
	return 0;
}
'Folder' means 'directory' on Windows
Whatever folder/directory that the exe file for your code is in is the same folder/directory that you need to put sales2.dat in.
do you want to open in Binary mode ??

if than use ios::binary while opening ....
I actually changed it a txt file ,but I'm still having trouble reading it. I did a similar project and i was able to write it to a file. The txt file does exist I'm just not sure whats going on. I have the info set in notepad and it's saved, I then closed the file and try to read it with my program. If I'm missing a step please let me know. Thanks guys. Oh I don't believe I need it in binary mode.
Last edited on
OH !!! if the data stored there is in binary form, than you cant read it directly .....

beczuse,

in binary filing the data is stored as it is , for example if there is number 1234567 in bin binary file it will be saved as one integer ,( if data type is int ) ,

while if you store it in txt file it is stored as a single character here 1 , 2 and all will be treated as different characters ...

so thats why you cant read it ......
here is what I have in my txt file

31569.45 29654.23 32982.54 39651.21
56321.02 54128.63 41235.85 54652.33
29654.35 28963.32 25353.55 32615.88

I'm following the examples in my book and I'm not seeing anything referencing your suggestion.
Topic archived. No new replies allowed.