473,499 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search txt file and put name in a ...

i try to search txt file in a folder

my code

Expand|Select|Wrap|Line Numbers
  1. int searchfile(char *dir, char *filename, int flength, char *ext)
  2. {
  3. DIR *pdir;
  4. pdir = opendir(dir);
  5. struct dirent *pent;
  6. if (!pdir)
  7. {
  8. printf ("%s - Incapable d'utiliser opendir()\n", strerror
  9. (errno));
  10. return 1;
  11. }
  12.  
  13. while((pent = readdir(pdir)) != NULL)
  14. {
  15. if(strcmp(pent->d_name + flength, ext) == 0)
  16. {
  17. printf("%s\n", pent->d_name);
  18. }
  19. }
  20. closedir(pdir);
  21. return 0;
  22. }
  23.  
i call this function with
Expand|Select|Wrap|Line Numbers
  1. searchfile(local_dir_led,"", 28, ".txt");
  2.  

all my file name are the same size...
they are surely a better way to do it (the file name size are not the
same)...

i would like to put the file name in a list....
what is the better way to do it?

Nov 14 '05 #1
2 1562

collinm wrote:
i try to search txt file in a folder

my code

Expand|Select|Wrap|Line Numbers
  1. int searchfile(char *dir, char *filename, int flength, char
  • *ext)
  •  {
  •      DIR *pdir;
  •      pdir = opendir(dir);
  •      struct dirent *pent;
  •      if (!pdir)
  •      {
  •          printf ("%s - Incapable d'utiliser opendir()\n", strerror
  •  (errno));
  •          return 1;
  •      }
  •      while((pent = readdir(pdir)) != NULL)
  •      {
  •          if(strcmp(pent->d_name + flength, ext) == 0)
  •          {
  •              printf("%s\n", pent->d_name);
  •          }
  •      }
  •      closedir(pdir);
  •      return 0;
  •  }
  •  
  •  
  • Lots of this stuff is off-topic, but your question may not be. For any
  • actual questions involving the above, you'll need comp.unix.programmer.
  •  

  • i call this function with
    Expand|Select|Wrap|Line Numbers
    1.  searchfile(local_dir_led,"", 28, ".txt");
    2.  

    That doesn't sound good. Magic numbers are usually very poor practice.

    all my file name are the same size...

    they are surely a better way to do it (the file name size are not the
    same)...
    Sure.

    const char *extension = ".txt";
    size_t len;
    len = strlen (filename) - strlen (extension);

    That gives you the length of the base filename. I wouldn't even bother
    passing that into the function, just have searchfile compute it.

    i would like to put the file name in a list....
    what is the better way to do it?

    What kind of list? Linked list? Expandable array? Without knowing what
    you intend to do with them it's hard to advise.

    Brian

    Nov 14 '05 #2
    collinm wrote:

    my code

    [code]int searchfile(char *dir, char *filename, int flength, char *ext)
    syntax error [code]
    {
    DIR *pdir;
    undefined DIR
    pdir = opendir(dir);
    undefined opendir
    struct dirent *pent;
    undefined, struct dirent. Also illegal here after executable code.
    if (!pdir)
    {
    printf ("%s - Incapable d'utiliser opendir()\n", strerror(errno));
    undefine printf. This is standard in stdio.h.
    return 1;
    }

    while((pent = readdir(pdir)) != NULL)
    undefined readdir and NULL. NULL is also defined in stdio.h
    {
    if(strcmp(pent->d_name + flength, ext) == 0)
    undefined strcmp. Standard in string.h
    undefined d_name
    {
    printf("%s\n", pent->d_name);
    Undefined printf. See above.
    Undefined d_name
    }
    }
    closedir(pdir);
    undefined closedir
    return 0;
    }


    All the errors I could find. Probably more. Can't be compiled.

    --
    Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    Nov 14 '05 #3

    This thread has been closed and replies have been disabled. Please start a new discussion.

    Similar topics

    1
    8698
    by: Les Juby | last post by:
    A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
    2
    4577
    by: richardkreidl | last post by:
    I want to be able to delete and search for elements in a XML file, I'm using the code below for adding elements which works great: Public Sub cmdAddElement_Click(ByVal sender As System.Object,...
    1
    2708
    by: Eric | last post by:
    Hi: I have two files. I search pattern ":" from emails text file and save email contents into a database. Another search pattern " field is blank. Please try again.", vbExclamation + vbOKOnly...
    3
    9524
    by: Chung Leong | last post by:
    Here's the rest of the tutorial I started earlier: Aside from text within a document, Indexing Service let you search on meta information stored in the files. For example, MusicArtist and...
    13
    1872
    by: Ray Muforosky | last post by:
    Hello all: Task: I want to do file search, using the "conatining text" option from a web page. How do I search for a file on my local drive containing a certain string, from a web page. That...
    10
    2219
    by: B. Williams | last post by:
    I have an assignment that requires me to write a program that uses a class, a constructor, a switch, and store the records in a text file. The second requirement is to create a function called...
    8
    1844
    by: stunna | last post by:
    Hi there, i want to implement a a system to search for a file in a folder using the file name and if its there, i want it to be displayed. I have one through a couple of books on how to implement...
    1
    7510
    Merlin1857
    by: Merlin1857 | last post by:
    How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
    0
    2707
    by: JamesOo | last post by:
    I have the code below, but I need to make it searchable in query table, below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only,...
    0
    10705
    Debadatta Mishra
    by: Debadatta Mishra | last post by:
    Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
    0
    7130
    marktang
    by: marktang | last post by:
    ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
    0
    7007
    by: Hystou | last post by:
    Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
    1
    6893
    by: Hystou | last post by:
    Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
    0
    5468
    agi2029
    by: agi2029 | last post by:
    Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
    1
    4918
    isladogs
    by: isladogs | last post by:
    The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
    0
    4599
    by: conductexam | last post by:
    I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
    0
    3098
    by: TSSRALBI | last post by:
    Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
    0
    3090
    by: adsilva | last post by:
    A Windows Forms form does not have the event Unload, like VB6. What one acts like?
    0
    1427
    by: 6302768590 | last post by:
    Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.