473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 5574
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

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

Similar topics

4
by: Jens Lippmann | last post by:
Hi all! I'm new to Python and just tried to assign values to a portion of a string, but I don't get it. My code is: bits = '\3ff' * bmi.bmiHeader.biSizeImage ofs = 0x1E2C0 for i in range(0,...
9
by: Rafi Kfir | last post by:
Hi, This may look as a smiple task to most of you, but to me (a beginner with C), it drives me crazy. All I want is that one function passes a two dimensional array of strings to another...
4
by: Barry | last post by:
Hi all! I support a rather large production EDI application with a number of C programs, and I ran across a very interesting problem. I have some code that used to work just fine for years, and...
1
by: Jean-Marc Blaise | last post by:
IBM recommends to place each ts container on a different physical disk. What about the impacts if all containers (DMS) are in the same FS (supported by multiple disks) ? Does DB2 preallocate...
14
by: Dennis Benzinger | last post by:
Hi! The following program in an UTF-8 encoded file: # -*- coding: UTF-8 -*- FIELDS = ("Fächer", ) FROZEN_FIELDS = frozenset(FIELDS) FIELDS_SET = set(FIELDS)
15
by: Nindi73 | last post by:
HI If I define the class DoubleMap such that struct DoubleMap : public std::map<std::string, double>{}; Is there any overhead in calling std::map member functions ? Moreover are STL...
4
by: nass | last post by:
hello everyone, i have a bit of problem reading char * strings from a buffer (a shared memory, pointed to by 'file_memory'). basically i have a structure in memory 'ShMem' that can be accessed by...
4
by: CoreyWhite | last post by:
/* WORKING WITH STRINGS IN C++ IS THE BEST WAY TO LEARN THE LANGUAGE AND TRANSITION FROM C. C++ HAS MANY NEW FEATURES THAT WORK TOGETHER AND WHEN YOU SEE THEM DOING THE IMPOSSIBLE AND MAKING...
6
by: arnuld | last post by:
On Tue, 09 Sep 2008 10:12:54 +0200, Fred Zwarts wrote: Thats array .. oh .. no. and I thought I was giving the maximum number of elements this vector must have. This is the new version with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.