do i need threading to do this?

good evening friends,
i am working on files in c++. i created two program working good separately but don't work when merged.
1st program is to create a file "test.txt" which holds name dynamically created. code is below...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <fstream>

using namespace std;

int main()
{
    fstream afile;
    afile.open("test.txt", ios::out);

    int i = 1;
    while(i <= 10)
    {
        afile << "file" << i << ".txt ";
        i++;
    }

    return 0;
}


2nd program is to show names which were created in "test.txt" file. code below...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <fstream>
#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    string file_name;
    fstream read;
    read.open("test.txt", ios::in);
    while(!read.eof())
    {
        read >> file_name;
        cout << file_name;
    }

    return 0;
}


now both work good when i run first program & then second program but when i tried to merge code of both programs into single program it work only half ways means it only create file but don't display its content. code for of merging after merging...

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
#include <fstream>
#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    fstream afile;
    afile.open("test.txt", ios::out);

    int i = 1;
    while(i <= 10)
    {
        afile << "file" << i << ".txt ";
        i++;
    }

    string file_name;
    fstream read;
    read.open("test.txt", ios::in);
    while(!read.eof())
    {
        read >> file_name;
        cout << file_name;
    }

    return 0;
}


now the question is, is it possible to create a file and get its content in use in the same program. or i it is to THREADING, i guess. if it is not about thread then how will we do this in a single program attach code.

i think my question is little bit clear. *can you also tell me little bit about threads or its tutorials about how to use it in c++(if possible attach link of good tutorials of thread).*
thanks in advanse...
Last edited on
i got it i just need to close afile stream before using it "test.txt" somewhere else.
need to put afile.close(); before second half of program.
Topic archived. No new replies allowed.