Thread: c question help for adv programmers

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    53

    c question help for adv programmers

    I have a structure called EMPDATA that contains char firstname[25] and other arrays. I have defined EMPDATA target; and I have used memcpy to copy inputted data such as firstname, lastname, etc into the structure EMPDATA target; I would like to display this information in a text file of all the inputted data. How would I go about doing this?

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    If you want it in text format then:
    Code:
    #include <stdio.h>
    
    typedef struct {
      char name[25];
    } EMPDATA;
    
    void write(FILE *fp, EMPDATA *emp) {
      // if you want to put labels:
      fprintf(fp, "name: %s\n", emp->name);
    
      // without labels
      fputs(fp, emp->name);
    }
    
    int main() {
      EMPDATA emp;
      strcpy(emp.name, "Billy Bob");
    
      FILE *fp = fopen("output.txt", "w");
      write(fp, &emp);
      fclose(fp);
    
      return 0;
    }
    Or if you want it in binary:
    Code:
    #include <stdio.h>
    
    typedef struct {
      char name[25];
    } EMPDATA;
    
    int main() {
      EMPDATA emp;
      strcpy(emp.name, "Billy Bob");
    
      FILE *fp = fopen("output.txt", "w");
      fwrite(&emp, sizeof(EMPDATA), 1, fp);
      fclose(fp);
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    Ok thanks, I'll play around with that when I get home...Why would you do it in binary format?

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    I gave you two options: text and binary. I wasn't sure how you wanted to save your data. You could've wanted it in xml or something else, haha.

    Binary format is generally more compact, not easy for users to edit, but easier to read and write. It basically dumps the whole chunk of memory into a file. Then you just use:
    Code:
    fread(&emp, sizeof(EMPDATA), 1, fp);
    ...to read it.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but easier to read and write
    Not if you want to do so portably. The portable functions to read and write binary files (which you have to create yourself most of the time) are long and complex enough to be a good argument in favor of text files.
    My best code is written with the delete key.

  6. #6
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    empdata=e
    use this code if you want
    Code:
    #include "stdio.h"
    #include "conio.h"
    
    typedef struct {
      char name[25]={'.','.','.','.','.','.','.','.,'.',.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'};/* should be 25 */
    } E;
    d E;
    void main()
    {
    int j;
    for(j=0;i<25;j++)
    {if (d.name[i]=='.'){break;}
    /* (d.name[i])====> the caracter */
    /*call function of writing  a character in the file */
    /* tha's easy to do */
    }
    getch()
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just quit. None of your code even compiles!
    Code:
    typedef struct {
      char name[25]={'.','.','.','.','.','.','.','.,'.',.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'};/* should be 25 */
    } E;
    d E;
    Wrong. It should be:
    Code:
    typedef struct {
        char name[25];
    } E;
    
    E d;
    1) You have your E after your d, which is basicly putting the name before the type.
    2) You cannot initialize portions of structures in the definition.

    Code:
    void main()
    No. Read the FAQ.

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

  8. #8
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    sorrysorrysorrysorrysorrysorrysorrysorrysorrysorry sorrysorrysorry
    sorrysorrysorrysorrysorrysorrysorrysorrysorrysorry sorrysorrysorry
    sorrysorrysorrysorrysorrysorrysorrysorrysorrysorry sorrysorrysorry
    sorrysorrysorrysorrysorrysorrysorrysorrysorrysorry sorrysorrysorry
    sorrysorrysorrysorrysorrysorrysorrysorrysorrysorry sorrysorrysorry
    sorrysorrysorrysorrysorrysorrysorrysorrysorrysorry sorrysorrysorry
    sorrysorrysorrysorrysorrysorrysorrysorrysorrysorry sorrysorrysorry
    sorrysorrysorrysorrysorrysorry ===== > and so sorry
    Last edited by Salem; 04-28-2004 at 12:34 AM. Reason: Abuse of code tags

  9. #9
    Welcome to the real world
    Join Date
    Feb 2004
    Posts
    50
    Quzah - thank you being the first person to publicly express what I've been wanting to say about enjoy's tenure here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Are programmers engineers?
    By joshdick in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 04-01-2003, 01:55 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Programming Puns
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 03-23-2002, 04:38 PM
  5. One question to C++ game programmers
    By incognito in forum C++ Programming
    Replies: 6
    Last Post: 12-31-2001, 11:47 AM