Thread: HELP! Reading from txt file and comparing to input

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    11

    Talking HELP! Reading from txt file and comparing to input

    Hey, possibly a bit of a noob question, i'm not quite sure...

    I have to create a sockets program that can identify clients and allow connection to the server based on their IP. I have created a text file that contains two "good" IP addresses:

    127.0.0.1
    127.0.0.2

    basically, i need to copy the IP of the potential client trying to connect (maybe hold it in a temp holder?), compare it to the values in the text file and allow or refuse the connection....however, as a new programmer I have pretty much no idea how to do this.

    The psuedocode as I understand it is as follows:
    <code>
    copy their_addr to temp holder(tempAdd)
    open "knownip.txt"
    while file != EOF
    if text line!= tempAdd
    go to next line
    if EOF -> refuse connection
    else if line==tempAdd
    connection allowed
    </code>

    this may not be accurate but it gives you the idea, any help on this would be very appreciated...

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Functions to look at:
    inet_ntoa()
    strcmp()
    fopen()
    fgets()
    fclose()

    The user's IP address will be stored in network byte order in the sin_addr member of the structure that you pass to accept().
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    11

    thanks

    ok thanks, i've done a quick google search for a few of those functions and they look promising, i'll give them a try and see what happens.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    11

    Question

    ok, its taken a while but i've got it to read the first line from a text file and compare it to the connecting ip address. thats fine. however, i'm having trouble finding a way to loop so that it can check subsequent lines in the file if the first address doesnt match...i'm certain this is possible but can't find anywhere that explains how

    also, don't know whether this is possible, but then again it most probably is... i need to have the last login date for each user stored in the file next to their address. this needs to be read and then strings sent to the user from another file that fall within certain criteria , i.e. anything since they last logged in.

    very complicated explanation i know, but i can't simplify it any more. maybe there is a more efficient way of doing this? otherwise there is a lot of i/o and date calculations to perform....

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This loops, reading one line of the file at a time, until the end of the file is reached.
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
      /* do stuff */
    }
    Extract whatever you want out of the buff (print it, store it, etc) and let the loop read the next line from the file.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    11
    thanks for the help, could you tell me if using puts will put the text into the temp holder (temp_addr) or if i have to specify something more, like put buff in temp_addr?

    Code:
    FILE * pFile;
    
    pFile = fopen ("clients.txt", "r");
        if (pfile == NULL)
               perror("Error opening file"):
        else {
        while (fgets(buff, BUFSIZ, fp) != NULL) {
               puts (temp_addr);
               strcmp(temp_addr, theiraddr);
               fclose (pFile);
    
        }
    if !=0 {
        printf("Connection refused")
    else printf ("connection accepted")

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    puts (temp_addr);
    Writes buff to stdout

    strcmp(temp_addr, theiraddr);
    compares two strings, then throws the answer away
    OK I suppose, but it doesn't really advance the program.

    fclose (pFile);
    Closes the file - are you sure you want to do this inside the loop - it's not much of a loop doing this here

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    11
    some schoolboy errors there, oops!

    fixed them now, apart from "stdout", could you tell me how that works please?

    (temp_addr is a field i have defined to hold the value read from the text file so i can compare it to their_addr)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. algorithm for duplicate file checking help
    By geekoftheweek in forum C Programming
    Replies: 1
    Last Post: 04-04-2009, 01:46 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  4. Comparing words in a file...
    By Zykaz in forum C Programming
    Replies: 4
    Last Post: 04-11-2004, 04:12 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM