Thread: Can i make my programm any better?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    11

    Question Can i make my programm any better?

    I have written a program that reads from a sequential text file
    then validates the record.
    the rec is for example
    c12345my name my address;and house number; 12345678
    the final print out is what is store on file.

    How can i use structures to seperate the record fields, to have
    a printout like

    c 12345 my name my address;and house number; 12345678
    (just to make the printout more readable)

    Thanks

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by vVv
    I don't get it.

    >How can i use structures to seperate the record fields

    Code:
    struct foo {
      char *name,
           *address;
      int   housenum;
    } bar = {
      "meh", "leggen", 123
    };
    
    ....
    
    fwrite( &bar, sizeof( struct foo ), 1, some_file );
    /*
    * rewind( some_file );
    * fread( &other_structure, sizeof( struct foo ), 1, some_file );
    * printf( "%s %s %d\n", other_structure.name, other_structure.address, other_structure.housenum );
    */
    ?!
    I don't get it!
    Code:
    struct foo
    {
       char name[128];
       char address[128];
       int housenum;
    }

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    11
    Well, what i wantt to do is, the record on file is like this ->

    Code:
    c12345my name my address;and so on; postcode\n
    i want a print out like so ->
    
    c   12345    my name      my address;and so on;     post code
    i think this is more readable.
    any more veiws on this please.


    Code tags added by Hammer

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >any more veiws on this please.
    Create a structure with the necessary fields (something like already shown)
    Parse the input data, populating the structure elements accordingly.
    Use a formated print function (like printf() ) to write out what ever bits you want, using the format modifiers to set the field widths.
    >printf ("%10s, %10s, %10s", foo.field1, foo.field2, foo.field3);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make a Packet sniffer/filter?
    By shown in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2009, 09:51 PM
  2. "Cannot make pipe"
    By crepincdotcom in forum C Programming
    Replies: 5
    Last Post: 08-16-2004, 12:43 PM
  3. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  4. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM
  5. Replies: 6
    Last Post: 04-20-2002, 06:35 PM