Thread: Agh. Yet another newbie trying to read from a TXT.

  1. #1
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605

    Agh. Yet another newbie trying to read from a TXT.

    Alright, I hate to ask this, because I know its been asked hundreds of times before, but I could not come up with anything really useful in a search. I'm trying to read a string from a text file, but alas, no luck. Here's what i've got:

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <stdlib>
    
    using namespace std;
    
    int main()
    {
    ifstream(liscense);
    liscense.open("regfile.txt");
    while(!liscense.eof())
    {
     getline(liscense,line);
    }
    cout<<liscense<<endl;
    system("pause");
    
    return 0;
    }
    I'm getting nothing for output, and the file does contain data. Any suggestions?

    Thank you in advance.
    .

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Quote Originally Posted by civix View Post
    Alright, I hate to ask this, because I know its been asked hundreds of times before, but I could not come up with anything really useful in a search. I'm trying to read a string from a text file, but alas, no luck. Here's what i've got:

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <stdlib>
    
    using namespace std;
    
    int main()
    {
    ifstream(liscense);
    liscense.open("regfile.txt");
    while(!liscense.eof())
    {
     getline(liscense,line);
    }
    cout<<liscense<<endl;
    system("pause");
    
    return 0;
    }
    I'm getting nothing for output, and the file does contain data. Any suggestions?

    Thank you in advance.
    As a newbie myself, I would check and make sure that your file path is correct. I've used the wrong file path a million times. For example is your text file on ("F:\\regfile.txt")?

    Try adding this to the beginning of your program to see if it is reading the file

    Code:
    if ( !liscense)
    {
    	cout<<"**Can't open input file**"<<endl;
    	return 1;
    }
    Last edited by dispatch4599; 03-02-2008 at 10:35 PM.

  3. #3
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    File path is good. I've already checked it like five times. Haha.

    EDIT: Turns out that the file was set to read-only.

    Now its returning what looks like a memory address. wtf?

    EDITx2: Alright, I had a brain fart.

    It wasnt seeing the file because I was executing it right out of Dev-C++ it had nothing to do with being read-only.

    It was showing me a memory address because I was trying to output the filestream.

    Problem solved, thanks.
    Last edited by civix; 03-02-2008 at 10:48 PM.
    .

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Sorry, I'm not more knowledgeable on this stuff like the end of file stuff. Right now I don't use that and just insert the file name to read just from the file.

    Code:
    while (liscense)
    {
     getline(liscense,line);
    }
    cout<<liscense<<endl;
    system("pause");
    
    return 0;
    }

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    while (getline(liscense,line))
    {
       cout<<line<<endl; 
    }
    Something like this maybe?
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And unless liscense means something else, perhaps spell it correctly "license"?

    --
    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.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include <cstdlib>

  8. #8
    coder
    Join Date
    Feb 2008
    Posts
    127
    finally, this is your code + indentation:
    doesn't it look more readable? (for you and for us)
    Code:
    int main()
    {
    	ifstream(liscense);
    	liscense.open("regfile.txt");
    	
    	while(!liscense.eof())
    	{
    		getline(liscense,line);
    	}
    	cout<<liscense<<endl;
    	system("pause");
    
    	return 0;
    }

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by carlorfeo View Post
    finally, this is your code + indentation:
    doesn't it look more readable? (for you and for us)
    Code:
    int main()
    {
    	ifstream(liscense);
    	liscense.open("regfile.txt");
    	
    	while(!liscense.eof())
    	{
    		getline(liscense,line);
    	}
    	cout<<liscense<<endl;
    	system("pause");
    
    	return 0;
    }
    No it does not - still checks the eof as a loop control, and tries to cout the stream
    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
    coder
    Join Date
    Feb 2008
    Posts
    127
    vart...
    it's a demonstration of his code indented. I didn't care about the code itself

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Try this.

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    
    using namespace std;
    
    int main()
    {
    ifstream(liscense);
    liscense.open("regfile.txt");
    string line;
    while(!liscense.eof())
    {
     getline(liscense,line);
     cout<<line<<endl;
    }
    
    return 0;
    }

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try this.
    It looks like you have not tried it yourself as it has an obvious error.

    Also, kindly indent your code, and do not use the eof() member function of istream to control a loop.
    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

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Quote Originally Posted by laserlight View Post
    It looks like you have not tried it yourself as it has an obvious error.

    Also, kindly indent your code, and do not use the eof() member function of istream to control a loop.
    Why don't you try this?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why don't you try this?
    Okay, I admit: I learned something about C++ syntax today.

    That still does not excuse your use of eof() to control the loop, and the lack of indentation.

    EDIT:
    For an explanation, see the FAQ: Why it's bad to use feof() to control a loop. The same logic can be applied to eof().
    Last edited by laserlight; 03-04-2008 at 12:41 PM.
    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
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Quote Originally Posted by laserlight View Post
    Okay, I admit: I learned something about C++ syntax today.

    That still does not excuse your use of eof() to control the loop, and the lack of indentation.

    EDIT:
    For an explanation, see the FAQ: Why it's bad to use feof() to control a loop. The same logic can be applied to eof().
    Sorry about my laziness. I just copied and pasted the original code and tried to make it work. I agree with you about the indent and loop control.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read Causes Freezing?
    By Masna in forum C Programming
    Replies: 5
    Last Post: 07-18-2008, 04:31 PM
  2. Read a txt file
    By swgh in forum C++ Programming
    Replies: 10
    Last Post: 12-15-2006, 07:59 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Read a txt and find data
    By BianConiglio in forum C Programming
    Replies: 8
    Last Post: 04-25-2004, 03:14 AM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM