The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
I/O C++
Posted by: Robert | November 21, 2010 @ 1:13am
C++ Code
[
Download
]
/* Program creates a user-named .txt file (if file does not exist) in which user can build a list of words up to user-chosen number of entries desired. Console then displays all entries in .txt If same file is reopened, the list is appended from the end. */ #include <iostream> #include <string> #include <fstream> using namespace std; int main() { string filename; // creates string for user input cout << "Input file name to open: "; cin >> filename; cout << endl; fstream myfile(filename.c_str(), ios::out | ios::app); // declares fstream class "myfile". Opens file from user input. if (myfile.is_open()) { int x = 0; cout << "Enter the number of words you would like to enter: "; cin >> x; cout << endl; string input; // User controlled for-loop from x for (int i = 0;i < x; i++) { cout << "Enter word " << i <<":"; cin >> input; myfile << input << endl; } cout << endl; myfile.close(); } else cout << "Unable to open file!" << endl; //------------------------------------------------------------------------------------------------------- string line; // creates string to read line from .txt myfile.open("example.txt", ios::in); // Opens created file (myfile declared on line 14) if (myfile.is_open()) { int numPlace = 1; cout << "The list is:" << endl; while (!myfile.eof()) // while-loop to print what is in .txt aka(myfile.good()) { getline (myfile, line); cout << numPlace << ". " << line << endl; numPlace++; } myfile.close(); } else cout << "Unable to open file!" << endl; return 0; }
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.68 / 8
50 Votes