multiple output files

Hi all,

imagine that you have a file (for instance .txt) where there is a line with a date. Below that line there's information for that day. Then, again another line with another date and the respective information to that date... and so on.

How can I pick those dates and put them into the names of the output files?

I would really appreciate if your help was in c++.

Thanks for your time,
Ska




"Gravity isn't responsible for people falling in love." - Albert Einstein
It would be a beautiful world if everyone could wake up, have an idea, explain it and ask for the answer to come as the finished product. Meaning that you can't just ask for code without trying something yourself, if you won't put in the effort no-one else will.

One approach is to add a special character after the dates on that line, when you read a line that ends in that character it must be a date. Thats one approach.
Look.

I read the beginners stuff etc... They say not to put code and ask for answers...so that's what I did. NOT PUT CODE! So if you want to see the code just ask for it.

Another thing, I wouldn't be just waiting for help and not try something. It's just a waste of time, wouldn't you agree?

I don't need the full program. Just the commands that give the name of the output file from words inside the input file.
Last edited on
No, you look.

Umz wasn't very nice, but in this world, he was actually fairly kind.

The beginner's stuff says not to just give answers -- especially code -- to those who ask. You have to learn this stuff. Not the people who can post pretty C++ code for you. Those who can get really tired of "please post code for my elementary question" kind of posts. A good question states
1. exactly what you want (yours was very vague),
2. what you have done to attempt it (which you didn't),
3. what you would like to learn to fix it yourself (you only asked for a solution, not guidance to figuring out your own solution).


But enough of that.

From what you posted, I will assume (and I could be wrong) that your input file is something like:

28/04/99
Sunny. High 73F.
29/04/99
Cloudy. High 65F.
30/04/99
Thunder storms. High 68F.

In this case, all you need is a loop to read every two lines (into two string variables, say string date and string info) until there are no more lines to read. Each time through the loop, if you have successfully read both lines, modify the first string (date) to look like the new filename, then use it to create the new file, write the info, and close it.
1
2
3
ofstream f( date.c_str() );
f << info << endl;
f.close();


If that doesn't help then you need to state more clearly what you are trying to do, and post what you have done so far.

Hope this helps.
"I read the beginners stuff etc... They say not to put code and ask for answers...so that's what I did. NOT PUT CODE! So if you want to see the code just ask for it."


I think that you misunderstood. You NEED to post some code so that we understand what you need help with. What you shouldn't do is give a vague description of what you are trying to do without posting what you have attempted thus far (properly indented and surrounded by code tags).
First of all. I'm Sorry for the hard words and all. I was tired and ansious while writing that last post.

It's been hard to find an answer for this question, so I thank all of you trying to help.

Now let's get to the business. Here's the code, the comments are your guidelines and the Bold comments are the problems.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<iostream>
#include<fstream>
#include<cmath>
#include<math.h>
#include<string>

using namespace std;

int main(){

    float longitude, latitude;
    int i=0, k=0, linenumber=0, wordnumber=0;
    int ConvertToInt, direction, velocity, level;
    string word, line, hour, date;

    ifstream windIn;
    ofstream windOut;

    windIn.open("file.win", ios::in);
    windOut.open("wind.txt", ios::out);//this is the file I use now to put all the info but that's 
                                       //not what I want as you will see.

    windOut << "Longitude,Latitude,Direction,Velocity,Date-Hour-Level\n";

    if(windIn.is_open()){
        while(!windIn.eof()){
            while(linenumber<20){     //Before the 20th line it's just a header.
                getline(windIn,line);
                linenumber++;
            }

            if(linenumber==20||linenumber==96||linenumber==172){//these are the lines with information
                                                                //about the date, hour, etc.
                linenumber++;

                for(wordnumber=0;wordnumber<=13;wordnumber++){
                    windIn >> word;
                    if(wordnumber==5){//word number 5 (actually number 4!!) corresponds to the hour.
                        hour=word;
                    }
                    if(wordnumber==8){//word number 8 (actually number 7!!) corresponds to the date.
                        date=word;
                    }
                    if(wordnumber==11){//word number 11 (actually number 10!!) corresponds to the height.
                        if(word=="medium"){
                            level=0;
                        }
                        else{
                            ConvertToInt=atoi(word.c_str());
                            level=ConvertToInt;
                            wordnumber++;
                        }
                    }
                }
            }

            //latitude and longitude are known points not read from the file.

            while(linenumber>20 && linenumber<96){
                //Here should appear the command to open an output file called "Wind-Hour-Date-Level.txt"
                //where Hour, Date and Level correspond to the respective variables.
                for(i=0;i<25;i++){
                    longitude=-20.0+i*1.0;
                    for(k=0;k<18;k++){
                        windIn >> word;
                        windOut << longitude << ",";
                        latitude=30.0+k*1.0;
                        windOut << latitude << ",";

                        ConvertToInt=atoi(word.c_str());
                        velocity=ConvertToInt%1000;
                        direction=ConvertToInt/1000;
                        windOut << direction << "," << velocity << ",";
                        windOut << year << "-" << month << "-" << day << "---" << hour << "--" << level << "\n";
                    }
                    linenumber=linenumber+3;//I put +3 and not +1 because this is implicated by
                                            //the input file. This is no trouble. More will appear.
                }
            }

            while(linenumber>96 && linenumber<172){
                //Again here should appear the command to open an output file called "Wind-Hour-Date-Level.txt"
                //where Hour, Date and Level correspond to the respective variables.
                for(i=0;i<25;i++){
                    longitude=-20.0+i*1.0;
                    for(k=0;k<18;k++){
                        windIn >> word;
                        windOut << longitude << ",";
                        latitude=30.0+k*1.0;
                        windOut << latitude << ",";

                        ConvertToInt=atoi(word.c_str());
                        velocity=ConvertToInt%1000;
                        direction=ConvertToInt/1000;
                        windOut << direction << "," << velocity << ",";
                        windOut << year << "-" << month << "-" << day << "---" << hour << "--" << level << "\n";
                    }
                    linenumber=linenumber+3;
                }
            }

            while(linenumber>172 && linenumber<248){
                //Again here should appear the command to open an ouput file called "Wind-Hour-Date-Level.txt"
                //where Hour, Date and Level correspond to the respective variables.
                for(i=0;i<25;i++){
                    longitude=-20.0+i*1.0;
                    for(k=0;k<18;k++){
                        windIn >> word;
                        windOut << longitude << ",";
                        latitude=30.0+k*1.0;
                        windOut << latitude << ",";

                        ConvertToInt=atoi(word.c_str());
                        velocity=ConvertToInt%1000;
                        direction=ConvertToInt/1000;
                        windOut << direction << "," << velocity << ",";
                        windOut << year << "-" << month << "-" << day << "---" << hour << "--" << level << "\n";
                    }
                    linenumber=linenumber+3;
                }
            }
            getline(windIn,line);
        }
    }
    else{
        cout << "ERROR: The File file.win wasn't found.\n";
    }

    windOut.close();
    windIn.close();

    return 0;
}
Last edited on
Topic archived. No new replies allowed.