Question

Can someone explain the process of this Display a text file program and show the supposed...

Can someone explain the process of this Display a text file program and show the supposed output of the program?

I tried it with my text file and all it does is printing out the same line of words for 24 times. Can you also explain what the [COL_WIDTH + 1 ] and the c = input_line [0] ; is doing?

Please show me what this program is doing. Thanks.

Here below is the actual program.

#include

#include

using namespace std;

#define COL_WIDTH 80

int main() {

int c; // input character

char filename[FILENAME_MAX];

char input_line[COL_WIDTH + 1];

  

cout << "Enter a file name and press ENTER: ";

cin.getline(filename, FILENAME_MAX);

ifstream file_in(filename);

if (! file_in) {

cout << filename << " could not be opened.";

cout << endl;

return -1;

}

while (true) {

for(int i = 1; i <= 24 && !file_in.eof(); ++i) {

file_in.getline(input_line, COL_WIDTH);

cout << input_line << endl;

}

if (file_in.eof()) {

break;

}

  

cout << "More? (Press 'Q' and ENTER to quit)";

cin.getline(input_line, COL_WIDTH);

c = input_line[0];

if (c == 'Q' || c == 'q') {

break;

}

}

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

#include<fstream>

using namespace std;

#define COL_WIDTH 80

int main() {

int c; // input character

//used to store the file name
//FILENAME_MAX is a predefined constant
//It specify the maximum length for a file name

char filename[FILENAME_MAX];

//used to store the line read from file
//COL_WIDTH is the number of characters to be read from each line
//In COL_WIDTH + 1, 1 is for storing string terminator '\0'

char input_line[COL_WIDTH + 1];

//prompt the user to enter a file name to be read
cout << "Enter a file name and press ENTER: ";

//read the file name and stores in character array 'filename'
cin.getline(filename, FILENAME_MAX);

//make the file to be read
ifstream file_in(filename);

//if file cannot be opened
if (! file_in) {

cout << filename << " could not be opened.";

cout << endl;

return -1;

}

//indefinite while loop
while (true) {


//reads first 24 lines
//file_in.eof() checks whether end of file is reached or not

for(int i = 1; i <= 24 && !file_in.eof(); ++i) {


//reads 79 characters in each line and 80 for '\0'
file_in.getline(input_line, COL_WIDTH);
//display the readed line
cout << input_line << endl;

//if it not reached the end of file
//when the user press enter instead of 'q' or 'Q'
//then above for loop will read from 25th line to next 24 lines
//until reaching end of file


}

//checks whether end of file is reached
//if it is the end , terminate the program

if (file_in.eof()) {
//break the while loop and exit program
break;

}

  
//ask user to continue or quit
cout << "More? (Press 'Q' and ENTER to quit)";
//read the input from user
//and stored to character array 'input_line'

cin.getline(input_line, COL_WIDTH);

// 'c' contains first character in the character array 'input_line'
// 'q' or 'Q'

c = input_line[0];
//if c = 'q' or 'Q'
if (c == 'Q' || c == 'q') {


//terminate while loop and exit program
break;

}

}

return 0;

}

Below file has more than 80 characters in a line, the program read only 79 characters

Output from above text file

If a file cannot be opened

Add a comment
Know the answer?
Add Answer to:
Can someone explain the process of this Display a text file program and show the supposed...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define...

    can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block {    std::string word;    int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) {    string filename="input.txt";    //declare array of struct word_block    word_block arr[SIZE];    int count = 0;    if (argc < 2)    {        cout << "Usage: " << argv[0] << "...

  • Hey, so i am trying to have my program read a text file using a structure...

    Hey, so i am trying to have my program read a text file using a structure but i also want to be able to modify the results(I kinda have this part but it could be better). I cant seem to get it to read the file(all the values come up as 0 and i'm not sure why because in my other program where it wrote to the txt file the values are on the txt file) i copied and pasted...

  • -can you change the program that I attached to make 3 file songmain.cpp , song.cpp ,...

    -can you change the program that I attached to make 3 file songmain.cpp , song.cpp , and song.h -I attached my program and the example out put. -Must use Cstring not string -Use strcpy - use strcpy when you use Cstring: instead of this->name=name .... use strcpy ( this->name, name) - the readdata, printalltasks, printtasksindaterange, complitetasks, addtasks must be in the Taskmain.cpp - I also attached some requirements below as a picture #include <iostream> #include <iomanip> #include <cstring> #include <fstream>...

  • Use the file processing example program shown at the bottom. Modify the program to enter the...

    Use the file processing example program shown at the bottom. Modify the program to enter the grades, and count number of grades. Also get total and average grade. Use the following data for grades: 79, 99, 85, 97, 88, 95, 100, 87, 94 EXAMPLE OUTPUT Total: 9 grades Total grade sum: 824 Average grade: 92 Letter grade: A / Using Break, and Continue #include "stdafx.h" #include using namespace std; int main() { int i = 0; for (int x =...

  • Hi there! I need to fix the errors that this code is giving me and also...

    Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...

  • // Write a program that determines how many of each type of vowel are in an...

    // Write a program that determines how many of each type of vowel are in an entered string of 50 characters or less. // The program should prompt the user for a string. // The program should then sequence through the string character by character (till it gets to the NULL character) and count how many of each type of vowel are in the string. // Vowels: a, e, i, o, u. // Output the entered string, how many of...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • In c++ programming, can you please edit this program to meet these requirements: The program that...

    In c++ programming, can you please edit this program to meet these requirements: The program that needs editing: #include <iostream> #include <fstream> #include <iomanip> using namespace std; int cal_avg(char* filenumbers, int n){ int a = 0; int number[100]; ifstream filein(filenumbers); if (!filein) { cout << "Please enter a a correct file to read in the numbers from"; }    while (!filein.eof()) { filein >> number[a]; a++; } int total = number[0]; for (a = 0; a < n; a++) {...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT