I'm making a program that can write RSS files. Now so far everything works right, except one thing, whenever I create a new RSS file (it can open or create a new one), it either doesn't save the file until it exits, which is bad because I need to read the file afterwards. This is my code:
The problem seems like it's not saving the file directly after I close it, because if I take out the check of the first line, it couts nothing, but if I leave it in it only couts the first line. And when I open the file it's the way it's supposed to be. So is there a way I can manually save the file? Thanks.Code://Headers #include <iostream> #include <fstream> #include <string> using namespace std; //Channel info struct channel { string title; string desc; string link; }; //Item info struct item { string title; string desc; string link; string size; string type; int pos; }; //Set up Cahnnel and Items channel chan; item items[100]; //Other variables bool inItem = false; bool inChan = false; bool inRSS = false; string fileName; char fileNameChar[100]; //Because fileName can't be string when used with i/o fstream bool newFile = false; string file[500]; //Will store the file by lines for easier accessing/editing int numLines; string line; //Used to store individual lines when needed // Function Prototypes void insertLine(int pos, string lineInserted); //Start of program int main() { cout << "RSS File Maker" << endl << "By: TyPR124" << endl << endl; cout << "What is the name of the RSS file? (NOTE: \"NEW\" will create a new file)" << endl; getline(cin, fileName); if (fileName == "NEW") { newFile = true; cout << endl << "What should your file be called?" << endl; getline(cin, fileName); } //Check if fileName ends in .xml, add if it doesn't //FIRST CHECK MUST BE IF LENGTH IS < 5 B/C ERROR WILL HAPPEN IF TRY TO GET SUBSTR STARTING FROM NEGATIVE # if ( (fileName.length() < 5) || (fileName.substr(fileName.length()-4, fileName.length() ) != ".xml") ) { fileName = fileName + ".xml"; } //Convert fileName to fileNameChar so it can be used with i/o fstream for (int a=0; a<=fileName.length(); a++) { fileNameChar[a] = fileName[a]; } //Open file for reading/writing (use extend for writing) ifstream RSS_read (fileNameChar); ofstream RSS_write (fileNameChar, ios::app); RSS_read.close(); RSS_write.close(); //If file is new, write channel title, desc, and link if (newFile) { RSS_write.open(fileNameChar, ios::app); RSS_write << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl << "<rss version=\"2.0\">"; RSS_write.close(); cout << endl << endl << "What is the name of the RSS Channel?" << endl; getline(cin, chan.title); RSS_write.open(fileNameChar, ios::app); RSS_write << endl << endl << "<channel>" << endl << endl; RSS_write << "<title>" << chan.title << "</title>" << endl; RSS_write.close(); cout << endl << "What is the description of the RSS Channel?" << endl; getline(cin, chan.desc); RSS_write.open(fileNameChar, ios::app); RSS_write << "<description>" << chan.desc << "</description>" << endl; RSS_write.close(); cout << endl << "What is the link to the RSS Channel?" << endl << "NOTE: IT MUST BE A FULL HTTP LINK" << endl; getline(cin, chan.link); RSS_write.open(fileNameChar, ios::app); RSS_write << "<link>" << chan.link << "</link>" << endl; RSS_write.close(); } //Save the RSS File in the var. file and find the number of lines RSS_read.open(fileNameChar); for (int a=0; getline(RSS_read, line); a++) { file[a] = line; numLines = a; //I won't count the first line as a numbered line because it MUST ALWAYS be the same in RSS files } //Check the first line to make sure it is valid if (file[0].substr(0, 18) == "<?xml version=\"1.0\"") { file[0] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; } else { insertLine(0, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); } //TEST for (int a=0; a<=numLines+1; a++) { cout << file[a] << endl; } cin.ignore(); cin.get(); } void insertLine(int pos, string lineInserted) { string temp[500]; for (int a=0; a<500; a++) { temp[a] = file[a]; } file[pos] = lineInserted; for (int a = pos+1; a <= numLines+1; a++) { file[a] = temp[a-1]; } }



LinkBack URL
About LinkBacks




