2 questions - twice the fun!

This is a discussion on 2 questions - twice the fun! within the C++ Programming forums, part of the General Programming Boards category; I have two questions which relate to this rather simple program: Question 1. How do I read the first line ...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    2 questions - twice the fun!

    I have two questions which relate to this rather simple program:
    Question 1. How do I read the first line of a text (which the program creates) into the 'password' variable which is in the below source code?

    Question 2. With the code below, I have a problem. This program is meant to read the password which is inputted by the user and then outputted into the text file by the program (still need that bit) and when they try to get back into the program, they need to type in the same password. But no matter what type in, the pogram says that they typed in the wrong password.

    Confused? Here's the code:

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <fstream.h>
    
    int main()
    {
    	char attempt[40];
    	char password[40];
    	if(/* text file empty - but how is this checked? */) 
    	{
    		cout << "You have not set a password. You must set one now:\n";
    		cin >> password;
    		ofstream newPass("data.psw");
    		newPass << password;
    	}
    	
    	cout << "Enter your password:\n";
    	cin >> password;
    	
    	if(attempt==password) // <-- this password is meant to be read from the text file, but how?
    	{
    		cout << "Access granted . . .\n";
    		// ...program continues...
    	}
    	else
    	{
    		cout << "Sorry, you have inputted and incorrect password . . .\n";
    		// ...program exits
    	}
    	return 0;
    }
    Thanks for any help.

  2. #2
    Registered User cody's Avatar
    Join Date
    Sep 2001
    Posts
    86
    Hi,

    Answer 1:

    There are several ways to read text from a file. Since you are already using streams just have a look at the stream functions for input instead of output fread(), getc (), etc. etc. etc. would be alternatives...

    Answer 2:
    Code:
    if(attempt==password)
    You're comparing the memory adresses instead of the content.
    This will of course never be true Use strcmp() zu compare the strings.

    bye
    cody
    #include "reallife.h"

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    ok, could you please explain how to use the fread(), getc (), etc. functions to me? Could you also give me an example of how you would the the strcmp() (or whatever) function becuase I have never had to use it before...thanks.

  4. #4
    Registered User cody's Avatar
    Join Date
    Sep 2001
    Posts
    86
    *copy* ... *paste* ... *copy* ... *paste* ...

    enjoy


    strcmp,

    Compare strings.

    int strcmp( const char *string1, const char *string2 );

    Routine Required Header Compatibility
    strcmp <string.h> ANSI, Win 95, Win NT

    Return Value

    The return value for each of these functions indicates the lexicographic relation of string1 to string2.

    Value Relationship of string1 to string2
    < 0 string1 less than string2
    0 string1 identical to string2
    > 0 string1 greater than string2

    Remarks

    The strcmp function compares string1 and string2 lexicographically and returns a value indicating their relationship. wcscmp and _mbscmp are wide-character and multibyte-character versions of strcmp. The arguments and return value of wcscmp are wide-character strings; those of _mbscmp are multibyte-character strings. _mbscmp recognizes multibyte-character sequences according to the current multibyte code page and returns _NLSCMPERROR on an error. (For more information, see Code Pages.) These three functions behave identically otherwise.

    Example

    /* STRCMP.C */

    #include <string.h>
    #include <stdio.h>

    char string1[] = "The quick brown dog jumps over the lazy fox";
    char string2[] = "The QUICK brown dog jumps over the lazy fox";

    void main( void )
    {
    char tmp[20];
    int result;
    /* Case sensitive */
    printf( "Compare strings:\n\t%s\n\t%s\n\n", string1, string2 );
    result = strcmp( string1, string2 );
    if( result > 0 )
    strcpy( tmp, "greater than" );
    else if( result < 0 )
    strcpy( tmp, "less than" );
    else
    strcpy( tmp, "equal to" );
    printf( "\tstrcmp: String 1 is %s string 2\n", tmp );
    /* Case insensitive (could use equivalent _stricmp) */
    result = _stricmp( string1, string2 );
    if( result > 0 )
    strcpy( tmp, "greater than" );
    else if( result < 0 )
    strcpy( tmp, "less than" );
    else
    strcpy( tmp, "equal to" );
    printf( "\t_stricmp: String 1 is %s string 2\n", tmp );
    }

    ---------------------------------------------------------------------------------

    fread
    Reads data from a stream.

    size_t fread( void *buffer, size_t size, size_t count, FILE *stream );

    Function Required Header Compatibility
    fread <stdio.h> ANSI, Win 95, Win NT

    Return Value

    fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count. Use the feof or ferror function to distinguish a read error from an end-of-file condition. If size or count is 0, fread returns 0 and the buffer contents are unchanged.

    Parameters

    buffer

    Storage location for data

    size

    Item size in bytes

    count

    Maximum number of items to be read

    stream

    Pointer to FILE structure

    Remarks

    The fread function reads up to count items of size bytes from the input stream and stores them in buffer. The file pointer associated with stream (if there is one) is increased by the number of bytes actually read. If the given stream is opened in text mode, carriage return–linefeed pairs are replaced with single linefeed characters. The replacement has no effect on the file pointer or the return value. The file-pointer position is indeterminate if an error occurs. The value of a partially read item cannot be determined.

    Example

    /* FREAD.C: This program opens a file named FREAD.OUT and
    * writes 25 characters to the file. It then tries to open
    * FREAD.OUT and read in 25 characters. If the attempt succeeds,
    * the program displays the number of actual items read.
    */

    #include <stdio.h>

    void main( void )
    {
    FILE *stream;
    char list[30];
    int i, numread, numwritten;

    /* Open file in text mode: */
    if( (stream = fopen( "fread.out", "w+t" )) != NULL )
    {
    for ( i = 0; i < 25; i++ )
    list[i] = (char)('z' - i);
    /* Write 25 characters to stream */
    numwritten = fwrite( list, sizeof( char ), 25, stream );
    printf( "Wrote %d items\n", numwritten );
    fclose( stream );

    }
    else
    printf( "Problem opening the file\n" );

    if( (stream = fopen( "fread.out", "r+t" )) != NULL )
    {
    /* Attempt to read in 25 characters */
    numread = fread( list, sizeof( char ), 25, stream );
    printf( "Number of items read = %d\n", numread );
    printf( "Contents of buffer = %.25s\n", list );
    fclose( stream );
    }
    else
    printf( "File could not be opened\n" );
    }
    #include "reallife.h"

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    ARGH! You should be slapped! void main(void)??! That blinds me. Not only is this horrible C, but any self-respecting ANSI compliant C++ compiler will not allow you to compile this AT ALL! (Not VC++ *cough cough*) You have a long way to go my friend. Please stop teaching the newbies to use bad programming practices like invoking undefined behavior.....
    one fish two fish
    red fish blue fish

  6. #6
    Unregistered
    Guest
    i'm not exactly a newbie, but I just havn't ever had the need of using the strcmp function that's all...

  7. #7
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    oops, forgot to sign in when i sent the above message

  8. #8
    Registered User cody's Avatar
    Join Date
    Sep 2001
    Posts
    86

    LOL

    Hi Agrajag,

    this is not MY code but an example from the MSD help section

    > You have a long way to go my friend.
    Yeah. I guess you're right...I'll go back to my Hello World! application right now...

    > Please stop teaching the newbies to use bad programming
    > practices like invoking undefined behavior
    I'll continue to post some hints, but I'll check the code examples that aren't mine more intensive next time. I just copied and pasted it, because I had to do other things but wanted to provide some hints.

    Anyway, thank you very much for your nice comment

    With very very friendly greetings
    Your lifetime fan
    cody
    Last edited by cody; 12-22-2001 at 08:47 AM.
    #include "reallife.h"

  9. #9
    Unregistered
    Guest
    am i missing something...or...is he not setting attempt to anything? wouldn't that be the cause of some of your problems?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 06:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 08:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 08:22 AM

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