open output file based on text in input file

I wrote some code to parse a csv file and change all commas to spaces before writing to an output file. Now I want to add some code that will write output to a new file each time the last item on a row changes.

In other words, I have several columns, but the last column is a date and I want to write each line that has the same date to one file. Then when there is a new date, I want to open a new output file (using the date as part of the file name) and write lines to that file until the date changes again.

Here is a highly simplified example of my input file to get the idea across:

Station,Result,Date
FSB_78B,-0.033,20010705
FSB_80C,1.458,20010705
FSB_82B,0.5838,20010705
FSB_78B,-0.033,20010710
FSB_80C,1.458,20010710
FSB_82B,0.5838,20010710

where the date has been written like yyyymmdd so that it can easily be added to the end of the output file name "U235_" such as "U235_20010705" for use as input to another program.

I have tried creating a string variable called outfname that I can change when a new date is detected. But I get errors when I try to open a file with that string as the filename. I have tried several ways of opening an output file that have worked for me in other programs, but none of them are working here.

Can anyone help me? I'll keep trying to work on it. My code follows below.
Thanks,
Jennifer



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdlib>

using namespace std;

void error(const char* p, const char* p2 = " ")
{
    cerr << p << ' ' << p2 << '\n';
    std::exit(1);
}

int main (int argc, char* argv[])
{
    if (argc != 2) error("wrong number of arguments");

    std::ifstream infile(argv[1]);                              // open input file stream
    if (!infile) error("cannot open input file", argv[1]);

    string line, firstline, outline, outfname, prevdate, currdate;
    int linenum = 1;
    int itemnum = 0;

    /**** Need to get the first line which has the headers and save into a string to output to all files ****/
    getline (infile, line);
    istringstream linestream(line);
    string item;
    firstline = "";
    while (getline (linestream, item, ','))
    {
	if (itemnum>0) firstline += " ";
	firstline += item;
	itemnum++;
    }
    firstline += '\n';

    prevdate = " ";
    while (getline (infile, line))
    {
        outline = "";
        linenum++;
        istringstream linestream(line);
        itemnum = 0;
        while (getline (linestream, item, ','))
        {
	    if (itemnum>0) outline += " ";
	    outline += item;
            itemnum++;
        }

	/* last item is the date in the file name format yyyymmdd				*/
	/* need to check to see if outfile is open, then close when last row is reached		*/
	/* so need some kind of flag to signal change in date					*/
	currdate = item;
	if (currdate!=prevdate)
	{
	    //if (linenum!=2) outfile.close();	// NEED TO EXPLICITLY CLOSE THE PREVIOUS FILE??
	    outfname = "U235_" + currdate;
	    ofstream outfile ( (outfname).c_str() );
/*
	    std::ofstream outfile(outfname);                             // open output file stream
*/
	    if (!outfile) error ("cannot open output file", outfname);
	    outfile << firstline;
	    prevdate = currdate;
	}

	outline += '\n';
/*
	outfile << outline;
*/
    }
    //outfile.close(); // NEED TO EXPLICITLY CLOSE THE PREVIOUS FILE?? 

    return 0;
}
Topic archived. No new replies allowed.