myfile issue

Ok so maybe some of you were wondering what I was working on here is all of my 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
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <windows.h>
#include <fstream>
#include <string>
using namespace std; //I remembered
int main()
{
string line;
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
CreateDirectory("c:\\u", NULL); //creates folder
ofstream myfile;
  myfile.open ("C:\\u\\work.txt");
  myfile << cout << rand() << endl; //writes random integer in txt.
  myfile.close();
ifstream myfile ("c:\\u\\work.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      cout << line << endl; //reads line
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 
return 0;
}



I just need help with one more thing. For some reason it is saying " error C2371: 'myfile' : redefinition; different basic types
c:\documents and settings\primary\desktop\xxewf\xxewf\xxewf.cpp(15) : see declaration of 'myfile' " What is wrong with myfile at that point?
You cannot redeclare myfile as an ofstream and ifstream (.close() does not terminate the existence of myfile; it's still declared as an ofstream). Rename the second myfile and you should be fine.

-Albatross
Ok thank you again Albatross!! Saving my hungover butt tonight. Plus I think there might be something up with my random integer. When I run it I get DBCC right in the middle of two four digit numbers.. The number is changing but DBCC is just sitting there. Any clue of what that might be?
I wouldn't be fully sure.

...although, I retract that. On further inspection... what's that cout doing in line 17? If you simply want to write your random number to your file, you can ditch the cout.
myfile << rand() << endl;

EDIT: About your profile, while some of our projects might be Godzilla-caliber, we specifically take time off them just because helping others to us is more important than some massive green program hopping around Tokyo dodging bat projectiles and biting Primes.

-Albatross
Last edited on
That works PERFECTLY. Thank you. You're fixing a lot of problems that I probably should have noticed myself. Next time I think I will bring it up to the forums only after some decent inspections done by myself.

-- Thank you for being there for me and others then and taking time off your Godzillas. Even though a Godzilla program would be pretty cool.
Topic archived. No new replies allowed.