I/O files made easier :)

Submitted on: 1/1/2015 2:46:00 PM
By: Benjamin Westrich (from psc cd)  
Level: Beginner
User Rating: By 9 Users
Compatibility: C++ (general), Microsoft Visual C++
Views: 788
     This is to make Sequential access files easier to use. I remember when I first learned them from my text, it wasnt easy, and this should clear a few things up.

				Well, We are going to look into Sequential access files and how to make and use them with a C++ program. Its not so hard... below I have fully commented code that will tell you step by step what each thing does. As while this is beginner, you should atleast know a few things like loops and variables before doing this. THe first one is the complete source to a program, the second bit is a commented snippet to companion the first. Please rate me even if a bad rating:
#include //Has the info for opening the file, and input/output
int main()
	//declare variable 
	float number; 
	//open file 
	ofstream Out_File; //associates it(object (Out_File) with this class, used to ouput info to a file"datafile.dat", ios::app);//commented on heavily at end 
	//Check for open 
	if (! //checks to make sure it DIDNT fail (! operator) 
		//input names and write to file 
		cout << "Please enter a number(enter a negative to exit): "; 
		cin >> number; 
		while (number >= 0) //loops to check for a negative number 
		Out_File << number << endl; //adds the line to the data file you opened. 
		cout << "Please enter a number(enter a negative to exit): "; 
		cin >> number; 
		}//end while 
		//close the file 
	else //incase it didnt open for any reason.... 
	cout << "Sorry, error opening file... " << endl; 
	//end if 
}//end main function(close program, no other prototypes) 
	/* Now, the '"datafile.dat", ios::app)'works like so. The open part is 
	fairly obvious, it opens the Object Out_file. The first part, "datafile.dat" is the
	filename you wish to open, or create(incase it doesnt exist).the second is the type 
	of I/O you want, they follow the 'ios::' command and are as follows:
	ios::in Opens file for input into the program, Default mode for input files(will talk about later)
	ios::app Opens file for 'append', If the file doesnt exist it will create a new one, but if it 
	does it will actually just continue writing to it with whatever you had on there before still on 
	ios::out Opes a file for output to it, creates a new one, or deletes all the info in an old one so
	that it it starts of new. Very different then the append....
	on a side note: Out_File << number << endl; the endl actually makes a newline
 on the data file so that the data is on different lines and can be differentiated.
	Hopes this helps :) */
	Now... the code for the input of info from the file to the program is as follows...
	This is just a snippet and lacks header files, variable declaration, and main function call as you have seen all that in the last bit:
 //open file for for input
 ifstream input_file;"datafile.dat", ios::in); //make sure open was successful(more important in this one then the last)
 if ( ! )
		//read from file
 		input_file >> score; 
		while ( !input_file.eof() )
 			//increment counter and average
 			index++; //same as index = index + 1, shortens it 
 			totalscore += score; //same as totalscore = totalscore + score, shortens it 
 			input_file >> score; //takes from file into the program.
 		}//end while loop
		//close input_file
		//display average
 		average = (float) totalscore / (float) index; 
 		cout << "The average of the numbers you entered was " << average << endl;
 else //failed to open...
 		cout << "cant open file";
 //end if
 /* Pretty straight forward, we used to ifstream (input file) instead of ofstream this time,
	and it is to be noted that you dont have to assign it a value to read from as an array, since
	it does it in order. One by one. Hopes this helps to! */

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.