Reading Data from Multiple Files

I am trying to write a program which takes input from 1.yaml , 2.yaml ,3.yaml and so on for 300.yaml files one after the other i.e., my program processes the code for 1.yaml and sends the output to output.dat but I don't know how to give the next yaml file as input as I couldn't afford to write 300 freopen and close statements .
Loops are made for this:
1
2
3
4
5
6
7
8
for(int i = 1; i <= 300; ++i)
{
  std::ifstream ifs{std::to_string(i) + ".yaml"};
  if(ifs.is_open())
  {
...
  }
}
@coder777
I have written the following code for 1 yaml file and I couldn't find a method of how to change the main block of my code to the one you suggested.
Here is my code and please can you find how to change it.

#include<bits/stdc++.h>
using namespace std;


void solve()
{

int total=0;
int noOfBalls=0;
int wicket=0;
string input,country1;
int scoreOfBall;
string Extras,variable,LB,Wide,NoBall,Total;
int f=1;
for(;(cin>>input) && f;)
{
if(input=="team:")
{
f=0;
cout<<"HI";cin>>country1;
}
}
for(;(noOfBalls<300) && (wicket <10);)
{

cin>>variable;

if(variable=="extras: ")
{
noOfBalls++;
cin>>Extras;
if(Extras=="noballs:") {noOfBalls--;}
else if(Extras=="wides:") {noOfBalls--;}

}
else if(variable=="total:")
{
noOfBalls++;
cin>>Total;
int scoreOfBall=0;
stringstream conv(Total);
conv>>scoreOfBall;
total+=scoreOfBall;
}
else if(variable=="wicket:")
{
wicket++;
}
cout<<total<<" "<<wicket<<endl;

}
int main()
{
freopen("001.txt", "w", stdout);
for(int k=1;k<=300;++k)
{
stringstream ss;
ss << k;
string s=ss.str();
///The above 3 lines of code is used as my compiler is not able to compile to_string method

///Generally for one yaml file I would have written the following code

/** freopen("1.yaml","r",stdin);
solve();
**/
/// But now I have to open one yaml after another and apply solve function to it.
}
return 0;
}
1
2
3
4
5
for(int i = 1; i <= 300; ++i)
{
  freopen((std::to_string(i) + ".yaml").c_str(),"r",stdin);
  solve();
}
@coder777

The loop iterated well for 21 iterations only (took an execution time of 0.062sec.
But I want to iterate for at least 300 files.
The new code in main is

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
freopen("001.txt", "w", stdout);
cout<<"S.NO\tTeam\tTOTAL\tWickets\tBalls"<<endl;
for(int k=1;k<=300;++k)
{
cout<<k<<"\t";
stringstream ss;
ss << k;
string s=ss.str();
freopen((s + ".yaml").c_str(),"r",stdin);
solve();
fclose(stdin);
}
return 0;
}

If k is <=21 then the loop executes normally but after it , it doesn't get executed.
Sorry The problem was with my datasets in yaml file.
The problem is solved.
Thank you @coder777
first is it to adding yaml.h with -lyaml or -lyaml-cpp to your linker by QT creator is it the same as bottom show

1
2
3
4
5
6
unix:!macx: LIBS += -L$$PWD/../../../../../usr/lib/x86_64-linux-gnu/ -lyaml
unix:!macx: LIBS += -L$$PWD/../../../../../usr/lib/x86_64-linux-gnu/ -lyaml-cpp
INCLUDEPATH += $$PWD/../../../../../usr/lib/x86_64-linux-gnu
DEPENDPATH += $$PWD/../../../../../usr/lib/x86_64-linux-gnu
unix:!macx: PRE_TARGETDEPS += $$PWD/../../../../../usr/lib/x86_64-linux-gnu/libyaml.a
unix:!macx: PRE_TARGETDEPS += $$PWD/../../../../../usr/lib/x86_64-linux-gnu/libyaml-cpp.a


than you can with that code reading the yaml file.

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
#include <iostream>
#include <yaml.h>
#include <stdio.h>
//#include <yaml-cpp/parser.h>

using namespace std;

int yaml_parser_initialize(yaml_parser_t *);
void yaml_parser_delete(yaml_parser_t *);
int yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token);
void yaml_token_delete(yaml_token_t *token);
void yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file);


int main(void)
{
  FILE *fh = fopen("/etc/netplan/Netzwerk.yaml", "r");
  yaml_parser_t parser;
  yaml_event_t  event;   /* New variable */

  /* Initialize parser */
  if(!yaml_parser_initialize(&parser))
    fputs("Failed to initialize parser!\n", stderr);
  if(fh == nullptr)
    fputs("Failed to open file!\n", stderr);

  /* Set input file */
  yaml_parser_set_input_file(&parser, fh);

  /* START new code */
  do {
    if (!yaml_parser_parse(&parser, &event)) {
       printf("Parser error %d\n", parser.error);
       exit(EXIT_FAILURE);
    }

    switch(event.type)
    {
    case YAML_NO_EVENT:
        puts("No event!");
        break;
    /* Stream start/end */
    case YAML_STREAM_START_EVENT:
        puts("STREAM START");
        break;
    case YAML_STREAM_END_EVENT:
        puts("STREAM END");
        break;
    /* Block delimeters */
    case YAML_DOCUMENT_START_EVENT:
        puts("Start Document");
        break;
    case YAML_DOCUMENT_END_EVENT:
        puts("End Document");
        break;
    case YAML_SEQUENCE_START_EVENT:
        puts("Start Sequence");
        break;
    case YAML_SEQUENCE_END_EVENT:
        puts("End Sequence");
        break;
    case YAML_MAPPING_START_EVENT:
        puts("Start Mapping");
        break;
    case YAML_MAPPING_END_EVENT:
        puts("End Mapping");
        break;
    /* Data */
    case YAML_ALIAS_EVENT:
        printf("Name:    %s\n", event.data.alias.anchor);
        break;
    case YAML_SCALAR_EVENT:
        printf("Eintrag: %s\n", event.data.scalar.value);
        break;
    }
    if(event.type != YAML_STREAM_END_EVENT)
      yaml_event_delete(&event);
  } while(event.type != YAML_STREAM_END_EVENT);
  yaml_event_delete(&event);
  /* END new code */

  /* Cleanup */
  yaml_parser_delete(&parser);
  fclose(fh);
  return 0;
}
Topic archived. No new replies allowed.