C++ string

This is a discussion on C++ string within the C++ Programming forums, part of the General Programming Boards category; when i use c++ string i won't be able to use Code: scanf() USING SCANF: when i need to check ...

  1. #1
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    C++ string

    when i use c++ string i won't be able to use
    Code:
    scanf()
    USING SCANF:

    when i need to check the end of input file i do it using scanf by the following approach:
    Code:
    while(scanf("%s",&s1)!=EOF)
    
    {
    {
    but when i use c++ string how to check the end of input file

  2. #2
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    I think streams take care of that for you, but don't quote me. Take a look at this, fstream - C++ Reference

  3. #3
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    nope..what do i do if i need to check it explicitly..any suggestion/help??

  4. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Again, I want to stress I have little experience in either C/C++, but try
    Code:
    fstream file="C:/Path";
    while (file.get()!=-1);
    The "-1" is just the value of EOF, I don't know if it's defined by std.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,238
    Quote Originally Posted by User Name: View Post
    The "-1" is just the value of EOF, I don't know if it's defined by std.
    EOF is defined, and should be used instead of a literal -1. EOF is not guaranteed to be equal to -1.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    I got -1 from stdio(I do know the difference), I've yet to browse iostream. The stdio I read shipped with MSVC, are the defines and other stuff standardized?

  7. #7
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    92
    Quote Originally Posted by User Name:
    I got -1 from stdio(I do know the difference), I've yet to browse iostream. The stdio I read shipped with MSVC, are the defines and other stuff standardized?
    MSVC may use -1 for EOF, some other compiler may use 32767 for EOF, some other compiler may use something else for EOF, but if they comply with the standard they will define EOF. Most of what is in those headers will be standard stuff, but Microsoft likes to add their own extensions so you may find various things that are not. Other compilers may do the same thing as well. For determining which is which, google is your friend.

    Even if a define isn't a standard, it is almost always better to use a #define'd (or better yet const in C++) value rather than a magic number.
    C+/- programmer extraordinaire

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Quote Originally Posted by jdragyn
    MSVC may use -1 for EOF, some other compiler may use 32767 for EOF, some other compiler may use something else for EOF, but if they comply with the standard they will define EOF.
    That said, if a standard library implementation defines EOF as 32767, then it does not comply with the standard since EOF must be negative.
    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

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    To the OP. You can check End-Of-File with streams with
    Code:
    istream s;
    while (!s.eof()) ...  //so while(!cin.eof()) ... for std input
    You can also use .c_str to make a std::string a normal null terminated C-style string.

    getline() also uses the approach i stated in the beginning.

    Other approaches like
    Code:
    string s;
    while(!getline(cin, s)) ...
    will also work as well

    There are a lot of way to read input. You should search it a bit more to see how to check for EOF for all ways. Scanf() is surely not the only function that works and you should try a C++ way to do so.

    In any case, this is not a std::string thing. It is about reading input and checking for EOF...

  10. #10
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    i am seeing things here or am i the only one that noticed a giant chunk of random code that has been lumped into this thread?

  11. #11
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Nope rogster001, you're not seeing things. And I think we're just going to keep ignoring the random code

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by rags_to_riches View Post
    Nope rogster001, you're not seeing things. And I think we're just going to keep ignoring the random code
    Indeed. We don't like things being shoved in our faces.
    If that person wants us to look at that code then they'll have to start their own thread, ask some actual questions, and actually pay attention to any and every response they get. After reading/re-reading the rules/guidelines of this site of course.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    SourceForge.net: Fflush - cpwiki

    Also, since each tri_matrix is rather large, you may not want to pass them by value (e.g., to multi_matrix).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slicing problem with inherited classes.
    By Swerve in forum C++ Programming
    Replies: 6
    Last Post: 10-20-2009, 02:29 PM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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