Thread: Problem writing to file

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    Problem writing to file

    I have this code below and I am reading from a file named "Main.txt".
    The file contain these rows:

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

    Now I want to put the last value that is 1535 to a file while I read the file "Main.txt". I will put these values to a new file that I have called "MainResult.txt".

    When I run this code, only a "0" is written to the new file "MainResult.txt".
    What I am after is:

    1535
    1535
    1535

    What could be wrong in this code ?


    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;
    }
    Last edited by Coding; 01-19-2008 at 10:41 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Try printing out your other variables that you read in. You may be surprised.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes I know, I tried this too and then the whole line except the first letter "A" is printed to the file... Very confusing.. what could be wrong...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No, I meant print each variable, directly to the screen, one at a time so you can see what's where.

    My bet is that the entire line is in the Name variable.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes, If I am print Name then this is put to file, (the first "AAA" that is the name is not written):
    BBB,01/04/2001,01/04/2001,1535

    Then If I am writing "Action" or "BDate" then everything is written except A:
    BC,BBB,01/04/2001,01/04/2001,1535

    SDate gives just an emty line. (It does the "\n") and BTime prints a 0 to the file.

    It seems very strange, the code look clean to me but something must be wrong :/

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're making an incorrect assumption, namely that << stops at a comma. It does not; it only stops at whitespace. So:
    the getline reads up to the comma;
    then << Name reads up to the \n;
    then << Comma reads one character, namely the first 'A' on the next line;
    then << Action reads up to the \n;
    then << Comma reads one character, namely the first 'A' on the next line;
    then << BDate reads up to the \n;
    and we're out of data, so the rest is junk.

    If you want to split on commas, you need to keep using getline as in your while statement thingy.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I kind of following what you meen but I am not really sure if I understand everything. Is it possible if you know to show a code example where I am doing wrong.

    As you said, what I beleive that I am doing is to split the line with the delimiter ',' and these are inside the while loop that I have declared as Comma.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I found out that If I am reading lines that look like this instead:

    01/04/2001,1535,1540
    01/04/2001,1535,1540
    01/04/2001,1535,1540

    Then it do work. I have 1540 printed to the file 3 times.


    So if the line before looked like this:

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


    What could be the difference. The first that works I do only have one String variable(Name) followed by 2 int variables that I use like:

    while ( getline(MainFile, Name, ', ') )



    The second example uses 4 String variables. Also I try with 2 string variables in the beginning it does not work. So something is happening
    when using more than one stringvariable in the beginning..
    Last edited by Coding; 01-19-2008 at 11:12 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Coding View Post
    As you said, what I beleive that I am doing is to split the line with the delimiter ',' and these are inside the while loop that I have declared as Comma.
    You can believe that all you want, but it doesn't change reality: << does not stop for commas, or any character other than whitespace (spaces, tabs, end of line). If you want to stop at commas, you can use getline. You've got an example where it works:
    Code:
    getline(Main, Name, ',');
    There's no reason you can't read in BDate, and Action, and all those other variables that way too.
    Last edited by tabstop; 01-19-2008 at 11:17 PM. Reason: C++ not C

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes, I will try to experiment how it could be done as you also said, as it should work as I beleive too. Many thanks for your time and help...

    Coding

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I did find out where the problem were. For Name that is red with getline. It should be declared as a string like this and Action that follow should instead be declared as a char. Then it do work.

    std::string Name;
    char Action;

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Coding View Post
    I did find out where the problem were. For Name that is red with getline. It should be declared as a string like this and Action that follow should instead be declared as a char. Then it do work.

    std::string Name;
    char Action;
    How do you get "BBB" (I'm assuming that was intended to be the action) to fit into a char?

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Using getline(stream, string, delimiter), only once, is the wrong approach to solving the problem.

    In this use, getline() will stop at the first comma - so that part, in concept and implementation, is correct. However, all subsequent Main >> string|char ; clauses are not doing anything with the delimiter, as you, I think, expect. The first >> operator is pulling in the remainder of the first line of data (on the first loop).

    If your loop was full of additional getline(stream,string, delimiter) statements, you would achieve what you want to achieve using the logic structure you currently have. (Omitting the stream pulls for the comma, of course.)

    If it were me, I would simply getline(stream,string), without the delimiter, and then inside the loop, I would use string.rfind() to find the position of the last comma, and then string.substr() out the last 4 characters. Simpler, to the point, less to maintain and test.

    Todd
    Last edited by Dino; 01-20-2008 at 11:07 AM. Reason: clarification

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    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;
    }

  15. #15
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Here's a simple example of their use. The exercise is left for you to implement back into your program.

    Code:
    #include <iostream>
    #include <string>  
    
    using namespace std;
    int main () 
    { 
    	std::string Data = "Hi there, my name is Todd, what's yours?" ;
    
    	// To find the last occurence of a character, use rfind()                 
    	// When using string.rfind(), it returns a data type of "string::size_type" 
    
    	string::size_type pos = Data.rfind(',') ; // return the position of the rightmost comma 
    	
    	if (pos == string::npos) { 
    		cout << "Comma not found!! Data format error" << endl ; 
    		return -1 ; 
    	} 
    		
    	// Otherwise, write out the final substring of Data starting after "pos" 
    	cout << (Data.substr(pos+1)) << endl ;  
    	
    	return 0;
    }
    Todd

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