Actually, it does check whether the file is open and it does also show the contents of the file...The only thing that doesn't work is the line count I made. But I see what you mean: Below is the whole code:
unsigned int lc=0;
reDo:
cout << "\n\n\n\n\tUser, please Enter the filename (with directory) below:\n\n\t\t::==:: ";
getline(cin, filename_user);
ifstream read(filename_user.c_str());
if(read.fail())
{
cout << "\n\n\t";
error_slashes();
cerr << "\n\n\t Sorry, but this program was unable to locate/read\n\n\t\t\tthe given file!\n\n\t";
error_slashes();
cin.get();
cout << "\n\n\t\tDo you wish to open a new file? (y/n[quit]) ";
cin >> choice;
if(choice=='y')
{
goto reDo;
}
else
{
exit(1);
}
}
read.open(filename_user.c_str(), ios::in);
while(getline(read, linecount, '\n'))
{
++lc;
}
read.clear();//clear memory
read.close();
read.open(filename_user.c_str(), ios::in);
while(getline(read, lines, '\n'))
{
cout << lines << "\n\n\n";
}
cout << "\n\n\t\t" << lc;
In the code, it checks for the existence of the file before opening...and it does output the file contents...Only thing not working is the line count.
Just out of curiosity: You do know that void main() doesn't exist right? I'm referring to your signature...
Good... Even signature is checked! :twisted:
Yes, I am aware that void main doesn't exist as a standard, although this does work in older c++ compilers, like the ones Iused to use...
If this code were to work at all, it would display the cout line as many times as there are newlines in the file. I suppose that wasn't your intent?
There are a few other things wrong with your program, but I guess this isn't all of it?
Well, the code does do this, and the only reason this exists is to check that the program can actually render the file properly...and it does, it keeps printing the line as many times as it encounter '\n' delimiter.
And you're correct, This is just a part of a bigger program. I'll try your code out and will try and modify my own...
Thanks for all the replies...
[EDIT]Thanks for your code and advice, 'niek_e', it helped such a lot! The code's finally working!!
Why even bother reading the signature?(although you got to admit, some are pretty funny)
I quite agree to that myself....
I'm trying to help people with C++. void main() isn't standard C++, so every time I encouter it, I mention it to the poster. That way, when/if the poster gets a job in the programming-field, he/she won't get laughed out of the building by using void main() .
What difference does it make if I see it in the post or in the signature?
I'm trying to help people with C++. void main() isn't standard C++, so every time I encouter it, I mention it to the poster. That way, when/if the poster gets a job in the programming-field, he/she won't get laughed out of the building by using void main() .
What difference does it make if I see it in the post or in the signature?
And you're correct as well, but I never use void main() now-a-days, although I have some really old compilers on my computer that accept void main() , so I used it when I was learning basic c++. And the only reason its like that in my signature is because you're allowed only 5 lines, and a return statement will make 6...