infile/outfile

anyone know why my file just terminates.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{

ifstream myfile;
ofstream newfile;
string daFile;

myfile.open ("daFile.txt");
if (!myfile) //there was an error on open, file not found
{ cout << "Cannot open file, terminating program"<< endl;
system("pause") ;
exit (1);
}
newfile.open ("myfile");
myfile >> daFile; //this is called a priming read, only used once
while (!myfile.eof()) //or while (!infile.eof())
{
newfile <<daFile<< endl;
myfile>> daFile;
system("PAUSE");
};
myfile.close();
newfile.close ();
return 0; }
Do any of your error messages print?

What data does your input file contain?


yea it tells me cannot open file, terminating program and it closes doesnt let me do anytin else
Last edited on
So does your input file exist in the directory where your program expects it to be?

i jus put cout << name << endl; to try it out and i made it on notepad for both i tried changing "daFile.txt" to "c:\\temp\\daFile.text"; but still didnt function and i tried making for oth newfile and dafile

and now i changed it to myfile >> hello; and it doesnt function haha but it runs just does notin
Last edited on
Try this simple program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{

   ofstream myfile("MyTrialFile.txt");
   if (!myfile) //there was an error on open, file not found
   {
      cout << "Cannot open file, terminating program"<< endl;
      system("pause") ;
      return 1;
   }

   cout << "File opened. " << endl;
   system("PAUSE");
   return 0;
}


If the "File opened. " message appears use your operating systems search functionality to find the file with the correct name. Is your input file in this same directory?



lol it was saving in a whole dif location ima try changing it to notepad
thanks ill let u know if it works
thanks alot it worked but im guessing the file doesnt really open?
What? You said your program was reporting that the file didn't open so, no it didn't open. In the program I supplied, if the "File opened." message appeared then yes the file opened.

You need to understand that when dealing with input streams and using the default open mode, the file must exist, it will not be created if it doesn't exist. With a output stream using the default open mode, if the file doesn't exist a empty file will be created. And also an output stream erases the contents of the file when you open it, when using the default open mode.

yea i made it work it transfers information from myfile to newfile
now im just trying to find a way to make a loop.
the problem was that for some reason my .txt files were saving inside of visual studios so i just moved them and copied their location
Last edited on
im glad i got it to run im behind 1 lab so i did this one because were working on it tomorrow and im behind on my other lab because it has to do with prime numbers and perfect, for some reason i cant get the formula to work lol

you helped alot haha cant believe the file was in the wrong spot lasted hours on this simple lab.
Last edited on
Topic archived. No new replies allowed.