Hey guyz.. I want to know how to print certain line from the text file and certain part of the record for example

data.txt...

123   John Smith    80
222   chris brown   50
325   christine     60

I only want to print out the name from the record in the text file..
for example when the user enter 222 for the id it should print out the id and the name .... 222 chris brown


anyone please help ....thx

Since the name may or may not contain spaces you will have to
1) read the entire line into a character array. You can use fgets() to do that.

2) use a char pointer to advance from the beginning of the string to the beginning of the name. Copy all remaining characters into another character buffer.

3) in the new buffer from 2) above locate the end of the string, back up to the first space, then back up some more until there are no more characters. That will locate the end of the name. At that spot add a NULL character -- '\0'.

char iobuf[] = "222   chris brown   50";
char tbuf[40] = {0};
char* ptr;
// find the location of the first character in the name
//
// find the first space
ptr = iobuf;
while(*ptr && !isspace(*ptr) )
   ptr++;
// now find the first non-space
while(*ptr && isspace(*ptr))
   ptr++;
// ok, we're at the first letter of the name
// so copy remainder to new buffer
strcpy(tbuf,ptr);
// now locate end of string
ptr = tbuf + strlen(tbuf) - 1;

// back up to first space
while(!isspace(*ptr) )
   ptr--;
// back up to first non-space
while(isspace(*ptr) )
   ptr--;
// we're now at the end of the name.  So truncate the string
*(ptr+1) = 0;

// print the name
printf("%s\n", tbuf);

Another method

char iobuf[] = "222   chris brown   50";
char* ptr;
// find beginning of name
while( !isalpha(*ptr))
   ptr++;
memmove(iobuf,ptr,strlen(ptr)+1);
// find end of name
ptr = iobuf + strlen(iobuf)-1;
while(!isalpha(*ptr))
    ptr--;
// truncate
*(ptr+1) = 0;

Another method

char iobuf[] = "222   chris brown   50";
char* ptr;
// find beginning of name
while( !isalpha(*ptr))
   ptr++;
memmove(iobuf,ptr,strlen(ptr)+1);
// find end of name
ptr = iobuf + strlen(iobuf)-1;
while(!isalpha(*ptr))
    ptr--;
// truncate
*(ptr+1) = 0;

That obtains the string name out of the buffer, but I believe that what the OP is asking leads to hash table lookup.

[...]
for example when the user enter 222 for the id it should print out the id and the name .... 222 chris brown

Most likely it's not going to help you, but perhaps you get the idea of the scope of it. Here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.