Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Program wont write to file

Program wont write to file

Scheduled Pinned Locked Moved C / C++ / MFC
helpiosdatabase
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CNewbie
    wrote on last edited by
    #1

    /* 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 write the text to the file, anywhere. If someone can tell me why I'd love to know, because I cannot figure out why. Thanks */ #include #include #include 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; }

    S D 2 Replies Last reply
    0
    • C CNewbie

      /* 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 write the text to the file, anywhere. If someone can tell me why I'd love to know, because I cannot figure out why. Thanks */ #include #include #include 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; }

      S Offline
      S Offline
      Sujan Christo
      wrote on last edited by
      #2

      Hi, I took tried ur code. Seems that fstream is havin some prob. I tried with a different stream for writing after closing the current stream and it worked !. ie added the following code after stream.close(); fstream stream2("database.txt", ios::in | ios::out|ios::app ); stream2 << current << endl; stream2.close(); This means that the first stream is having some problem Sujan

      C 1 Reply Last reply
      0
      • C CNewbie

        /* 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 write the text to the file, anywhere. If someone can tell me why I'd love to know, because I cannot figure out why. Thanks */ #include #include #include 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; }

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Based on Sujan's comment, could it be that getline() is positioning the file pointer past the end of the file such that the append operation fails?


        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

        1 Reply Last reply
        0
        • S Sujan Christo

          Hi, I took tried ur code. Seems that fstream is havin some prob. I tried with a different stream for writing after closing the current stream and it worked !. ie added the following code after stream.close(); fstream stream2("database.txt", ios::in | ios::out|ios::app ); stream2 << current << endl; stream2.close(); This means that the first stream is having some problem Sujan

          C Offline
          C Offline
          CNewbie
          wrote on last edited by
          #4

          Thank you sujan, that did work. :) That makes me wonder why the first stream didnt work? If it did get corrupted like you said. Funny thing, before I posted this I did try where I closed the original stream and then reopened it again and it still didnt work. Go figure!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups