Put "True results" into different files

This is a discussion on Put "True results" into different files within the C++ Programming forums, part of the General Programming Boards category; I wonder for a solution/approach how to use combinations for a value in a function and to put this results ...

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

    Put "True results" into different files

    I wonder for a solution/approach how to use combinations for a value in a function and to put this results into different files. I will try to put out all information of what I am doing and what I want to do and then explain again.

    I am reading a .txtfile from top to bottom. (The .txtfile look as follows, 2 lines)

    09/19/2006,2113,21.00
    09/19/2006,2114,22.00

    The line represent: Date,Time,Open.

    (The code I use to scan this file look as follows)

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    
    using namespace std;
    int main () 
    { 
    	std::string Date;
    	int Time = 0;
    	char Comma;
    	double Open = 0;
    	ofstream Test;
      
    	Test.open ("file2.txt");
    	ifstream myfile ("CCE.txt");
    			
    	while	(getline(myfile, Date, ','))           		
    	{
    		myfile >> Time;		        	// 2113, 2114
    		myfile >> Comma;                   
    		myfile >> Open;				// 21, 22
    		myfile.get();				// read in trailing newline character
    				
    		
    	if ((Time >= 2113) && (Open >= 21))
    	{
    		Test << Time <<"\n";   
    	}
    	
    	
    	}						// End of While Statement
    	 	return 0;	
    }							// int main braces

    Now I will explain exactly what I want to do.
    I have a function in the code that says:
    if ((Time >= 2113) && (Open >= 21))

    For the first line in the file this statement is true and will return: 2113 to a file called "file2". This is quite simple.
    So what I want to do now is this. I will give an example.
    So I have 2 values called, Time and Open, okay!.

    For these values that for the moment in the function is: 2113 and 21.
    I want the function to test different combinations for these, that I will have an oppurtunity to specify with an interval like this:

    For Time:
    2113 To 2114 Interval: 1 (This meens to first test 2113 and then 2114 because the interval was set to 1)
    For Open:
    21 To 22 Interval: 1 (This meens to first test 21and then 2 because the interval was set to 1).

    (It is like a "StartValue" To "EndValue" with an Interval, ex: 1 in this case)

    So the combinations out of this that will tests is Totally 4 then, that look like this:
    2113 21
    2114 22
    (2113 and 21, 2113 and 22, 2114 and 21, 2114 and 22)

    This makes as seen 4 combinations, that will be tested for each row in the file. So for the moment as seen in my code posted I can only do 1 combination.

    What I then want to do is to store the output to 4 different files(Each combination will find it&#180;s own file. For example will the first combination store its values to a file called: file1, Combination 2 will store its values to a file called: file2 etc... to file4 for the last combination.

    Is there any ideas how I can solve this. I will be very happy for ideas and help on this.
    /Coding.
    Last edited by Coding; 12-26-2007 at 04:52 PM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Location
    North Georgia Mountains
    Posts
    11
    First, >==, what sort of operator is that - did you mean >=?
    For the logic, use two loops, one nested inside of the other - you can use while or for:
    simple example:
    Code:
    for(i=0; i<10;i++){
        ....
        for(j=1; j,5;j++){
         .....
        }
        ....
    }

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Thanks... I think I will try out a solution to use nested loops and see what I will get out of that...
    Last edited by Coding; 12-26-2007 at 02:58 PM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    I have tried an approach that I beleive should work, where I have nested a while loop into an for loop like this:

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    
    using namespace std;
    int main () 
    { 
    
    	for (double Minute = 2113; Minute < 2115; Minute++) 
    	{
    
    	std::string Date;
    	int Time = 0;
    	char Comma;
    	double Open = 0;
    	ofstream Test;
      
    	Test.open ("file2.txt");
    	ifstream myfile ("CCE.txt");
    			
    	while	(getline(myfile, Date, ','))           		
    	{
    		myfile >> Time;		        	// 2113, 2114
    		myfile >> Comma;                   
    		myfile >> Open;				// 21, 22
    		myfile.get();				// read in trailing newline character
    				
    		
    	if (Time == Minute)
    	{
    		Test << Time <<"\n";   
    	}
    	
    	
    	}						// End of While Statement
    	}	                                       // End of For Loop
    		return 0;	
    }							// int main braces
    While the "for loop" is stepping from 2113 to 2114 with an increament with 1, I want to write the current time to a file like the code says. The file is called "file2".
    But when I run the code, only the last time in the two rows is written to the file.
    What I am trying to for this statement is that first the code will try out the first row where Minute tells 2113, here it will write a line to the file, then on that same row the for loop will try out 2114 also as it is stepping(increasing) and the same for next row that has the time 2114:
    So the output I want to the file should look like this then but the file only writes 2114:
    2113
    2114

    if (Time == Minute)
    Last edited by Coding; 12-26-2007 at 05:22 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Because you open the file for writing, you clobber its contents -- so you never see the output of 2113. And you don't get 2115 because you used <, not <=. (Not sure if this is what you wanted or not.)

    Consider opening the file for append, or just opening the file once outside the for loop.

    [edit] 5,800th post! [/edit]
    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
    Dec 2007
    Posts
    383
    Thanks that did work... I changed the opening of the file to this and now this is working:
    Test.open ("file2.txt",ios::app);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do cpp Files get Included Automatically in VS?
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 05-20-2009, 05:12 PM
  2. Where to put files to be read in on a Mac
    By bassist11 in forum C Programming
    Replies: 3
    Last Post: 03-12-2009, 10:39 AM
  3. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 01:59 AM
  4. fopen vs. _open (for BIG image files)
    By reversaflex in forum C Programming
    Replies: 3
    Last Post: 04-01-2007, 01:52 AM
  5. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 02:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21