C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 28th, 2006, 11:56 AM
mateoc15's Avatar
mateoc15 mateoc15 is offline
Business Analyst
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Mar 2004
Location: The 'Ville
Posts: 1,087 mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 19 h 17 m 11 sec
Reputation Power: 21
File read/write problem

This is MFC code, but the core of it is just ifstream and ofstream. The file pattern I'm looking for in this test is c:\temp1\test*.txt and the CFileFind successfully finds each file. I get to the part that checks for both files to be open and they both are. The first iteration through I get test1.txt and it opens the file, reads it, and writes it to the output file, no problems. But when I get to the second and third files test2.txt and test3.txt it opens the files, both are open, but it never writes anything to the output file.

Basically it never enters the while loop for files 2 and 3. All three files contain multiple lines and carriage returns. Any ideas?

Thanks in advance!

Code:
bWorking = find.FindFile(csInputFile);
		while(bWorking)
		{
			bWorking = find.FindNextFile();
			
			if(csInputFile.CompareNoCase(csOutputFile) == 0 || bWildcard)
			{
				csTemp = find.GetFilePath() + _T(".bak");
				rename((LPCTSTR) find.GetFilePath(), (LPCTSTR) csTemp);
			}	
			else
			{
				csTemp = csInputFile;
			}

			// skip directories ('.' and '..')
			if(!find.IsDots())
			{
				if(bWildcard)
				{
					csOutputFile = find.GetFilePath();
				}
				else
				{
					csOutputFile = csTemp;
					csOutputFile.TrimRight(_T(".bak"));
				}

				fInput.open((LPCTSTR)csTemp, ios::in);
				fOutput.open((LPCTSTR)csOutputFile, ios::out);			

// this part works, both are open	
if(fInput.is_open() != 0 &&
				   fOutput.is_open() != 0)
				{
					while(getline(fInput, s))
					{
						fOutput.write(s.c_str(), s.length());
					}

					// clean up
					fInput.close();
					fOutput.close();
				}
				else
				{
					bReturn = false;
				}
			}
		}
__________________
Discontent is the first necessity of progress. - Edison

Last edited by mateoc15 : July 28th, 2006 at 12:06 PM.

Reply With Quote
  #2  
Old July 28th, 2006, 09:44 PM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,676 Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
[jogs memory]Instead of merely closing the streams, I think you also need to clear the flags -- namely that you are still at the end of the input file. Perhaps this will jog some else's memory while I search a bit...

[edit]This is something like what I was remembering.

Perhaps add an output statement where the is_open call fails to verify first.
__________________
Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.

Last edited by Dave Sinkula : July 28th, 2006 at 09:55 PM.

Reply With Quote
  #3  
Old July 31st, 2006, 02:33 PM
mateoc15's Avatar
mateoc15 mateoc15 is offline
Business Analyst
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Mar 2004
Location: The 'Ville
Posts: 1,087 mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level)mateoc15 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 19 h 17 m 11 sec
Reputation Power: 21
Quote:
Originally Posted by Dave Sinkula
[jogs memory]Instead of merely closing the streams, I think you also need to clear the flags -- namely that you are still at the end of the input file. Perhaps this will jog some else's memory while I search a bit...

[edit]This is something like what I was remembering.

Perhaps add an output statement where the is_open call fails to verify first.

I did a clear() to both files before closing them and solved the problem. Thanks!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > File read/write problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap