#include <string>
#include <fstream>
#include <iostream>
using namespace std;
void main()
{
ifstream infile("infile.txt"); //This file could not be opened
ofstream outfile("outfile.txt");//OK, it openes
}
does not open the infile file. Why? The ofstream file, (for output) openes, but for input don't. Franjo
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
PLEASE quit writing
void main()
its wrong!!!!!!!! Look up the standard. main is required to return an integer. One day, as compilers drift towards the standard, this will stop working.
Now, todays dumb question. Does the file "infile.txt" exist?
Wayne
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Now, I looked at your code, and the question occured to me....based on this code, how can he tell if the input file is open or closed? So...I modified your code somewhat. I got it to read in the contensts of the input file (which I wrote out earlier), and put data out into the ouput file. It worked. Note that I DID have the input file existing with data before I tried to read from it. You can cheat like I did and have the program write data to the output file, then change names to insure that there is data tor ead in.
#include <string>
#include <fstream>
#include <iostream>
#include<stdlib.h>
using namespace std;
int main(void)
{
ifstream infile("outfile.txt");
char ch;
while (infile.get(ch))
{
cout << ch;
}
ofstream outfile("outfile2.txt");
outfile << "This is a test\n";
system("pause");
return 0;
}
Wayne
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here a Create and Open programs...Compile them as two programs. Run the Create program first: This creates a txt file with the text you typed into it.
Now run the Open program, you should see the text you wrote come up. NOTE: the open program will run with a blank screen and do nothing until you close it ... ( sounds like me when i fall asleep and the wife kicks me to come to bed ) be sure to have a file by the name in the open program.
Hello, my problem is that the code
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
void main()
{
ifstream infile("infile.txt"); //This file could not be opened
ofstream outfile("outfile.txt");//OK, it openes
}
does not open the infile file. Why? The ofstream file, (for output) openes, but for input don't. Franjo
Franjo,
PLEASE quit writing
void main()
its wrong!!!!!!!! Look up the standard. main is required to return an integer. One day, as compilers drift towards the standard, this will stop working.
Now, todays dumb question. Does the file "infile.txt" exist?
Wayne
Now, I looked at your code, and the question occured to me....based on this code, how can he tell if the input file is open or closed? So...I modified your code somewhat. I got it to read in the contensts of the input file (which I wrote out earlier), and put data out into the ouput file. It worked. Note that I DID have the input file existing with data before I tried to read from it. You can cheat like I did and have the program write data to the output file, then change names to insure that there is data tor ead in.
#include <string>
#include <fstream>
#include <iostream>
#include<stdlib.h>
using namespace std;
int main(void)
{
ifstream infile("outfile.txt");
char ch;
while (infile.get(ch))
{
cout << ch;
}
ofstream outfile("outfile2.txt");
outfile << "This is a test\n";
system("pause");
return 0;
}
Wayne
Here a Create and Open programs...Compile them as two programs. Run the Create program first: This creates a txt file with the text you typed into it.
Now run the Open program, you should see the text you wrote come up. NOTE: the open program will run with a blank screen and do nothing until you close it ... ( sounds like me when i fall asleep and the wife kicks me to come to bed ) be sure to have a file by the name in the open program.
1. Create program. //------------------------------------------------------
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
ofstream outfile;
char word[200];
outfile.open("file.txt", ios::out);
do
{
cout << "Type a word. ";
cin.getline(word,200);
outfile << word <<endl;
}
while(strcmp(word, "" ) != 0);
outfile.close();
system("PAUSE");
return 0;
}
2. Open program //-------------------------------------------------------
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
void read(ifstream &T)
{
char ch;
while(!T.eof())
{
T.get(ch);
cout << ch;
}
}
int main()
{
ifstream T("file.txt");
read(T);
system("PAUSE");
T.close();
}
If any problems ?... please come back to this thread and ask your question... I or maybe another will be able to help you with it. Hope this helps.
Two more things !!!
The Create program DO NOT USE SPACES in the text.
The create program you can type many lines of text, just hit enter two times to exit.