Thread: Problem reading from external file

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    9

    Question Problem reading from external file

    Hey im totally new to the boards and c programming and ive been trying to make a program in which the user inputs a username and password

    now when they try to login again im having trouble getting the program to read the username(external filename) and the password which is contained inside it.

    here is my code...its all messed up cuz i was trying different things. If someone could please tell me how to get it so that it reads the external file("username") and opens it and compares the password inputted by the user and the password contained inside the external file("username") that would be great. Thanks

    Code:
    void existing()
    {
    int openFiles('r');
    int readDataFromFile();
        //Opens the file from the directory and reads the password
        openFiles('r');
        {
            username = fopen("%s", username, "r");
        } 
        readDataFromFile();
        {
             fgets("%s", username);
        }
            
            extern char username;
        char *fp;
        
        //If statement that confirms the correct username and password
        if (strcmp(user, username) == 0 && strcmp(pass, password) ==0)
        printf("\nAccess Granted\nYou may enter the Matrix\n");
        else
        printf("\n%s is incorrect\nProgram will now terminate.\n\n",password);
             
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Let me show you what your code is actually doing:
    Code:
    void existing()
    {
        int openFiles('r');
        int readDataFromFile();
    
        //Opens the file from the directory and reads the password
        openFiles('r');
        username = fopen("%s", username, "r");
        readDataFromFile();
        fgets("%s", username);
            
        /* this only works in C99 */
        extern char username;
        char *fp;
        
        //If statement that confirms the correct username and password
        if (strcmp(user, username) == 0 && strcmp(pass, password) ==0)
            printf("\nAccess Granted\nYou may enter the Matrix\n");
        else
            printf("\n%s is incorrect\nProgram will now terminate.\n\n",password);
             
    }
    Is that what you intended? None of those braces actually did anything in your program. If they were supposed to do something, then it appears as if you're trying to create nested functions. Furthermore, your code is horrible to read and try and fix, because of your insistance on all of your variables being globals.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    You're also using the wrong syntax for fgets and fopen. I don't see any declaration for password and pass. Also, what is the point of the declaration char *fp, if you're not going to use it.

    What are your openFiles and readDataFromFile functions doing?

    See your man pages for the proper way to fgets and fopen.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    Its horrible to read cuz half of that stuff was me just trying different things
    username and password are my only variables.
    username is what is inputted by the user and the username is stored as the external filename. Now the password is written to that file. I have all that accomplished but im having a hard time getting it to open the username and match the password inputted by the user and the one that is contained in the file. How do i go about opening the username file and is my if statement enough to compare whats inputted and whats in the file?

    Code:
    void existing()
    {
           username = fopen("%s", username, "r");
           fgets("%s", username);
       
        
        //If statement that confirms the correct username and password
        if (strcmp(user, username) == 0 && strcmp(pass, password) ==0)
        printf("\nAccess Granted\nYou may enter the Matrix\n");
        else
        printf("\n%s is incorrect\nProgram will now terminate.\n\n",password);
             
    }

  5. #5
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    Code:
    /* proper use for fopen */
    FILE *fp; /* file descriptor */
    if((fp = fopen("passwordfile.txt", "r")) == NULL) /* open for path for reading, check for errors */
    	perror("passwordfile.txt"), exit(1); /* if error, print the error received and exit*/
    		
    /* proper use for fgets */
    char username[20]; /* declare username as an array of 20 characters */
    fgets(username, 20, stdin); /* recieve input from the stdin stream */
    username[strlen(username) - 1] = '\0'; /* cut off the '\n' that fgets receives */

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    k well ive tried that before..and im trying it again all im getting is an error. I just dont get what to put and where and why most importantly
    im trying this...

    Code:
    void existing()
    {
        //Opens the file from the directory and reads the password
        FILE *fp;
        if((fp = fopen("&s" ,username, "r")) == NULL)
    	perror("%s" ,username);
        
        //If statement that confirms the correct username and password
        if (strcmp(user, username) == 0 && strcmp(pass, password) ==0)
        printf("\nAccess Granted\nYou may enter the Matrix\n");
        else
        printf("\n%s is incorrect\nProgram will now terminate.\n\n",password);
             
    }
    what i see is that fp=fopen opens the file "username" with the "r" command....i dont see where its reading the file? and i dont see why this doesnt work

  7. #7
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    Code:
    if((fp = fopen("&s" ,username, "r")) == NULL)
    	perror("%s" ,username);
    fopen() only opens the file for reading, it doesn't do any reading itself. The arguments for fopen "&s", username will not do anything but give an error. The syntax for fopen is:
    FILE *fopen(const char * restrict path, const char * restrict mode);
    fp = fopen("file.txt", "r"); will open file.txt for reading.

    perror is not like printf, it's syntax is:
    void perror(const char *string);
    perror(""); will print the error without any text before it. perror("file.txt") will print file.txt: [error here]. perror() does NOT accept arguments like printf and such does.

    There are many ways to read from a file once you have open, I suggest you search the forums or google for how to do that. If you have problems, post your problem and code.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    fp = fopen("file.txt", "r"); will open file.txt for reading.

    well for this i see that is does that
    but as there is user input i cannot put in file.txt its a variable thats why i use %s.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    k so far you guys have been of no help

    please help i need this finished by tonight

    ALSO
    how do i clear the screen after a function has completed?
    what library is needed and whats the command?

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    FAQ > How do I... (Level 1) > Clear the screen?

    >k so far you guys have been of no help
    But you already knew to check the FAQ because you read the Announcement.
    Last edited by Dave_Sinkula; 03-23-2005 at 05:15 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)

    that was of no use to me
    however i did get the clrscr to work thank you

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well then you're stupid. It tells you how to get a line of text. You then take that line of text, and use it as your file name once you've removed the newline. Seriously, you obviously have no idea how to read anything from the user correctly, because you're doing stuff like this:
    Code:
    if((fp = fopen("&s" ,username, "r")) == NULL)
    	perror("%s" ,username);
    That's supposed to do what exactly? You were given the FAQ link for a reason. It's apparent to everyone here who does know how to correctly read information from the user, that you do not.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User coolshyam's Avatar
    Join Date
    Mar 2005
    Posts
    26

    Red face An Alternate Method

    create a file named user.txt in temp directory
    i have given you an example in which only the username is taken into account
    remaing work is your effort
    Code:
    int main()
    {
    	FILE *fp;
    	char shyam[10],usr[10];
    	fp=fopen("c:\\winnt\\temp\\user.txt","r");
    	fgets(usr,10,fp);
    	scanf("%s",shyam);
    	if(strcmp(shyam,usr)==0)
    	{
    		printf("correct")
    	}
    	return 0;
    }

    any questions any type in programming

    a ready made answer
    -----------------------------------------------------------------------------------
    FORTUNE FAVOURS THE BOLD!
    -----------------------------------------------------------------------------------

  14. #14
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    Quote Originally Posted by coolshyam
    Code:
    int main()
    {
    	FILE *fp;
    	char shyam[10],usr[10];
    	fp=fopen("c:\\winnt\\temp\\user.txt","r");
    	fgets(usr,10,fp);
            if(usr[strlen(usr) - 1] == '\n')
                     usr[strlen(usr) - 1] = '\0';
    	scanf("%s",shyam);
    	if(strcmp(shyam,usr)==0)
    	{
    		printf("correct")
    	}
    	return 0;
    }
    You forgot to remove the '\n' from fgets(). Some people will also say scanf() is bad to use, so I suggest to use fgets() again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 06:52 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM