Organizing a text file

This is a discussion on Organizing a text file within the Tech Board forums, part of the Community Boards category; So... I'm making a chatbot program to familiarize myself with the functions in <stdio.h>... it uses fnmatch, getc, fopen, and ...

  1. #1
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426

    Organizing a text file

    So... I'm making a chatbot program to familiarize myself with the functions in <stdio.h>... it uses fnmatch, getc, fopen, and stuff like that... but I had a question: what's the best way to organize the data file? It holds patterns to match, like "Hello*.", and responses to those patterns, like ">>Hi.". Here's what it looks like, basically:

    Code:
    Hello*.
    >>Hi.
    How are you*?
    >>Peachy.
    *
    >>What?
    (The ">>" is just to differentiate between patterns and responses.)

    So all I have to do here is check every other line with fnmatch() and if it matches, I get the next line and display it.
    I'm not concerned with finding the best match, I can do that on my own time. Just the speed with which I find a match.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Go with that.
    It's simple enough to parse, and easy enough to explain.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bernt View Post
    I'm not concerned with finding the best match, I can do that on my own time. Just the speed with which I find a match.
    So read the entire file into memory using an array of stucts like this one:
    Code:
    struct whatever {
           char pattern[64];
           char response[64];
    };
    Much, much faster than repeatedly reading a static file. It will also make the rest of the programming easier, etc. You could of course set these up in the code itself and not use any text file, but I think the text file is a good idea that will make the program more dynamic and easy to use/modify.

    Some OOP methods would be perfect here if you are using C++.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Thanks.

    I looked at the ALICE program. They split the patterns into files (A.txt, B.txt...) based on the first letter of the input, to minimize time spent searching.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bernt View Post
    Thanks.

    I looked at the ALICE program. They split the patterns into files (A.txt, B.txt...) based on the first letter of the input, to minimize time spent searching.
    Leave it all in one file and then break it up when you load it:
    Code:
    struct whatever *Alist, *Blist...etc
    switch (pattern[0]) {
         case ('a'): addto(Alist, pattern, response)
    Alist would be a pointer to an array of structs, and "addto" would be a function to allocate another struct in the array + add the data to it. I guess these would ideally be linked lists -- maybe that is a bit much to tackle all at once...

    And on the other hand...
    If you are just doing this as a learning exercise, don't waste your time making up tons and tons of patterns/responses -- that is not programming, it's tedious. I think the point you should clue into here is that dealing with data in memory is much much much faster than reading it from a file. You should be able to do the struct array (forget linked lists) -- a basic task. That's learning C programming.
    Last edited by MK27; 05-15-2009 at 02:37 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. checking values in a text file
    By darfader in forum C Programming
    Replies: 2
    Last Post: 09-24-2003, 02:13 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21