Question

Write a program that reads a file, removes any blank lines at the beginning or end of the file, and writes the remaining line

c++

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

CODE:

#include<iostream>
#include<fstream>

using namespace std;

int main(){
ifstream fin;
string line;
//declaring an array to store the lines from the input file
string *linesArray = new string[1000];
int lines = 0;

fin.open("input.txt");
while(fin){
getline(fin, line);
//a blank line will have a length zero, so the line with length zero will
//not be added in th array
if(line.length() != 0){
linesArray[lines] = line;
lines += 1;
}
}
//closing the input file
fin.close();
ofstream fout;
fout.open("input.txt");

//writing the lines other than the blank lines in the file
for(int i =0;i<lines;i++){
//the last line should not add another line in the end
if(i == lines-1)
{
fout<<linesArray[i];
break;
}
//printing the lines
fout<<linesArray[i]<<endl;
}
//closing the file
fout.close();
return 0;
}

___________________________________________

CODE IMAGES AND OUTPUT:

#include<iostream> #include<stream> 2 3 4 5 using namespace std; 6 7 8 int main() { ifstream fin; string line; //declaring an

THe input.txt file before executing the program:

input.txt - Notepad х File Edit Format View Help This is a line which will be written in the file without blank lines. This l

input.txt file after executing the program:

х input.txt - Notepad File Edit Format View Help This is a line which will be written in the file without blank lines. This l

_______________________________________________

Feel free to ask any questions in the comments section

Thank You!

?mid=&wid=52562&sid=&tid=8561&rid=LOADED?mid=&wid=52562&sid=&tid=8561&rid=BEFORE?mid=&wid=52562&sid=&tid=8561&rid=FINISH?mid=&wid=52562&sid=&tid=8561&rid=OPTOUT

Add a comment
Know the answer?
Add Answer to:
c++ Write a program that reads a file, removes any blank lines at the beginning or...
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
  • C++ ONLY Write a program that reads a file, removes any blank lines at the beginning...

    C++ ONLY Write a program that reads a file, removes any blank lines at the beginning or end of the file, and writes the remaining lines back to the same file. C++ ONLY

  • In C write a text analyzer program that reads any text file. The program displays a...

    In C write a text analyzer program that reads any text file. The program displays a menu that gives the user the options of counting: • Lines • Words • Characters • Sentences (one or more word ending with a period) • or all of the above Provide a separate function for each option. At the end of the analysis, write an appropriate report.

  • IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and...

    IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. For example if file input is: This is a test File output should be 1. This is a test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BELOW FROM NOTEPAD FILE. This file contains lines of text to determine if you can properly read and write from a file.

  • Write a program in python that reads each line in a file, reverses its lines, and...

    Write a program in python that reads each line in a file, reverses its lines, and writes them to another file. For example, if the file input.txt contain the lines: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go. and you run your Python file then output.txt contains The lamb was sure to go. And everywhere that Mary went Its fleece was white as snow Mary had...

  • write a C++ program that reads in a text file and reads each character or lexemes...

    write a C++ program that reads in a text file and reads each character or lexemes and states what token it is and writes to another file for example: while (fahr < upper) a = 23.00 whileend Output: token lexeme -------------------------------- keyword while separator ( identifier fahr operator < identifier upper separator ) identifier a operator = real 23.00 keyword whileend the program aslo reads in comments but does not add to the list. characters that are commented out or...

  • In c programming, I need to write a program that reverses each of the lines in...

    In c programming, I need to write a program that reverses each of the lines in a file. The program accepts two command line arguments: The first one is the name of the input file The second is the name of the output file If the user didn't give two filenames, display an error message and exit. The program reads in each line of the input file and writes each line in reverse to the output file. Note, the lines...

  • JAVA Write a program that 1. opens a text file 2. reads lines of text from...

    JAVA Write a program that 1. opens a text file 2. reads lines of text from the file 3. Converts each line to uppercase 4. saves them in an ArrayList 5. Writes the list of Strings to a new file named "CAPSTEXT.txt" Mainjavo ext.txt Instructions from your teacher 1 My mistress' eyes are nothing like the sun; 2 Coral is far more red than her lips' red; 3 If snow be white, why then her breasts are dun; 4 If...

  • Write a C program that reads in the file and outputs it to another file with...

    Write a C program that reads in the file and outputs it to another file with its lines in reverse order. Use the following array to store the addresses of the lines as you read them in: char *lines[200]; Save each line so that it occupies only enough memory required by that line. Use recursion. Hint: On each call of the recursive function, read ONE line into a LOCAL buffer.

  • ( IN JAVA): Write a program that reads each line in a file, and writes them...

    ( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...

  • Write a c program that takes in a text file and writes lines with only lowercase...

    Write a c program that takes in a text file and writes lines with only lowercase letters to another text file which is disclosed from the command line. If no text file to output to is disclosed in the command line, the lines with only lowercase letters should be written into a text file called "all_lower.txt"

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