I'm somehow messing up opening a file and making a c-style filename

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 2
Reputation: shipeshipaca is an unknown quantity at this point 
Solved Threads: 0
shipeshipaca shipeshipaca is offline Offline
Newbie Poster

I'm somehow messing up opening a file and making a c-style filename

 
0
  #1
Aug 29th, 2008
The following code is part of a program to document the book in my house, like those things they have at public libraries. I learned about minupulating files in programs last night and have many errors in this program.

First, the c-style string for filename comes out as - filename.txt" instead of without the quote

And even if you try running my program with a default filename like newbook it turns out to fail.


Book::Book(string Title,string AuthorLastName,string AuthorFirstName,string PublishingCompany,int SeriesNumber,int NumPages,int CopyrightYear,string Series):
m_Title(Title),
m_Series(Series),
m_AuthorFirstName(AuthorFirstName),
m_AuthorLastName(AuthorLastName),
m_PublishingCompany(PublishingCompany),
m_SeriesNumber(SeriesNumber),
m_NumPages(NumPages),
m_CopyrightYear(CopyrightYear)
{
fstream filehandle;
string filename=m_Title+".txt";

char m_filename[80];
for(int i=0;i<filename.size();++i)
{
        m_filename[i]=filename[i];
}
cout<<m_filename;
filehandle.open(m_filename);




if (!filehandle || !filehandle.good())
{
std::cout << "could not open file!\n";
}
filehandle << "out" << std::endl;
if (filehandle.fail())
{
std::cout << "failed to append to file!\n";
}

filehandle<<m_Title<<"\n";
filehandle<<m_AuthorFirstName<<" "<<m_AuthorLastName<<"\n";
filehandle<<"Published by "<<m_PublishingCompany<<" in "<<m_CopyrightYear<<"\n";
filehandle<<"No. "<<m_SeriesNumber<<" in the "<<m_Series<<" series\n";
filehandle<<m_NumPages<<" pages\n";      
                                                     
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 440
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: I'm somehow messing up opening a file and making a c-style filename

 
0
  #2
Aug 29th, 2008
simply use the 'c_str' function of the string class to convert to c-style string.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

Re: I'm somehow messing up opening a file and making a c-style filename

 
0
  #3
Aug 29th, 2008
Please see my comments below
Originally Posted by shipeshipaca View Post
Book::Book(string Title,string AuthorLastName,string AuthorFirstName,string PublishingCompany,int SeriesNumber,int NumPages,int CopyrightYear,string Series):
m_Title(Title),
m_Series(Series),
m_AuthorFirstName(AuthorFirstName),
m_AuthorLastName(AuthorLastName),
m_PublishingCompany(PublishingCompany),
m_SeriesNumber(SeriesNumber),
m_NumPages(NumPages),
m_CopyrightYear(CopyrightYear)
{
fstream filehandle;
string filename=m_Title+".txt";

char m_filename[80];
for(int i=0;i<filename.size();++i)
{
        m_filename[i]=filename[i];
}
//the c-style string should end with a '\0', 
//so add line m_filename[i] = '\0'; 
//define i out of the for statement
cout<<m_filename;
filehandle.open(m_filename);

// the simplest way is to use filehandle.open(filename.c_str());
                                                     
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC