Thread: Help me solve this format problem...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    Help me solve this format problem...

    What I need to do is have a variable, variable list inside a fscanf function. I am trying to make my visualiztion program independant of the format of the simulation files. Thus I am using a configuration file to define the format of the input files in the preferences. Now the idea I have is to use pointers something like so

    fscanf(file, format_str, p1, p2, p3, p4, p5 ,p6);

    Where the p's are void ptrs of which I can change what they point to depending on the configuration format specified. Now would this work? and are there any major problems I am getting myself into?
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >and are there any major problems I am getting myself into?
    It looks like you're getting into the problem of solving the wrong problem. If the format is variable then strict formatted input with fscanf is not the ideal solution.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    208
    Well it is not variable in the sense that it changes from file to file, just from user to user, thus it will be part of the preferences to change how you wish for your files (coordinate files) to be read in
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Well it is not variable in the sense that it changes from file to file, just from user to user
    Yet it still changes. That implies that you need a generic enough solution to fit user perferences, and fscanf is inherently inflexible.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    208
    Well is there a better way?

    The inputs are just basic text files
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I'm still not even sure exactly what you're trying to do. I've never seen an application that required the user to know anything about the underlying data storage.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    208
    Ok I will explain further. I work in a Computational Physics Lab, Personally I do Lennard - Jones Molecular Dynamics simulations to study the properties of metastable liquids, however, other people in this lab, and through-out the computational physics realm do different simulations and there is no standard output which these simulations follow, usually due to the fact they are independantly written. Now I want my visualization program to be completely portable between any type of simulation, and all that requires is a bit of configuring on the user's part to establish the format of their output (which is in turn my input). Essentially what I want is to set it up so that my ReadData() function will read from any text file, as long as the user has configured the program to do so. I know how I want to do it, but my question is will using void pointers in a fscanf(); and then, changing what variables those pointers point to, during configuration work? I will outline a simple pseudocode below.

    Code:
    int x, y, z , type,
    
    void p1 p2 p3 p4 p5 p6 p7 p8
    
    fscanf(file, format_str, p1, p2,p3, p4, p5, p6, p7,p8);
    Now if the user's format is say
    txt= z txt=y txt= x txt=type

    then I want to have

    format_str = "%s %f %s %f %s %i"
    p1 -> dummy
    p2 -> z
    p3 -> dummy
    p4 -> y
    p5 -> dummy
    p6 -> x
    p7 -> dummy
    p8 -> type

    and I want all that to be changable incase the next person who use's my program comes along and their data format is x y z type, for example. They would set the format string (using easier techniques which I will outline in my program) and the program would then set the ptr's accordingly. That way I need only 1 read data function and just another function which rearranges what the ptr's point to and reassign's format_str.

    Catch my drift? If not I will be happy to answer any questions. This seems intuitvly possible but I am unsure about the void pointers. My assumption is typecasting them? Or maybe using them as pointers to pointers of proper type?
    Last edited by kas2002; 08-02-2006 at 01:08 PM.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    That seems kind of difficult. You'll have to have the pointers point to some kind of allocated memory.

    Could you give me a sample snippet of one of the data files. Are there newlines?
    If you understand what you're doing, you're not learning anything.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So for example, one persons file begins with

    x y
    1 2
    3 4

    And another persons file begins with
    x y z
    1 3 4
    3 5 6

    And you want to read the first line of the file to establish the format, and then read the rest of the lines in some kind of sense based on the format. Then presumably do something useful with the information.
    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.

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    208
    Essentially yes, now the files may be a little bit more complicated then that, but that is the most basic premise behind what I want to do. Added into the fact the the order, not only number, or variables and types may be changed.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So you read the first line, and assign each 'word' to be the column headings
    Say
    char **columnHeadings;

    Once you've parsed the first line, then you know how many fields should be in each data row.

    Then you can start with
    double **data;

    For each row, you read the line with fgets(), and allocate a new row with
    data[n] = malloc ( n * sizeof *data[n] );

    Then parse the line to extract n values from each line.
    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.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you would use some regular expression library you could transform the input to some standard format that you could parse easily using sscanf.
    Kurt

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    Does your program use a header? _
    Does how many fields per line? _
    Are blank fields allowed? _
    Do incomplete lines count? _
    Do you have 'skip blocks'? _
        Denote the start of a skip block: _
        Denote the end of a skip block: _
    Enter file name: _
    There are your prompts. Now code for each one of them. A list of lists should handle this fine. You can either have a list per variable, or a list per line, or link them both ways. Based on the result of each prompt, you'll now know what you need to stop and start on, and how to handle all of your lines.

    You can get a bit more advanced and add chunks per line to scan for, or multi-line data. I'll leave that up to you.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Solve This Problem Within 3 Hr Urgent Need
    By annum in forum C Programming
    Replies: 12
    Last Post: 10-04-2009, 09:56 AM
  2. Replies: 4
    Last Post: 09-14-2008, 06:17 PM
  3. Problem returning values in user defined format
    By manutdfan in forum C Programming
    Replies: 6
    Last Post: 11-20-2006, 05:20 PM
  4. Can someone solve this problem for me?
    By hykyit in forum C Programming
    Replies: 2
    Last Post: 03-17-2005, 02:57 AM
  5. problem cant solve please help.
    By sarah in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:32 PM