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;
}
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.
#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/