Writing to a file problem

Dec 8, 2012 at 5:57am
Hi all,
I have a problem writing to a file. I don't know where I'm wrong.
Any help will be greatly appreciated. My 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
#include <iostream>
#include <math.h>
#include <fstream>
#include <ios>
using namespace std;

int main()
{
	long double x = 0;
	long double y = 0.0000001;
	long double z = 0;

	ofstream a_file ( "C:\\tmci\\RawData\\Sqr_root.txt",  ios::app );
	if (! a_file )
	{
		cout << "Can't open input file " << " Sqr_root.txt " << endl;
		cin.get();
		exit(1);
	}

	for (int i = 0; i < 49; i++ )
	{
		x = (x + y);
		cout << x ;
		z = (x * x);
		cout << "    " << z << endl << endl;

		a_file <<  x << "    " << z ;

		//system ("Pause");
	}

	system ("Pause");
	return 0;
}
Last edited on Dec 8, 2012 at 3:06pm
Dec 8, 2012 at 9:14am
1. Why do you create a_file? It serves no purpose. b_file will create the requested file since it is an outstream.

2. You don't need to keep re-opening b_file during your for loop. Initialize it right before the loop itself.

3. Your problem is probably due to admin rights.
Dec 8, 2012 at 10:02am
I have same problem...
Dec 8, 2012 at 3:12pm
Blacksheep, thanks for your reply. As seen above, I followed your suggestion but my program still does not write to a_file. What am I doing wrong? Can you please give me an example? Thanks. . . . therry
Dec 8, 2012 at 5:15pm
@therry1

I compiled your program, and it worked perfectly, after I created the 'tcmi' and 'RawData' directories in the root of the C drive, since that is where the program is looking for it.
Dec 8, 2012 at 6:19pm
whitenite1, thanks for your reply. What would stop it from working on my computer? I use Windows 7 (x64) and Microsoft visual studio pro 2010. It works perfectly on screen, but it will not write to the file.
Thanks. . . .therry
Last edited on Dec 8, 2012 at 6:20pm
Dec 8, 2012 at 6:30pm
@therry1

Not sure, since I also use Windows 7 on a x64, and I use Visual Studio 2012 Express. The only thing that comes to mind is that one of the two directories, have a different spelling than what you're looking for in the program. Try removing "C:\\tmci\\RawData\\Sqr_root.txt" and replace it with just "Sqr_root.txt" instead. Then check the directory the <name of your program>.cpp is in, and see if the text file is in there. If it is, then the location you were checking originally, must be wrong. Hope this helps..
Dec 8, 2012 at 6:30pm
Hmm. Seemly worked it..fine because I am using on Dev C++ and Windows Vista.
Dec 8, 2012 at 7:37pm
whitenite1, Thanks for your help. I've tried everything that I could think of. Since the search for the file is outside of the for loop, it has no effect on the program. I'm beginning to suspect the line a_file << x << " " << z ; I don't know if it is really correct. . . or is there another way to write to a file? I wonder why the program does not complain when I build it. . . .therry
Dec 8, 2012 at 8:06pm
@therry1

I modified your program a bit, by removing the #include <ios> ,
and the if(! a_file) section. This still works. Try 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
// Write File.cpp : main project file.
#include <iostream>
#include <math.h>
#include <fstream>

using namespace std;

int main()
{
	long double x = 0.0;
	long double y = 0.01;
	long double z = 0.0;
	ofstream a_file( "C:\\tmci\\RawData\\Sqr_root.txt"); // Or try changing to
                                            // ofstream a_file( "C:\\Temp\\Sqr_root.txt");, then check Temp dir for result
		
	for (int i = 0; i < 49; i++ )
	{
		x = (x + y);
		cout << x ;
		z = (x * x);
		cout << "   " << z << endl;

		a_file <<  x << " " << z << endl; // Added a newline, so each set of numbers is on a separate line

	}
	a_file.close(); // Probably not needed, but still good idea to close files 
	system ("Pause");
	return 0;
}
Last edited on Dec 8, 2012 at 8:09pm
Dec 10, 2012 at 10:30am
whitenite1, thanks again for your replies. I tried the above code, it reacts the same as mine. Both works well on screen, but both will not write to a_file regardless of the directory that Sqr_root.txt is in. If the above code writes to a_file on your computer, can you please email me a copy of "Sqr_root.txt" file so I can check it against my computer? Send it to <therry1@yahoo.com>. I now question my computer installation of my compiler. . . thanks again,. . . . therry
Topic archived. No new replies allowed.