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

This is a discussion on Agh. Yet another newbie trying to read from a TXT. within the C++ Programming forums, part of the General Programming Boards category; Alright, I hate to ask this, because I know its been asked hundreds of times before, but I could not ...

  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 09: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 09: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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Code:
    while (getline(liscense,line))
    {
       cout<<line<<endl; 
    }
    Something like this maybe?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    21,448
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,448
    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 11:41 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 06: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21