Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search name in file

Status
Not open for further replies.

Noizz

Technical User
Jan 25, 2004
27
0
0
BE
Hi

The file is called: user.txt and has the following data in it:

bart|blabla|0|0
jeroen|gvrs|0|0
ann|cls|0|0
peter|clssns|0|0

I want users to be able to add a username & pass to this file, but how do I search in this file for existing names?

So: if somebody types: "bart", C should return an error

I just need the function to search for this name.

Thx in advance
Noizz
 
How do you put this textfile in an array so we can easily find names and adjust the data in the textfile?

greetz
 
See fopen, fgets/fread, fclose (plus fseek/ftell, malloc;).
It depends on what do you want to do with search result...
 
Wouldn't use an array, but if I had to..

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>




char **aResize (int, char **);
void prArray (char **, int);

int
main (void)
{
  int i = 0;
  char **arr;

  while (i < 10)
    {
      if ((arr = aResize ((i * 10), arr)) != NULL)
	{
	  prArray (arr, (i * 10));
	}
      ++i;
    }
  return 0;
}

char **
aResize (int nm, char **aa)
{
  int p;
  if (aa == NULL)
    {
      aa = malloc (nm * sizeof (char *));
      if (aa)
	{
	  for (p = ((nm - 10) >= 0 ? (nm - 10) : 0); p < nm; p++)
	    {
	      aa[p] = malloc (10 * sizeof (char));
	      if (!aa[p])
		{
		  perror ("malloc()");
		  exit (1);
		}
	      strcpy (aa[p], "!!foo!!");
	    }
	  return aa;
	}
      return NULL;
    }
  else
    {
      /*existing array */
      aa = realloc (aa, nm * sizeof (char *));
      if (aa)
	{
	  for (p = ((nm - 10) >= 0 ? (nm - 10) : 0); p < nm; p++)
	    {
	      aa[p] = malloc (10 * sizeof (char));
	      if (!aa[p])
		{
		  perror ("malloc()");
		  exit (1);
		}
	      strcpy (aa[p], "!!foo!!");
	    }
	  return aa;
	}
      return NULL;
    }
}

void
prArray (char **foo, int ne)
{
  int y = 0;

  printf ("Head pointer at: %p\n", foo);
  while (y < ne)
    {
      printf ("Pointer: %d at: %p = %s\n", y, &foo[y], foo[y]);
      y++;
    }
}
HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top