Thread: Problem writing to file

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Coding View Post
    Todd Burch,

    The solution with string.rfind() and string.substr() sounds interesting. Still I have the problem.

    I have never used these functions though and have a little problem to implement them in the code. I have tried but not really get it right.
    If you have the time I would appreciate some assistance how an approch could look like for this code.

    The line in the file look like this:

    ABC,BBB,01/04/2001,01/04/2001,1535

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    #include <vector>
    
    using namespace std;
    int main () 
    { 
    
    	std::string Name;
    	std::string Action;
    	std::string BDate;
    	std::string SDate;
    	char Comma;
    	int BTime = 0;
    	
    
    	ofstream MainResult;
      
    	MainResult.open ("MainResult.txt");
    	ifstream Main ("Main.txt");
    
    	
    	while	(    getline(Main, Name, ',')   )           		
    	{
    		                
    		Main >> Action;				
    		Main >> Comma;			
    		Main >> BDate;				
    		Main >> Comma;			
    		Main >> SDate;				
    		Main >> Comma;			
    		Main >> BTime;			
    		
    		Main.get();				// read in trailing newline character
    
    		
    		MainResult << BTime << "\n";
    		
    
    	
    	}						// End Whilestatement
    
    		return 0;
    }
    Isn't this where you started? Why do you insist on using >> when everyone here, and your own experiments, tell you it doesn't do what you want? You may believe that >> stops at commas, but it does not. Again, to read up to the comma, use getline -- as you have it in your program! --
    Code:
    getline(Main, Action, ',');
    getline(Main, BDate, ',');
    et cetera. Note as well that getline finds the comma and throws it away for you.

    Todd's idea is simpler, but will only find the last part of the line. If you will never need Action and Date and all the rest, then there's no reason to try to read them. If you do need them, or will in the future, then you'll have to parse through the line as above.

  2. #17
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Thanks Todd.. I managed it :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Creating File for File Processing
    By Dampecram in forum C Programming
    Replies: 2
    Last Post: 12-07-2008, 01:26 AM
  2. Problem with file writing
    By Goldrak in forum C++ Programming
    Replies: 7
    Last Post: 04-09-2006, 06:46 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM