Thread: File program doubt

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    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;
    }
    Last edited by chottachatri; 04-24-2008 at 04:29 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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];
    while (f1.read(block, BUFSIZ))
    {
        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.
    Last edited by laserlight; 04-24-2008 at 04:28 AM. Reason: Fixed typo error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    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. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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:
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by chottachatri View Post
    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. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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;
    	}
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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.
    I was talking about windows
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    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. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    fout is like cout, only 3 letters better, duh....

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    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. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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 <<
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM