468,512 Members | 948 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Strings, containers, isstream

Hi,

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
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include "naSrcP.h"
  5. #include "qxP.h"
  6. #include "rwToFile.h"
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     ifstream sfin, sfin_html;
  13.     bool inFileCheck = true;
  14.     bool outFileCheck = true;
  15.     bool docFileCheck = true;
  16.     ofstream sfout;
  17.     string outfile, infile_html, docName;
  18.     char pause;
  19.     string marker = "doc";
  20.     const char delimitChar = '\t';    
  21.     std::vector<string>vecDocName;
  22.  
  23.     //This is the file from which strings are parsed and entered into vector.
  24.     string docfile = "htmlPageVar.txt";
  25.     sfin.open(docfile.c_str());
  26.     //Using  ifstream.isopen check 
  27.     docFileCheck = naFileOperation::inFileCheck(docfile,  sfin);
  28.     if (docFileCheck)
  29.         {}
  30.     else
  31.         cout << "Doc file could not be opened!\n";
  32.  
  33.  
  34.     //Read the file with the document variables and store titles in a vector and sort.
  35.     //This part has been checked. The vector can read in and write out names.
  36.     naHtmlFunc::vecRecTitles(sfin, marker, delimitChar, vecDocName);
  37.  
  38.     //Ask user to select  a page to generate and store the selection  in variable string docName.
  39.     //docName goes into vector as a string and should come out as a string? Shallow copy?
  40.     docName = naHtmlFunc::selectDoc(vecDocName);
  41.     //Checked - docName output is a valid document name.
  42.  
  43.     //Open outfile and associate with an ofstream.
  44.     outfile = "htmlOut.txt";
  45.     sfout.open(outfile.c_str());
  46.     //Uses ofstream.isopen check.
  47.     outFileCheck = naFileOperation::outFileCheck(outfile, sfout);
  48.     if (outFileCheck)
  49.     {}
  50.     else
  51.     {cout << "Out File could not be opened for writing\n";}
  52.  
  53.     //***********************************************************************
  54.     //The problem is here - the string obtained from the vector does not work with ifstream!
  55.     //A filename string entered from within the program works perfectly.
  56.     sfin_html.open(docName.c_str());
  57.     inFileCheck = naFileOperation::inFileCheck(docName,  sfin_html);
  58.     //***********************************************************************
  59.  
  60.     if (inFileCheck)
  61.     {        
  62.         //The rest works perfectly.
  63.         filebuf *pInBuffer, *pOutBuffer;
  64.         //Associate the pointer with the instream buffer.
  65.         sfout.flush();
  66.         char ch;
  67.         pInBuffer = sfin_html.rdbuf();
  68.         pOutBuffer = sfout.rdbuf();
  69.         ch = pInBuffer->sgetc();
  70.         while ( ch != EOF)
  71.             {
  72.                 pOutBuffer->sputc (ch);
  73.                 std::cout << ch;
  74.                 ch= pInBuffer->snextc();
  75.             }
  76.         sfin_html.close();
  77.         sfout.close();
  78.     }
  79.     else cout << "In file does not pass check!\n";
  80.     sfin.close();
  81.     return 0;
  82. }
  83.  
The compiler is gcc-4.1.0 and system Fedora 5.
Any help would be greatly appreciated. Thanks in advance.

Nathan81
Sep 7 '06 #1
1 5399
Hi,

You don't have to worry about this anymore. It turns out that the parseing function drags in an ascii 13 with the strings. So the parsed strings are different from the original strings.

Nathan81

Hi,

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
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include "naSrcP.h"
  5. #include "qxP.h"
  6. #include "rwToFile.h"
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     ifstream sfin, sfin_html;
  13.     bool inFileCheck = true;
  14.     bool outFileCheck = true;
  15.     bool docFileCheck = true;
  16.     ofstream sfout;
  17.     string outfile, infile_html, docName;
  18.     char pause;
  19.     string marker = "doc";
  20.     const char delimitChar = '\t';    
  21.     std::vector<string>vecDocName;
  22.  
  23.     //This is the file from which strings are parsed and entered into vector.
  24.     string docfile = "htmlPageVar.txt";
  25.     sfin.open(docfile.c_str());
  26.     //Using  ifstream.isopen check 
  27.     docFileCheck = naFileOperation::inFileCheck(docfile,  sfin);
  28.     if (docFileCheck)
  29.         {}
  30.     else
  31.         cout << "Doc file could not be opened!\n";
  32.  
  33.  
  34.     //Read the file with the document variables and store titles in a vector and sort.
  35.     //This part has been checked. The vector can read in and write out names.
  36.     naHtmlFunc::vecRecTitles(sfin, marker, delimitChar, vecDocName);
  37.  
  38.     //Ask user to select  a page to generate and store the selection  in variable string docName.
  39.     //docName goes into vector as a string and should come out as a string? Shallow copy?
  40.     docName = naHtmlFunc::selectDoc(vecDocName);
  41.     //Checked - docName output is a valid document name.
  42.  
  43.     //Open outfile and associate with an ofstream.
  44.     outfile = "htmlOut.txt";
  45.     sfout.open(outfile.c_str());
  46.     //Uses ofstream.isopen check.
  47.     outFileCheck = naFileOperation::outFileCheck(outfile, sfout);
  48.     if (outFileCheck)
  49.     {}
  50.     else
  51.     {cout << "Out File could not be opened for writing\n";}
  52.  
  53.     //***********************************************************************
  54.     //The problem is here - the string obtained from the vector does not work with ifstream!
  55.     //A filename string entered from within the program works perfectly.
  56.     sfin_html.open(docName.c_str());
  57.     inFileCheck = naFileOperation::inFileCheck(docName,  sfin_html);
  58.     //***********************************************************************
  59.  
  60.     if (inFileCheck)
  61.     {        
  62.         //The rest works perfectly.
  63.         filebuf *pInBuffer, *pOutBuffer;
  64.         //Associate the pointer with the instream buffer.
  65.         sfout.flush();
  66.         char ch;
  67.         pInBuffer = sfin_html.rdbuf();
  68.         pOutBuffer = sfout.rdbuf();
  69.         ch = pInBuffer->sgetc();
  70.         while ( ch != EOF)
  71.             {
  72.                 pOutBuffer->sputc (ch);
  73.                 std::cout << ch;
  74.                 ch= pInBuffer->snextc();
  75.             }
  76.         sfin_html.close();
  77.         sfout.close();
  78.     }
  79.     else cout << "In file does not pass check!\n";
  80.     sfin.close();
  81.     return 0;
  82. }
  83.  
The compiler is gcc-4.1.0 and system Fedora 5.
Any help would be greatly appreciated. Thanks in advance.

Nathan81
Sep 7 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Jens Lippmann | last post: by
9 posts views Thread by Rafi Kfir | last post: by
4 posts views Thread by Barry | last post: by
1 post views Thread by Jean-Marc Blaise | last post: by
14 posts views Thread by Dennis Benzinger | last post: by
15 posts views Thread by Nindi73 | last post: by
6 posts views Thread by arnuld | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.