Thread: memset()

  1. #1
    Unregistered
    Guest

    memset()

    I am trying to fill the storage for jones allocated by malloc with "#" using memset.

    I am not having much luck......please help I think there is also something wrong with my code.

    Here it is:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    FILE *fptor;
    FILE *ifp;

    struct phoneTemplate
    {
    char lastName[16];
    char firstName[16];
    char phoneNumber[16];
    };
    typedef struct phoneTemplate phoneRec;

    void printRec(struct phoneTemplate *thisName);
    int main(void)
    {
    struct phoneTemplate *smith;
    phoneRec *jones;

    fptor=fopen("c:\\c\\strprobfile.txt","w");
    ifp=fopen("c:\\c\\strprobfile.txt","r");

    smith=(struct phoneTemplate *)malloc(sizeof(struct phoneTemplate));
    if (smith==NULL)
    {perror("strprob3"); exit(1);}

    jones=(phoneRec *)malloc(sizeof(phoneRec));
    if (jones==NULL)
    {perror("strprob3"); free(smith); exit(1);}

    memset(jones,"#",sizeof(jones));

    strcpy(smith->lastName, "Smith");
    strcpy(smith->firstName, "Fred");
    strcpy(smith->phoneNumber,"(212)262-7321");

    strcpy((*jones).lastName, "Lowel");
    strcpy((*jones).firstName, "Jones");
    strcpy((*jones).phoneNumber,"(617)362-8328");

    fwrite(smith,sizeof(struct phoneTemplate),1,fptor);
    fwrite(jones,sizeof(struct phoneTemplate),1,fptor);

    fread(smith,sizeof(struct phoneTemplate),1,ifp);
    fread(jones,sizeof(struct phoneTemplate),1,ifp);

    printRec(smith);
    printRec(jones);

    free(smith);
    free(jones);

    fclose(fptor);
    fclose(ifp);

    return 0;
    }

    void printRec(struct phoneTemplate *thisName)
    {
    printf("Name: %s, %s\n", thisName->lastName,thisName->firstName);
    printf("Phone: %s\n\n", thisName->phoneNumber);

    return;
    }

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    >memset(jones,"#",sizeof(jones));

    You need to fill in the place with # with a number to fill jones. For example to fill jones to all zeros you would do this:
    Code:
    memset(jones, 0, sizeof(phoneRec);
    Here is the documentation for memset
    Synopsis
    #include <string.h>
    void *memset(void *s, int c, size_t n);

    Description
    The memset function copies the value of c (converted to an unsigned char) into each of the first n characters of the object pointed to by s.

    Returns
    The memset function returns the value of s.
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    spoon_
    Guest
    try

    Code:
    memset(jones, '#', sizeof(phoneRec);
    single quotes signal a character (or int... simple casting) while double quotes signal a string (char*).

    spoon_

    (i think i'm right.. =))

  4. #4
    Unregistered
    Guest
    thanks it works...I should have uses '#' insted of "#"

    thanks all!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. info on memset and recv
    By lolguy in forum Networking/Device Communication
    Replies: 15
    Last Post: 11-12-2009, 05:48 AM
  2. Question about memset
    By Mortissus in forum C Programming
    Replies: 3
    Last Post: 03-24-2009, 08:16 AM
  3. Simple Question on Correct Usage of memset
    By deadpoet in forum C++ Programming
    Replies: 2
    Last Post: 03-16-2004, 08:58 AM
  4. memset has conflicting declaration
    By mangoMan in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2004, 10:08 AM
  5. memset() or ZeroMemory()
    By jdinger in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2002, 09:38 AM