Need help moving input text file into output text file.

I've been working on this awhile, but nothing is showing up on the output.
Here's what I have to make it do. Plus I've filled in the input text data myself if I was supposed to do that.

For this assignment you will be writing a program that reads information about a sequence of tracks on an album from a file, and generates a formatted track listing with additional summary information. The input file should be named albuminfo.txt and have the following format:<title of album><album artist><song title for track 01><minutes> <seconds>...<song title for track XX><minutes> <seconds>The <minutes> <seconds> lines contain the lengths of each track in minutes and seconds (without leading zeros.)The output file should be named tracklist.txt and have the following format:Album title: <title of album>Artist: <album artist>Tracks:--------------------------------------------------01 - <song title for track 01> MM:SS...XX - <song title for track XX> MM:SS--------------------------------------------------In addition, you must display the following to the console:Welcome to <your name>'s tracklist generator!Processed XX tracks.Total album length: MM:SSNote: when displaying time information, the seconds should have leading zeros but the minutes should not.

Bows + Arrows
The Walkmen
What's in It for Me
2 53
The Rat
4 27
No Christmas While I'm Talking
4 30
Little House of Savages
3 15
My Old Man
4 46
138th Street
3 2
The North Pole
3 48
Hang On, Siobhan
3 45
New Year's Eve
2 20
Thinking of a Dream I Had
4 33
Bows + Arrows
5 16
Your program should generate the following output file:Album title: Bows + Arrows
Artist: The Walkmen
Tracks:--------------------------------------------------
01 - What's in It for Me 2:53
02 - The Rat 4:27
03 - No Christmas While I'm Talking 4:30
04 - Little House of Savages 3:15
05 - My Old Man 4:46
06 - 138th Street 3:02
07 - The North Pole 3:48
08 - Hang On, Siobhan 3:45
09 - New Year's Eve 2:20
10 - Thinking of a Dream I Had 4:33
11 - Bows + Arrows 5:16
--------------------------------------------------


Put the code you need help with here.
[#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
ifstream inputFile("albuminfo.txt");
ofstream outputFile("tracklist.txt");

inputFile.open("albuminfo.txt");
outputFile.open("tracklist.txt");

string albumT;
getline(inputFile, albumT);

string albumA;
getline(inputFile, albumA);

outputFile << "Album Title: " << albumT << endl;
outputFile << "Artist: " << albumA << endl;
outputFile << "Tracks: " << endl;
outputFile << "------------------------------------------------------------------------------" << endl;

int min = 0, sec = 0;
int numT = 0;
int TotalAM = 0, TotalAS = 0;
string songT;

while (!inputFile.eof())
{
getline(inputFile, songT);



inputFile >> min;
inputFile >> sec;
numT += 1;
outputFile << setw(2) << left << setfill('0') << right << numT << " - ";
outputFile << setw(40) << left << setfill(' ') << songT;
outputFile << setw(2) << right << min << ": ";
outputFile << setw(2) << setfill('0') << sec << setfill(' ') << endl;

TotalAM += min;
TotalAS += sec;

inputFile.ignore();
}

TotalAM = TotalAM + (TotalAS / 60);
TotalAS = TotalAS % 60;

outputFile << "------------------------------------------------------------------------------" << endl;

outputFile.close();
inputFile.close();

cout << "Welcome to Jalen Keller's tracklist generator!" << endl;
cout << "Processed " << numT << " tracks" << endl;
cout << "Total Album Length: " << TotalAM << ":" << TotalAS << endl;



system("pause");
return 0;]
Last edited on
What does the contents of your output file look like?
Hello Rayjk,

While I load up your program and give it a test this may help for now.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


I take it that the first lines are the input file?

Andy
Hello Rayjk,

The only way I was able to duplicate your problem was to open a file name that did not exist. The open did not work, but the program continued any way.

The first question is how do you know that the input file is open and usable? YOu do not.

I found this to be useful when I first started working with input and output files:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const std::string inFileName{ "albuminfo.txt" };
const std::string outFileName{ "tracklist.txt" };

std::ifstream inFile(inFileName);

if (!inFile)
{
	std::cout << "\n File " << std::quoted(inFileName) << " did not open" << std::endl;
	//std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread". Optional as is the header files.
	return 1;  //exit(1);  // If not in "main".
}


std::ofstream outFile(outFileName);

if (!outFile)
{
	std::cout << "\n File " << std::quoted(outFileName) << " did not open" << std::endl;
	//std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread". Optional as is the header files.
	return 2;  //exit(2);  // If not in "main".
}

Lines 1 and 2 are defined as constant variables because they should not be changed, but should you need this variable for user input just remove the "const".

Lines 4 and 14 both define the file stream variable and open the file at the same time.

The if statement at line 6 is a must for input files. Kind of 50/50 for output files because if the file name does not exist it will create it first. The if statement for the output file stream is useful if you have a path to the file name.

Lines 9 and 19 are optional. I use this because when the program executes a return, or "exit()", the console window closes and any last messages sent to the screen are lost when the console window closed. Some IDEs will wait for a key press to close the console window in which case these lines are not needed.

Lines 10 and 20 the "return" is used if the code is in "main". Use the "exit()" if used in a function otherwise you will just return back to the calling function.

The reason for the "return", or "exit()" is because there is no reason to continue with the program if you can not read from the file. This needs to be fixed before the program can continue.

After that the program works down to the while loop.

The while condition is a problem. while (!inputFile.eof()) this does not work the way that you are thinking. By the time the condition figures that you have reached "eof" you have already entered the loop tried to read past "eof" and your input stream has failed, but you still have to process what you could not read. Usually this uses what was left in the variables form the last read and gives you two of the last good read before the loop ends.

I cave come to the conclusion that students ate taught about the ".eof()" function, but not how to use it properly. To use what you have you should do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
getline(inputFile, songT); // <--- This here to catch if an empty file. Or read the first line.

while (!inputFile.eof())
{
	inputFile >> min;
	inputFile >> sec;

	numT += 1;

	outputFile << setw(2) << left << setfill('0') << right << numT << " - ";
	outputFile << setw(40) << left << setfill(' ') << songT;
	outputFile << setw(2) << right << min << ": ";
	outputFile << setw(2) << setfill('0') << sec << setfill(' ') << endl;

	TotalAM += min;
	TotalAS += sec;

	inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

	getline(inputFile, songT); // <--- This here to catch when "eof" is reached.
}


The method that is most often used is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while (getline(inFile, songT))
{
	inFile >> min;
	inFile >> sec;

	numT += 1;

	outFile << setw(2) << left << setfill('0') << right << numT << " - ";
	outFile << setw(40) << left << setfill(' ') << songT;
	outFile << setw(2) << right << min << ":";
	outFile << setw(2) << setfill('0') << sec << setfill(' ') << endl;

	TotalAM += min;
	TotalAS += sec;

	inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
}


This way when "getline" tries to read past "eof" the stream fails and so does the condition continuing with what comes after the while loop.

Inside the while loop you have min << ": ";. I removed the space after the colon so that the output would look better.

I played around with these two line:
1
2
3
outFile << "----------------------------------------------------" << endl;// For a better appearance take 26 or 27 "-"s
//outFile << std::string(52, '-') << '\n';
outFile << "Total Time" << std::string(35, ' ') << TotalAM << ':' << TotalAS << std::endl;

Line 2 is another way of writing line 1 that is easier to work with and change. Line 3 I added for fun. Use as you see fit.

Hope that helps,

Andy
Topic archived. No new replies allowed.