Comparing String to Filecontents?

This is a discussion on Comparing String to Filecontents? within the C++ Programming forums, part of the General Programming Boards category; I'm very new to C/C++, and I'm currently using the C-Styled strings. I'm trying to compare it to the contents ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    New York, New York
    Posts
    19

    Comparing String to Filecontents?

    I'm very new to C/C++, and I'm currently using the C-Styled strings. I'm trying to compare it to the contents of a file, just so I understand how to do it in the future. What kind of a command would I use to compare?

    Code:
    using namespace std;
    
    int main()
    {
      string user;
      cout<< "Enter your desired username: ";
      cin.getline( user, , '\n' );
      ofstream a_file ( "files.txt" );
      a_file<< user;
      a_file.close();
      ifstream b_file ( "files.txt" );
      char user2[50];
      if ( strcmp ( user, user2 ) == 0 )
           cout<< "File appending successful!\n";
      else
          cout<< "Internal Error\n";
      cin.get();
    }
    I know the code is VERY bad, but basically what I'm trying to do is compare char user[50] to the contents of files.txt which I was trying to store in char user2[50].

    Thanks ahead of time, sorry about the stupid question I'm very new.

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    Use ifstream.

    [edit]

    And for God's sake don't give people that anoying errors.
    Last edited by siavoshkc; 09-30-2006 at 05:04 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    New York, New York
    Posts
    19
    It wasn't actually meant to be a program, I'm only messing around with functions; I haven't even been doing C for a week yet.

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    All you need is in my signature.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    > I'm very new to C/C++
    Pick one - C or C++ and learn to use it well.
    Then if you feel like it, learn the other one.

    C/C++ is a train-wreck waiting to happen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    If you want to get into game programming later, ditch C and learn C++. C++ syntax is a little easier to understand than C, but there is no harm in learning C, then moving onto C++ as you would have some ground knowledge in the function of it.

    As Salem said, do not try to learn both languages at the same time, you will get confused. C++ keeps you away from some of the bad programming habits that C teaches you.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Location
    New York, New York
    Posts
    19
    Hm, well I want to go to college for software development but I thought I'd get a start ahead of time by learning some C++ in my spare time. I guess if I had to choose between the two, I'd choose C++. I already have the book 'C++ for You++' being delivered to my house, so I guess I already made my choice.

    But what I'm learning right now is whatever is in the tutorial on this site, and what they teach you is how to use C Strings (http://www.cprogramming.com/tutorial/lesson9.html). So I guess I just got a little confused between the two and I'll just wait for my book to arrive to learn any more.

  8. #8
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Well yes, the C string tutorial is about C strings.
    There's also a C++ string tutorial you can use though, linked to in the C string tutorial, which I suspect you will find much easier to use.

    http://www.cprogramming.com/tutorial/string.html

    And of course, for appending, you don't need to read in the entire file, append in memory, then output it all again, you can just specify append mode when opening, then just out_file << "stuff" to append it. Much simpler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 10:20 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  5. Comparing string contents
    By Slavakion in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2004, 12:14 PM

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