Need to edit only where the file read less than 20 lines it should has to change the 10 so that it will read 10 lines
while(!file.eof() && lines < 10)
this line will read the file if file has less than 10 lines other wise terminate the loop
cpp files:
#include <iostream>
#include<string>
#include<cstdlib>
#include <fstream>
using namespace std;
int main () {
string name; //file name
string inputline; //file line
int lines=0; //number of lines
//ask to enter file name
cout<<"Enter the file name: ";
getline(cin,name);
ifstream file;
file.open(name.c_str()); //will not create a new file
it will only open it.
//check for the if file exist
if(!file){
cout<<"Error opening
"<<name<< endl;
exit(EXIT_FAILURE);
}
//loop until end of file and file lines less than
10
while(!file.eof() && lines < 10){
getline(file, inputline,
'\n'); //get line of the file
lines++; //increment
line
cout<<inputline<<endl;
//ouput lines
}
if(lines<10) //check for the less than 10
lines
cout<<"\n The entire file has
been displayed.\n";
file.close(); //close file
return 0;
}
testing files:
poem1.txt:
had no direction to go up: and this the shattery road
its surface graining, trickle in late thaw is nothing amiss?
this melt, the sign assure us, natural cycle
and woosh, the water a dream of forgotten white
past aspens colored in sulfur, they trimbled, would poor sinners in
redemption song hed their tainted leaves
had no direction to go up: and this the shattery road
its surface graining, trickle in late thaw is nothing amiss?
this melt, the sign assure us, natural cycle
and woosh, the water a dream of forgotten white
past aspens colored in sulfur, they trimbled, would poor sinners in
redemption song hed their tainted leaves
had no direction to go up: and this the shattery road
its surface graining, trickle in late thaw is nothing amiss?
this melt, the sign assure us, natural cycle
and woosh, the water a dream of forgotten white
past aspens colored in sulfur, they trimbled, would poor sinners in
redemption song hed their tainted leaves
had no direction to go up: and this the shattery road
its surface graining, trickle in late thaw is nothing amiss?
this melt, the sign assure us, natural cycle
and woosh, the water a dream of forgotten white
past aspens colored in sulfur, they trimbled, would poor sinners in
redemption song hed their tainted leaves
had no direction to go up: and this the shattery road
its surface graining, trickle in late thaw is nothing amiss?
this melt, the sign assure us, natural cycle
and woosh, the water a dream of forgotten white
past aspens colored in sulfur, they trimbled, would poor sinners in
redemption song hed their tainted leaves
poem2.txt:
had no direction to go up: and this the shattery road
its surface graining, trickle in late thaw is nothing amiss?
this melt, the sign assure us, natural cycle
and woosh, the water a dream of forgotten white
past aspens colored in sulfur, they trimbled, would poor sinners in
redemption song hed their tainted leaves
sample output:
C++ Programming Objects Problem Assignment: Programming challenge #1 on page 714: "File Head Program" Write a...
The purpose of this assignment is to develop solutions that perform File IO and objects. Problem specifications are shown below with my changes in blue. 1. File Previewer Write a program that asks the user for the name of a text file. The program should display the first 10 lines of the file on the screen. If the file has fewer than 10 lines, the entire file should be displayed along with a message indicating the entire file has been...
Develop a functional flowchart and then write a C++ program to solve the following problem. 1. Create a text file named c1.txt and write your brand of computer (like Dell, HP, etc) in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your program will also read the brand of your computer from the keyboard. The process of the file creation (name of the file, mode for opening...
write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...
I need help with this assignment in C++, please! *** The instructions and programming style details are crucial for this assignment! Goal: Your assignment is to write a C+ program to read in a list of phone call records from a file, and output them in a more user-friendly format to the standard output (cout). In so doing, you will practice using the ifstream class, I'O manipulators, and the string class. File format: Here is an example of a file...
Write a c++ program in that file to perform a “Search and Replace All” operation. This operation consists of performing a case-sensitive search for all occurrences of an arbitrary sequence of characters within a file and substituting another arbitrary sequence in place of them. Please note: One of the biggest problems some students have with this exercise occurs simply because they don’t read the command line information in the course document titled “Using the Compiler’s IDE”. Your program: 1. must...
C++ please Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encrypted message from a file, decode the message, and then output the message to a file. The encryption method being used on the file is called a shift cipher (Info Here). I will provide you with a sample encrypted message, the offset, and the decrypted message for testing. For this project I will provide you main.cpp, ShiftCipher.h, and ShiftCipher.cpp....
Computer Science 182 Data Structures and Program Design Programming Project #3 – Link List Card Games One of the nice things about Java is it is very easy to do fun things with graphics. The start code provided here will display a deck of cards on the screen and shuffle them. Your mission, is to start with this code and build a card game. Blackjack, poker solitaire, what ever your heart desires. Blackjack is the easiest. Obviously any program you...
Hi, I have C++ programming problem here: Problem: Please modify your string type vector in for push_back() function as below: void push_back(string str) { // increase vector size by one // initialize the new element with str } In addition, the standard library vector doesn't provide push_front(). Implement push_front() for your vector. Test your code with the main function below. int main() { vector v1(3); cout<<"v1: "; v1.print(); // this should display -, -, - for...
can i get some help with this program CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write a Java program that uses recursion to find all solutions to the n-Queens problem, for 1 Sns 15. (Students who took CMPS 12A from me worked on an iterative, non-recursive approach to this same problem. You can see it at https://classes.soe.ucsc.edu/cmps012a/Spring l8/pa5.pdf.) Begin by reading the Wikipcdia article on the Eight Queens puzzle at: http://en.wikipedia.org/wiki/Eight queens_puzzle In...