Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Assisgnments/Submissions/Rupali/A3_filepointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include <fstream>
#include <string>

int main() {
// Declare file pointers
std::fstream file;

file.open("example.txt", std::ios::out);
if (!file) {
std::cerr << "File could not be opened for writing!" << std::endl;
return 1;
}

file << "Hello, this is a test file.\n";
file << "We are writing some text to it.\n";

file.close();

file.open("example.txt", std::ios::in);
if (!file) {
std::cerr << "File could not be opened for reading!" << std::endl;
return 1;
}

std::string line;
while (std::getline(file, line)) {
std::cout << line << std::endl;
}
file.close();

return 0;
}