Skip to content

Commit

Permalink
learning more and more every day. I love programming.
Browse files Browse the repository at this point in the history
  • Loading branch information
jesushilarioh committed Oct 15, 2016
1 parent efb2117 commit 7a2ea51
Show file tree
Hide file tree
Showing 4 changed files with 636 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Data-Storage/ifstream-test-for-file-open-errors.cpp
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 Data-Storage/ofstream-use-for-loop-to-write-data-to-a-file.cpp
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;
}
Loading

0 comments on commit 7a2ea51

Please sign in to comment.