Thread: going crazy... wont write to filestream in loop!?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    89

    Angry going crazy... wont write to filestream in loop!?

    Okay I am going absolutely insane. I cannot figure out why this isnt working. I have other pieces of code just like it that work fine... but anyway here it is:

    Code:
    tmpstrm.open("C:\\Tplease.txt");
    if(!tmpstrm.is_open())
    	MessageBox::Show("Not Open", "WTF", MessageBoxButtons::OK);
    tmpstrm.clear();
    tmpstrm << "This line writes!!!\n";
    			
    int cnt = 0;
    
    while (!read->Contains("(END)") && cnt < 500){
    	read = gcnew String(connector.ReadFromPipe().c_str());
    	connector.WriteToPipe("\r");
    	To_string(read, output);
            //  PROBLEM HERE //
    	tmpstrm << "This line wont write!?!?\n";
    	cnt++;
    }
    tmpstrm.close();
    I know the loop is executing as I have stepped thru the code multiple times and have even output cnt as a test to see if its getting incremented. My goal here is to write 'output' to the file but that didnt work so I am now testing with a straight string and still nada. Help

  2. #2
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Have you tried tmpstrm.write()?

    Does your program have permission to write on that file?

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    89
    yea i tried using .write() as well with the same results. Yes it can write to the file apparantly since it writes the first line, but once its in the loop nothing.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    89
    okay I changed some things around a little and I located the line that is screwing everything up, but I dont know how or why it should be having this effect.
    Code:
    tmpstrm.open("C:\\test_log.txt");
    			tmpstrm.clear();			
    			int cnt = 0;
    			while (!tmp->Contains("(END)") && cnt < 500){
    				connector.WriteToPipe("\r");
    				std::string output;
    				To_string(tmp, output);
    				tmpstrm << output;
    				tmp = gcnew String(connector.ReadFromPipe().c_str()); //PROBLEM LINE
    				cnt++;
    			}
    			tmpstrm.close();
    RedFromPipe() just returns a string from the pipe we are reading or "NULL" if it is empty. I dont understand why this would stop my program from writing to the file.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Perhaps the file stream has been set to an error state for some reason?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    89
    okay I did catch a failure, maybe you can help me understand why its failing here.

    Code:
    To_string(tmp, output);
    tmpstrm << output;
    if(tmpstrm.fail()){
    MessageBox::Show("Stream failure.", "stream write failed.", MessageBoxButtons::OK);
    }
    I then put a break at the MessageBox show and when it hit that I checked the output string and it had this:

    Code:
    ction Type          |   Sales  |   Tax   |   Total  | Count  | Check   | 
    |__________________________|__________|_________|__________|________|_________| 
    |Vending: Currency Cash    |     2.50 |     .   |     2.50 |     2  |    1.25 | 
    |_________
    If it helps any, this is coming out of a pipe from Plink (command line PuTTy for telnet). It seems like its getting other lines formatted similarly without problem. Do you think there might be some special character its catching which is screwing up the stream when I write it?
    EDIT: Sorry I should also add that To_string() converts a System::String^ to a std::string since I guess you can use System::String^ in a filestream.

    thanks
    Last edited by ac251404; 10-11-2006 at 11:55 AM.

  7. #7
    Registered User SpaceCadet's Avatar
    Join Date
    Oct 2006
    Posts
    23
    How about a flush?

    Code:
    stream << "Data" << flush;

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Flushing the stream isn't going to get rid of the fail state. You'd need to find out why the stream entered a fail state to begin with.

    The stream might not have been opened. Try .is_open() to see if it was opened. (I doubt writing to a stream that isn't open is a good idea.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-05-2007, 10:56 AM
  2. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  3. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  4. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM