my file doesnt terminate and doesnot show the proper output

hi im a beginer and i dont know file handeling that much just started learning

im making a program that opens a file that with the name user enters and my output is not what i expect to display please help me

im using turbo c++
and please give me solution how to get proper input in turbo c++ only
thanks in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 cout<<"\nVector:>>Enter the Name of the file to open followed with .txt:";
char thefile[512],*thetext;
cin>>thefile;
{
	ifstream fileopener;
	fileopener.open(thefile,ios::in);
	assert(!fileopener.fail());
	fileopener>>thetext;
	while (!fileopener.eof())
	{
	cout<<thetext<<" ";
	}
	fileopener.close();
	assert(!fileopener.fail());
	cout<<endl;
	goto first;
 }
Last edited on
Can you show me where goto first; actually goes to? Or maybe give the full code. What I expect to be the problem is that goto first goes to the start of your program and repeats it all again, thus never exiting.
Well, that isn't a complete program, so it's not possible to see what may be either right or wrong with it.

Generally the best advice for Turbo C++ is - don't. But just this once:
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
#include <iostream.h>
#include <fstream.h>
#include <conio.h>

void create_file(char * name)
{
    ofstream fout(name);
    fout << "first line\n";
    fout << "second line\n";
    fout << "and finally the last line\n";
}

void read_file(char * name)
{
    ifstream fin(name);
    if (!fin)
    {
        cout << "could not open input file\n";
        return;
    }

    char line[512];
    while (fin.getline(line, 512))
    {
        cout << line << '\n';
    }

}

int main()
{
    char filename[] = "test1.txt";

    create_file(filename);

    cout << "Line by line:\n" << "-------------\n";
    read_file(filename);

    cout << "\nword at a time:\n"<< "---------------\n";

    ifstream fin("test1.txt");
    if (!fin)
    {
        cout << "could not open input file\n";
        getch();
        return 1;
    }

    char line[512];
    while (fin >> line)
    {
        cout << line << '\n';
    }

    getch();
    return 0;
}


... but unless you really have to (for example because your school insists on Turbo C++), you are advised to get hold of a compiler from this century before proceeding any further.
http://www.cplusplus.com/doc/tutorial/introduction/
Last edited on
Topic archived. No new replies allowed.