Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > Visual C++
|
Visual C++ Questions specific to Microsoft's Visual C++. For questions not specific to this Microsoft version, use the C++ Programming forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 6th, 2004, 08:01 AM
Authorized User
 
Join Date: Mar 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default program won't write to file

/* Ok Here is the jist of what this program is supposed to do:
   This program checks "database.txt" for the text "New String"
   which is in the string name "string". If found it will say so.
   If it goes through the entire file without finding the text,
   it is then supposed to say it has found a new string and then
   append the text "New String" to the end of the file. This code
   is error free. Only problem is that it doesnt append the tex to
   the file, anywhere. If someone can tell me why I'd love to know,
   because I cannot figure out why. Thanks */

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int checkdatabase(int x, std::string current, std::string string)
{
    fstream stream("database.txt", ios::in | ios::out | ios::app);
    if (!stream)
    {
        cout << "Cannot Open database" << endl;
        return 2;
    }
    while (std::getline(stream, string))
    {
        x++;
        if (current == string)
        {
            std::cout << "string #" << x << " Found: " << string << std::endl;
            stream.close();
            return 0;
        }
    }
    x++; std::cout << "***New String Found*** " << "#" << x << ": " << current << std::endl;
    stream << "String" << endl;
    stream.close();
    return 0;
}

int main()
{
std::string current = "New String";
std::string string;
    int x = 0;
    checkdatabase(x,current,string);
    return 0;
}

 
Old December 7th, 2004, 06:20 PM
Registered User
 
Join Date: Dec 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you insert
  stream.clear();
before the line
  stream << "String" << endl;
then it will work (by the way, shouldn't that line be
  stream << "New String" << endl;
?) I'm not 100% sure why, but I think it's something to do with the fact that because you've read to the end of the file the eof bit is set, and this is preventing the text being appended.

Chris Jobson





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write peer to peer file sharing program ethiosoul Visual Studio 2005 1 March 16th, 2008 05:02 PM
How to write program tht will run every day at 9 pallavijyo Pro VB Databases 1 December 3rd, 2004 06:45 PM
Write a program to produce a contents page eapsokha Java GUI 1 September 7th, 2004 11:19 AM
reading HTML from file -> write to file mikeuk Beginning PHP 4 July 21st, 2004 05:40 AM
Write a vb program as services vincentc VB How-To 1 September 19th, 2003 08:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.