Need Help

This is a discussion on Need Help within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by xbusterx So I opened it and closed it? Why did it close? What do you think Code: ...

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by xbusterx View Post
    So I opened it and closed it?

    Why did it close?
    What do you think
    Code:
     infile.close();
    does?

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Opening a file with an fstream in your program doesn't open it up on your screen. You're confused about what the term "open" means in this context.

    If you want to open a file in another program (for example, open a text file in Notepad), then there are ways of doing that described in the FAQ. But that's not what you do with an fstream.

  3. #18
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by Daved View Post
    Opening a file with an fstream in your program doesn't open it up on your screen. You're confused about what the term "open" means in this context.

    If you want to open a file in another program (for example, open a text file in Notepad), then there are ways of doing that described in the FAQ. But that's not what you do with an fstream.
    By open I mean I want to display what is in the file on the terminal screen. That's what I mean, but's not working.

  4. #19
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    then you should maybe try to cout the file contents?
    Currently research OpenGL

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you want to display the contents of the file in the console window, you have to write code to output it to the console window. Opening a file in your program just means that the stream has the file ready for you to read from. You can use any of the many function available for reading from streams.

    The one you probably want is getline. Try looking up the getline function and figure out how to read from a stream. Then try outputting the line you read. Once that works, you'll want to use a loop to read each line and output them all to the screen.

    There are other methods, but I imagine you are trying to learn how to program and those other methods won't help you much in that regard.

  6. #21
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Oh brother. Doesn't anyone actually study anymore?
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #22
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by Daved View Post
    If you want to display the contents of the file in the console window, you have to write code to output it to the console window. Opening a file in your program just means that the stream has the file ready for you to read from. You can use any of the many function available for reading from streams.

    The one you probably want is getline. Try looking up the getline function and figure out how to read from a stream. Then try outputting the line you read. Once that works, you'll want to use a loop to read each line and output them all to the screen.

    There are other methods, but I imagine you are trying to learn how to program and those other methods won't help you much in that regard.
    That's exactly what I've been trying to do, but how do I read it from the stream I tried, but it didn't work!

  8. #23
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Ever heard of a book? In ancient times past we used these to learn things. Incredible, but true.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  9. #24
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by Sebastiani View Post
    Ever heard of a book? In ancient times past we used these to learn things. Incredible, but true.
    Well I've read the notes in class and I don't have the book for the class since it's optional it was over $200 and I couldn't afford it sorry some people aren't rich.

  10. #25
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,148
    What book was this then?

    Soma

  11. #26
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    There are a lot of resources out there. My first C programming book was found at a second-hand book store for less than $10. Then there are public libraries and even online tutorials. The point is, if you really want to program you are going to have to work very hard to get good at it, and that means lots of study, research, and practice. Good luck.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  12. #27
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Sebastiani I got to ask. the code in your signature. whats it do?
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  13. #28
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    .....
    Last edited by xbusterx; 11-17-2008 at 09:04 PM.

  14. #29
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    I learned everything I need to know about C++ from tutorials on the web, this message board, and Stroustrup's book.
    Think of it this way:
    Code:
    std::ifstream reader; //can read stuff
    reader.open("File.file");
    if(!reader) { /*error*/ }
    
    //NOW. What shall we do with this file? Let's READ data from the
    //beginning, until the first "newline" character.
    
    std::string buffer;
    std::getline(reader,buffer,'\n'); // '\n' is default, actually
    
    std::cout << "The first line goes like this:\n" << buffer << std::endl;
    
    //Now the stream is poised at the second line, if only you read it..... etc...
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  15. #30
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> but how do I read it from the stream I tried, but it didn't work!

    Show your attempt and describe how it doesn't work. If there are compiler errors, post them. If it compiles but doesn't do what you expect, explain what you expect and what it does.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

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