I am working on a program that reads document names from a file, presents these names to a user to select one document and then it generates this document. The programs parses the doc name file and enters these names into a vector. The names are output from the vector for selection. The selected string is passed to ifstream to associate and retrieve the document variables.
The problem is the string from the vector does not work with ifstream. Passing the document name from within the program as a variable works. The documents are all in the working directory. Why would the string from the vector not work?
Here is my code:
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <fstream>
- #include <string>
- #include "naSrcP.h"
- #include "qxP.h"
- #include "rwToFile.h"
- using namespace std;
- int main()
- {
- ifstream sfin, sfin_html;
- bool inFileCheck = true;
- bool outFileCheck = true;
- bool docFileCheck = true;
- ofstream sfout;
- string outfile, infile_html, docName;
- char pause;
- string marker = "doc";
- const char delimitChar = '\t';
- std::vector<string>vecDocName;
- //This is the file from which strings are parsed and entered into vector.
- string docfile = "htmlPageVar.txt";
- sfin.open(docfile.c_str());
- //Using ifstream.isopen check
- docFileCheck = naFileOperation::inFileCheck(docfile, sfin);
- if (docFileCheck)
- {}
- else
- cout << "Doc file could not be opened!\n";
- //Read the file with the document variables and store titles in a vector and sort.
- //This part has been checked. The vector can read in and write out names.
- naHtmlFunc::vecRecTitles(sfin, marker, delimitChar, vecDocName);
- //Ask user to select a page to generate and store the selection in variable string docName.
- //docName goes into vector as a string and should come out as a string? Shallow copy?
- docName = naHtmlFunc::selectDoc(vecDocName);
- //Checked - docName output is a valid document name.
- //Open outfile and associate with an ofstream.
- outfile = "htmlOut.txt";
- sfout.open(outfile.c_str());
- //Uses ofstream.isopen check.
- outFileCheck = naFileOperation::outFileCheck(outfile, sfout);
- if (outFileCheck)
- {}
- else
- {cout << "Out File could not be opened for writing\n";}
- //***********************************************************************
- //The problem is here - the string obtained from the vector does not work with ifstream!
- //A filename string entered from within the program works perfectly.
- sfin_html.open(docName.c_str());
- inFileCheck = naFileOperation::inFileCheck(docName, sfin_html);
- //***********************************************************************
- if (inFileCheck)
- {
- //The rest works perfectly.
- filebuf *pInBuffer, *pOutBuffer;
- //Associate the pointer with the instream buffer.
- sfout.flush();
- char ch;
- pInBuffer = sfin_html.rdbuf();
- pOutBuffer = sfout.rdbuf();
- ch = pInBuffer->sgetc();
- while ( ch != EOF)
- {
- pOutBuffer->sputc (ch);
- std::cout << ch;
- ch= pInBuffer->snextc();
- }
- sfin_html.close();
- sfout.close();
- }
- else cout << "In file does not pass check!\n";
- sfin.close();
- return 0;
- }
Any help would be greatly appreciated. Thanks in advance.
Nathan81