-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
learning more and more every day. I love programming.
- Loading branch information
1 parent
efb2117
commit 7a2ea51
Showing
4 changed files
with
636 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//********************************************* | ||
// This program tests for file open errors. | ||
// | ||
// By: JESUS HILARIO HERNANDEZ | ||
// Last modified: October 12, 2016 | ||
//********************************************* | ||
#include <iostream> | ||
#include <fstream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
ifstream inputFile; | ||
int number; | ||
|
||
// Open the file. | ||
inputFile.open("BadListOfNumbers.txt"); | ||
|
||
// If the file sucessfully opened... | ||
if (inputFile) | ||
{ | ||
// Read the numbers from the file and | ||
// display them. | ||
while (inputFile >> number) | ||
{ | ||
cout << number << endl; | ||
} | ||
|
||
// Close the file. | ||
inputFile.close(); | ||
} | ||
// Trailing Else statment | ||
else | ||
{ | ||
// Display and error message. | ||
cout << "Error opening the file.\n"; | ||
} | ||
|
||
//********************************************* | ||
// Another example | ||
//********************************************* | ||
|
||
inputFile.open("ListOfNumbers.txt"); | ||
if (inputFile.fail()) | ||
{ | ||
cout << "Error opening file.\n"; | ||
} | ||
else | ||
{ | ||
// Read the numbers from the file and | ||
// display them. | ||
while (inputFile >> number) | ||
{ | ||
cout << number << endl; | ||
} | ||
|
||
// Close the file. | ||
inputFile.close(); | ||
} | ||
return 0; | ||
} |
32 changes: 32 additions & 0 deletions
32
Data-Storage/ofstream-use-for-loop-to-write-data-to-a-file.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//************************************************** | ||
// This program uses a for loop to write the numbers | ||
// in increments of 2 within the range of 0 through | ||
// 1000. | ||
// | ||
// By: JESUS HILARIO HERNANDEZ | ||
// Last modified: October 14, 2016 | ||
//************************************************** | ||
#include <iostream> | ||
#include <fstream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
// Stream object | ||
ofstream outputFile; | ||
|
||
// Open or create file to write to | ||
outputFile.open("writenumbersto.txt"); | ||
|
||
for (int i = 0; i <= 1000; i += 2) | ||
{ | ||
outputFile << i << endl; | ||
} | ||
// Display data write complete | ||
cout << "Your written file has been written and " | ||
<< "complete." << endl; | ||
|
||
// Close file | ||
outputFile.close(); | ||
return 0; | ||
} |
Oops, something went wrong.