Thread: File I/O Sorting Months

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    45

    File I/O Sorting Months

    Efficiency is my problem here.
    I have a text file(10mb) that at (535,2) (on each line) it has a two digit code for the month. I need to make a separate file to break each month into its own file not all rows will probably be in the same file but could. Before the while loop I defined ofstream date01-date12. Exactly like this:
    Code:
    ofstream date01,date02,date03,date04,date05,date06,
    date07,date08,date09,date10,date11,date12;
    while ( getline(source2,s2Line) )  
        {        
            cout<<s2Line.substr(535,2)<<"  ";
            if ("01" ==s2Line.substr(535,2))
            {
                if (!date01)
                          ofstream date01("January.all");
                date01<<s2Line<<endl;
                g++;
            }
    
            if ("02" ==s2Line.substr(535,2))
            {
                if (!date02)
                    ofstream date02("Febuary.all");
                date02<<s2Line<<endl;
                g++;
            }
    ETC......
    With this code the files are made only when the if statement is satisfied but nothing is written to them. While testing I have defined each month with its own file before the while but then I open 12 months when only three might be needed.
    Any suggestion? If I was unclear let me know.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Try:

    Code:
    if (!date01.is_open())
        date01.open("January.all");
    date01<<s2Line<<endl;
    Let's look at a sample from your posted code and examine why this is happening:

    Code:
    ofstream date01;
    
    ...
    
    if ("01" ==s2Line.substr(535,2))
    {
        if (!date01)
            ofstream date01("January.all");
        date01<<s2Line<<endl;
        g++;
    }
    The line in red above creates a completely new ofstream object from the one declared in the line in blue. The one in red only has scope within that single line statement. The line in red has the effect of opening and closing immediately. This ofstream object has nothing to do with the one declared in blue. Since the object in red only has scope in that one line where it is created, the line below where you attempt to write to the file is actually refering to the ofstream object in blue (the one you NEVER open). This is why nothing is ever written to the file.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This looks like it needs an array or two.
    Something along these lines perhaps
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <fstream>
    
    using namespace std;
    
    int main ( ) {
        char *filenames[] = {
            "january.all", "february.all",
        };
        ofstream files[12];
        ifstream source2;
        string s2Line;
        int g = 0;
    
        while ( getline(source2,s2Line) )
        {
            cout<<s2Line.substr(535,2)<<"  ";
            istringstream foo(s2Line.substr(535,2));
            int month;
            foo >> month;
            month--;    // convert from 1..12 to 0..11
            if ( !files[month] ) files[month].open(filenames[month]);
            files[month] << s2Line << endl;
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    I knew something must have been going on with scope... hk_mp5kpdw used your suggestions with Salem's way of doing it works great now.
    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  2. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-13-2007, 12:48 AM
  3. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM