so this function is working correctly (as far as i know) but the only thing that i haven't quite figured out yet is how to send the new input, and completely overwrite the existing "in_stream" file.

this program opens a file (containing song titles, artists, and their genres) when the file opens the different data types are sent to their specific vectors. the part that i'm currently working on is where the program asks the user to add songs to the library. they cannot be added to the library if the input matches something already in the file (they can have the same song titles, but must have different artist names). if the input is not already existing, the new data should be added to the vectors. after the users quits the program to the data in the vectors should be written back to the file (if the string is empty it should be skipped).

this is my code

#include <iostream> // For input and output to the monitor
#include <fstream> // For file input and output (given)
#include <string> // For text data
#include <vector> // library for using vectors


using namespace std; // To make is easier put this in global namespace

void addsong(vector<string>&, vector<string>&, vector<string>&);
// function: this adds songs to the library
// parameters: string title, string artist, string genre
// functionality: prompts the user for the parameters and checks to make sure
// the song isn't already in the library


int main() 
{
	vector<string> titles;
	vector<string> artists;
	vector<string> genres;
	
	ifstream in_stream;
	ofstream out_stream;
	in_stream.open("music_library.txt");
	
	int i=1;
	if (in_stream.fail( )) 
	{
		cout << "Unable to open file.\n";

	} 
	
	else
	{
				
		string title, artist, genre;
		while(!in_stream.eof())
		{
			if((i%3==1)||i==1)
			{
				getline(in_stream, title);
				titles.push_back(title);
				i++;
			}
			if((i%3==2)||i==2)
			{
				getline(in_stream, artist);
				artists.push_back(artist);
				i++;
			}
			if((i%3==0)||i==3)
			{
				getline(in_stream, genre);
				genres.push_back(genre);
				i++;
			}
			
		}	
	}
	
	cout << "Titles size: " << titles.size() << endl;
	cout << "Artist size: " << artists.size() << endl;
	cout << "Genre size: " << genres.size() << endl;
	
	


	
	int choice;
	do
	{
		cout << "\n\n1. Add a new song\n";
		cout << "2. Remove an existing song\n";
		cout << "3. Search for a song\n";
		cout << "4. Quit program\n";
		cin >> choice;
		
		switch(choice)
		{
			case 1: //user wants to add a song			
			addsong(titles, artists, genres);
				break;
			
			case 2: //user wants to remove an existing song
			
				break;
			
			case 3: //user wants to search for a song
			
				break;
			
			case 4: //user wants to quit program
				cout << "GOODBYE!\n";
				
				break;
			
			
		}	
		
	}while(choice !=4);
	

	in_stream.close( );
	out_stream.close( );
	return 0;
}


void addsong(vector<string>& titles, vector<string>& artists, vector<string>& genres)
{
	ifstream in_stream;
	in_stream.open("music_library.txt");
	
	string t_title, a_artist, g_genre;
	
	cout << "Enter the title name: " << endl;
	cin.ignore(100, '\n');
	getline(cin, t_title); 
	cout << "Enter the artist name: " << endl;
	getline(cin, a_artist);
	cout << "Enter the genre: " << endl;
	getline(cin, g_genre);
	for(unsigned int i = 0; i < titles.size(); ++i)
	{
		if(t_title.compare(titles[i]) !=0&&(a_artist.compare(artists[i])!=0))
		{	
			
			cout << "Items have been added to library" << endl;
			
			titles.push_back(t_title);
			artists.push_back(a_artist);
			genres.push_back(g_genre);
			return;

		}	
		else
				cout << "Item exist!" << endl;
				return;
	}

		
	in_stream.close( );
}

If I'm not mistaken the default behaviour of ofstreams or ios::out mode of fstreams is to overwrite the existing file.

well i didn't know if i did it correctly. i thought that what i set up was correct, but when i check the file after adding data to the library it doesn't show up anywhere.

You declare out_stream and you close out_stream but I don't see where you ever open out_steam or use out_stream. So after you close in_stream and before you close out_stream, you should:

1) open out_stream using the file you want to write to/overwrite if it exists.
2) write material to the file in the desired format

that's actually something that i'm familiar with doing. the project specified that we needed to overwrite the same file. specifically: after the user quits the program all the information stored in the vectors should overwrite any information already in the file.

well the cheap way would be to use DeleteFile(char* szPath); and start a new file(not what is specified :P), or try using fopen(...); which has a definitive overwrite mode, also sometimes a file won't appear if it is still "open" and has had little or no data written to it, or if its not closed properly it can be lost

This article has been dead for over six months. Start a new discussion instead.