Thread: Programming Strategy Question

  1. #1
    Registered User Spectrum48k's Avatar
    Join Date
    May 2002
    Posts
    66

    Programming Strategy Question

    how would you go about creating a program in C, that will read a file, go through the data and return data in a diferent format.

    for example input movex1 movex2 movex3
    to an output like x1 x2 x3

    please dont write any code. i just want to get a general idea of how this will be done. in the above example it is easy to just get rid of the "move" but in some cases the output wont have any similar characters.

    Thank you in advance !

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Can you be a little more specific what you mean by "different format"?
    Removing parts that are the same in all data?

    ie:

    Glass
    Laser
    Blast

    turns to:

    Gs
    er
    Bt
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User Spectrum48k's Avatar
    Join Date
    May 2002
    Posts
    66
    well in software that programs CNC machines, the software doesnt directly output the code the machine reads. for example in C you get the .obj and than you get the .exe . Same with machines, you get an .apt file from the software, that has to be converted to the code the machine reads (sort of like an exe for the machine)

    the apt file will say "send_to_origin" and that will have to be output for the machine as "G54_X0_Y0_Z0"

    hope this helps..

    thanx..

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Compare the task at hand to a multilingual dictionary. You look up a word and get its translation into another language. The same holds true here.

    You need to build a table for your comparisons.
    Code:
    struct lookuptable
    {
        char *from;
        char *to;
    } table[] =
    {
        { "dog", "cat" },
        { "bird", "fish" },
        { "tree", "rock" },
        { "I", "am" },
        { "so", "tired" },
    };
    Then you figure out what your delimit is, or however you break your file for translation, and read the first "from", and send to your output stream it's "to".

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Assuming each command lies on a line of its own, read in one line at a time, then compare that string to every possible command that can be executed.
    I don't know if this is the best way to do this. Should work though...
    Code:
    LineFromFile = ReadInOneLineFromAFile();
    
    if(strcmp(LineFromFile, "Send_To_Origin") == 0)
    {
       OutputToFile("G54_X0_Y0_Z0");
    }
    else if(strcmp(LineFromFile, "Rotate_90_Degrees") == 0)
    {
       OutputToFile("G54_D90");
    }
    else if(...)
    ...
    
    else
    {
       OutputToConsole("Unknown command");
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User Spectrum48k's Avatar
    Join Date
    May 2002
    Posts
    66
    its alive... its ALIVE... ITS...AL...IIIVE !!!!!!!!!

    thanx !
    its alive... its ALIVE... ITS...AL...IIIVE !!!!!!!!!

  7. #7
    Unregistered
    Guest

    Question

    Assumption - each line has a command followed by a variable number of parameters. Each command will have a constant number of parameters. Meaning you can have a command A which always has 3 parameters
    and another command B which always has 5 parameters. (As opposed to B may have 3 or 4 or n parameters).

    If the command is an integer OR if the first character is unique then (call this X)
    - Have an array (or any convinient DS) indexed by X.
    else
    - find some convinient hashing algorithm to avoid strcmp for each string.

    Have a predefined format - for ex format for A will be like "%d%d%d" and for B will be "%d%d%d%d%d". This will probably static and stored in an array.

    Now read the command - retrieve the format - now you know what you want to do. If you need a seperate function to service each command you can also have a function pointer stored with the format.

    Finally HTH.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM