AP Computer Science A › Program Analysis
Consider the following C++ code:
1. #include
2. #include
3. using namespace std;
4. int main() {
5. ifstream outputFile;
6. inputFile.open("TestFile.txt");
7. outputFile << "I am writing to a file right now." << endl;
8. outputFile.close();
9. //outputFile << "I'm writting on the file again" << endl;
10. return 0;
11. }
What is wrong with the code?
The outputFile should be of type "ofstream", not "ifstream".
There is nothing wrong with the code.
The ifstream library should be included.
Testfile.txt shouldn't have double quotations around it.
Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.
Consider the following C++ code:
1. #include
2. #include
3. using namespace std;
4. int main() {
5. ifstream outputFile;
6. inputFile.open("TestFile.txt");
7. outputFile << "I am writing to a file right now." << endl;
8. outputFile.close();
9. //outputFile << "I'm writting on the file again" << endl;
10. return 0;
11. }
What is wrong with the code?
The outputFile should be of type "ofstream", not "ifstream".
There is nothing wrong with the code.
The ifstream library should be included.
Testfile.txt shouldn't have double quotations around it.
Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.
Consider the following C++ code:
1. #include
2. #include
3. using namespace std;
4. int main() {
5. ifstream outputFile;
6. inputFile.open("TestFile.txt");
7. outputFile << "I am writing to a file right now." << endl;
8. outputFile.close();
9. //outputFile << "I'm writting on the file again" << endl;
10. return 0;
11. }
What is wrong with the code?
The outputFile should be of type "ofstream", not "ifstream".
There is nothing wrong with the code.
The ifstream library should be included.
Testfile.txt shouldn't have double quotations around it.
Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.
Consider the following C++ code:
1. #include
2. #include
3. using namespace std;
4. int main() {
5. ifstream outputFile;
6. inputFile.open("TestFile.txt");
7. outputFile << "I am writing to a file right now." << endl;
8. outputFile.close();
9. //outputFile << "I'm writting on the file again" << endl;
10. return 0;
11. }
What is wrong with the code?
The outputFile should be of type "ofstream", not "ifstream".
There is nothing wrong with the code.
The ifstream library should be included.
Testfile.txt shouldn't have double quotations around it.
Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.
Consider the following C++ code:
1. #include
2. #include
3. using namespace std;
4. int main() {
5. ifstream outputFile;
6. inputFile.open("TestFile.txt");
7. outputFile << "I am writing to a file right now." << endl;
8. outputFile.close();
9. //outputFile << "I'm writting on the file again" << endl;
10. return 0;
11. }
What is wrong with the code?
The outputFile should be of type "ofstream", not "ifstream".
There is nothing wrong with the code.
The ifstream library should be included.
Testfile.txt shouldn't have double quotations around it.
Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.
Consider the following C++ code:
1. #include
2. #include
3. using namespace std;
4. int main() {
5. ifstream outputFile;
6. inputFile.open("TestFile.txt");
7. outputFile << "I am writing to a file right now." << endl;
8. outputFile.close();
9. //outputFile << "I'm writting on the file again" << endl;
10. return 0;
11. }
What is wrong with the code?
The outputFile should be of type "ofstream", not "ifstream".
There is nothing wrong with the code.
The ifstream library should be included.
Testfile.txt shouldn't have double quotations around it.
Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.
Consider the following C++ code:
1. #include
2. #include
3. using namespace std;
4. int main() {
5. ifstream outputFile;
6. inputFile.open("TestFile.txt");
7. outputFile << "I am writing to a file right now." << endl;
8. outputFile.close();
9. //outputFile << "I'm writting on the file again" << endl;
10. return 0;
11. }
What is wrong with the code?
The outputFile should be of type "ofstream", not "ifstream".
There is nothing wrong with the code.
The ifstream library should be included.
Testfile.txt shouldn't have double quotations around it.
Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.
Consider the following C++ code:
1. #include
2. #include
3. using namespace std;
4. int main() {
5. ifstream outputFile;
6. inputFile.open("TestFile.txt");
7. outputFile << "I am writing to a file right now." << endl;
8. outputFile.close();
9. //outputFile << "I'm writting on the file again" << endl;
10. return 0;
11. }
What is wrong with the code?
The outputFile should be of type "ofstream", not "ifstream".
There is nothing wrong with the code.
The ifstream library should be included.
Testfile.txt shouldn't have double quotations around it.
Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.
Consider the following C++ code:
1. #include
2. #include
3. using namespace std;
4. int main() {
5. ifstream outputFile;
6. inputFile.open("TestFile.txt");
7. outputFile << "I am writing to a file right now." << endl;
8. outputFile.close();
9. //outputFile << "I'm writting on the file again" << endl;
10. return 0;
11. }
What is wrong with the code?
The outputFile should be of type "ofstream", not "ifstream".
There is nothing wrong with the code.
The ifstream library should be included.
Testfile.txt shouldn't have double quotations around it.
Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.
Consider the following C++ code:
1. #include
2. #include
3. using namespace std;
4. int main() {
5. ifstream outputFile;
6. inputFile.open("TestFile.txt");
7. outputFile << "I am writing to a file right now." << endl;
8. outputFile.close();
9. //outputFile << "I'm writting on the file again" << endl;
10. return 0;
11. }
What is wrong with the code?
The outputFile should be of type "ofstream", not "ifstream".
There is nothing wrong with the code.
The ifstream library should be included.
Testfile.txt shouldn't have double quotations around it.
Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.