Hello friends,

Could someone tell me if i want to read a particular text from a file that exist in each line of file. How could i do this?

My text is like this:
poll mail.telesys.com protocol POP3 user vipin@gmail.com password abcd mda "cat > /export/home/m7istp/imcp/bin/ml201.txt

now i want to read text after /bin/ml and before .txt in each line how can i do this..

To get a filename without path or extension you need to do some string manipulations.

Here are three procs I use all the time (well, in C++... I haven't used C in a while):

#include <stdlib.h>
#include <string.h>

/* ExtractFilePath()
 *   Returns a new string containing the directory information in the
 *   path, including the terminating path separator.
 *   Don't forget to free() the result when you are done with it!
 * arguments
 *   path       The path to dissect, which may or may not contain
 *              directory information.
 *   separator  The path separator char to use if not '/'. You can specify
 *              0 for the default.
 * returns
 *   A newly malloc()ed string, or NULL if a new string could not be
 *   allocated or path is NULL.
 */
char* ExtractFilePath( const char* path, char separator )
  {
  char* p;
  char* result = NULL;
  if (!path) return NULL;
  if (!separator) separator = '/';

  p = strrchr( path, separator );
  if (p == NULL) p = (char*)path;
  else           p++;

  result = (char*)calloc( p -path +1, 1 );
  if (result) strncpy( result, path, (const char*)p -path );

  return result;
  }

/* ExtractFileName()
 *   Takes a complete path and returns a new string containing the
 *   filename part.
 *   Don't forget to free() the result when you are done with it!
 * arguments
 *   path       The path to dissect, which may or may not contain
 *              directory information.
 *   separator  The path separator char to use if not '/'. You can specify
 *              0 for the default.
 * returns
 *   A newly malloc()ed string, or NULL if a new string could not be
 *   allocated or path is NULL.
 */
char* ExtractFileName( const char* path, char separator )
  {
  char* p;
  char* result = NULL;
  if (!path) return NULL;
  if (!separator) separator = '/';

  p = strrchr( path, separator );
  if (p == NULL) p = (char*)path;
  else           p++;

  result = (char*)malloc( strlen( p ) +1 );
  if (result) strcpy( result, p );

  return result;
  }

/* ChangeFileExtension()
 *   Modifies the filename extension. The period (.) is considered part
 *   of the extension.
 *   Don't forget to free() the result when you are done with it!
 * arguments
 *   path       The path to modify
 *   separator  The path separator char to use if not '/'. You can specify
 *              0 for the default.
 *   extension  The new extension. May be NULL.
 * returns
 *   A newly malloc()ed string, or NULL if a new string could not be
 *   allocated or path is NULL.
 * examples
 *   // Get the filename without the extension
 *   filename1 = ChangeFileExtension( filename, "" );
 *   // Add the extension .doc
 *   filename2 = ChangeFileExtension( filename1, ".doc" );
 *   // Oops, I meant .txt
 *   filename3 = ChangeFileExtension( filename2, ".txt" );
 */
char* ChangeFileExtension(
  const char* path,
  char        separator,
  const char* extension
  ) {
  char* psep;
  char* pext;
  char* result = NULL;
  if (!path) return NULL;
  if (!separator) separator = '/';

  psep = strrchr( path, separator );
  if (psep == NULL) psep = (char*)path;
  else              psep++;

  pext = strrchr( path, '.' );
  if ((pext == NULL) || (pext < psep)) pext = (char*)path +strlen( path );

  result = (char*)calloc(
             (const char*)pext -path
               +(extension ? strlen( extension ) : 0)
               +1,
             1
             );
  if (result)
    {
    strncpy( result, path, (const char*)pext -path );
    if (extension) strcat( result, extension );
    }

  return result;
  }

You can use them either directly:

int main()
  {
  char* filename = "/export/home/m7istp/imcp/bin/ml201.txt";
  char* t;
  char* basename;
  t        = ExtractFileName( filename, 0 );
  basename = ChangeFileExtension( t, 0, NULL );
  free( t );
  printf( "The base name of \"%s\" is \"%s\".\n", filename, basename );
  free( basename );
  return 0;
  }

or as templates to write your own function.

Hope this helps.

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.