# File program doubt

This is a discussion on File program doubt within the C++ Programming forums, part of the General Programming Boards category; Hello, Please tell me in the below given code why some of the while loop condition is working and some ...

1. ## File program doubt

Hello,
Please tell me in the below given code why some of the while loop condition is working and some are not! Also tell me which one is the best. Comments are already written as to which one is working and which one is not. Thanks in advance

Code:
#include"fstream.h"
int main(void)
{
ifstream f1("d:t1.txt");
char c[80],s;
clrscr();
cout<<"\n\nThe File Contains :\n";
while(f1.eof()==0)  //loop works perfectly
{
f1.getline(c,80);
cout<<c;
}
while(f1)  //loop for some reason doesn't work?? :(
{
f1.getline(c,80);
cout<<c;
}
while(f1.get(s))  //This condition Works for single character
{
cout<<s;
}
while(f1.getline(c,80))  //This condition doesn't Work for string!
{
cout<<c;
}

return 0;
}

2. If you want to post such code, post compilable code that can be run. If not, just post incomplete code snippets and explain what you think each snippet does.

It seems that what you want to do is:
Code:
std::string str;
while (std::getline(f1, str))
{
std::cout << str << '\n';
}
Or according to cpjust's observations, something along these lines:
Code:
char block[BUFSIZ + 1];
{
std::cout << block;
}
I cannot say why your code, as it is, is not working, because it should not even compile. I could guess that you simply failed to reset the file pointer after reading the file, thus the next attempted read ended immediately as the end of file was encountered.

3. hahahaha laser you are very funny..that even i know that eof has reached . Ok let me put my question in better way!

case 1:
Code:
#include"fstream.h"
int main(void)
{
ifstream f1("d:t1.txt");
char c[80],s;
clrscr();
cout<<"\n\nThe File Contains :\n";
while(f1)  //loop for some reason doesn't work?? :(
{
f1.getline(c,80);
cout<<c;
}
return 0;
}
case 2:
Code:
#include"fstream.h"
int main(void)
{
ifstream f1("d:t1.txt");
char c[80],s;
clrscr();
cout<<"\n\nThe File Contains :\n";

while(fout.getline(c,80))  //This condition doesn't Work for string!But why??
{
cout<<c;
}
return 0;
}

4. You also have kind of a funky path in there too.... I use many operating systems between sun rise and sunset and none of them would take d:t1.txt as a fully qualified file path...

5. d:t1.txt as a fully qualified file path
It is not fully qualified path.
It path that ask to open the file from the current dir on the disk d:

6. Originally Posted by chottachatri
hahahaha laser you are very funny..that even i know that eof has reached . Ok let me put my question in better way!

case 1:
Code:
#include"fstream.h"
int main(void)
{
ifstream f1("d:t1.txt");
char c[80],s;
clrscr();
cout<<"\n\nThe File Contains :\n";
while(f1)  //loop for some reason doesn't work?? :(
{
f1.getline(c,80);
cout<<c;
}
return 0;
}
case 2:
Code:
#include"fstream.h"
int main(void)
{
ifstream f1("d:t1.txt");
char c[80],s;
clrscr();
cout<<"\n\nThe File Contains :\n";

while(fout.getline(c,80))  //This condition doesn't Work for string!But why??
{
cout<<c;
}
return 0;
}
Well I assume because of what laserlight said... as humorous as you may find it to be, your code all and all looks very screwy. I can tell looking at it that it won't compile on any of my compilers, and certainly if it did, it wouldn't run on any of my operating systems.

7. case 1:
//loop for some reason doesn't work??
Your code still does not compile, but this works for me:
Code:
#include<fstream>
#include<iostream>

int main()
{
using namespace std;

ifstream f1("d:t1.txt");
char c[80];

cout << "\n\nThe File Contains :\n";
while (f1)
{
f1.getline(c, 80);
cout << c << '\n';
}
}
case 2:
This condition doesn't Work for string!But why??
It does, once you fix your typo errors and actually compile and run the code:
Code:
#include<fstream>
#include<iostream>

int main()
{
using namespace std;

ifstream f1("d:t1.txt");
char c[80];

cout << "\n\nThe File Contains :\n";
while (f1.getline(c, 80))
{
cout << c << '\n';
}
}
The second is better than the first, methinks, since you want to terminate the loop as soon as end of file is encountered.

8. Originally Posted by vart
It is not fully qualified path.
It path that ask to open the file from the current dir on the disk d:
But it assumes that the "d:" is part of a valid file-path, which for Linux, Unix or AIX would not be true - I don't know for sure if ":" is a valid part of a filename - I think not.

--
Mats

9. Code:
while(fout.getline(c,80))  //This condition doesn't Work for string!But why??
what is fout here?

why not to use std::string?
Code:
	std::ifstream fin("c:\\text.txt");
std::string str;
while(std::getline(fin,str))
{
std::cout << str <<std::endl;
}

10. But it assumes that the "d:" is part of a valid file-path, which for Linux, Unix or AIX would not be true - I don't know for sure if ":" is a valid part of a filename - I think not.

11. well master5001 hahahaha it runs and compiles fine only thing is that it results in infinite loop if i put

Code:
while(f1.eof()==0)
{
f1.getline(c,80);
cout<<c;
}
it's working absolutely fine but when i put while(f1) it results in infinite loop and that's my only question. Also you can write it as d:t1.txt as well as d:\t1.txt. It works perfectly fine on C as well as any C++ compiler. You may check it out!
Cheers!
My only question is what's the difference between while(f1) and while(f1.eof()==0). Kindly answer me that

12. fout is like cout, only 3 letters better, duh....

13. Ok ya that was my mistake instead of f1 i wrote fout...! ;( actually i wrote without testing and compiling but that was a variable name mistake . But now please somebody reply to the question which i ask!?

14. well master5001 hahahaha it runs and compiles fine only thing is that it results in infinite loop if i put
Which standard non-compliant compiler are you using?

it's working absolutely fine but when i put while(f1) it results in infinite loop and that's my only question.
I cannot duplicate that, but then I cannot compile your code as-is either.

My only question is what's the difference between while(f1) and while(f1.eof()==0).
They are both somewhat correct, though while(f1.eof()==0) is less correct, in my opinion.

Ok ya that was my mistake instead of f1 i wrote fout...! ;( actually i wrote without testing and compiling but that was a variable name mistake . But now please somebody reply to the question which i ask!?
Oh yes, so perhaps the code that actually does not work looks nothing like the code you posted, which does not compile.

15. They are both somewhat correct, though while(f1.eof()==0) is less correct, in my opinion.
But both results in processing last line twice, because getline return value is not checked before calling cout <<

Page 1 of 2 12 Last