第12話 ファイル

 


fputs.c

/*

 * (c)Copyright Spacesoft corp., 2006 All rights reserved.

*/

#include <stdio.h>

 

int main(void)

{

    FILE* fp ;

    char* str="This is a pen.\n";

 

    /* オープン */

    if( (fp = fopen( "test.txt", "wt" )) == NULL )

        return -1;

 

    /* 書き込み */

    fputs(str, fp);

    fputs(str, fp);

    fputs(str, fp);

    fputs(str, fp);

 

    /* クローズ */

    fclose( fp ) ;

 

    return 0;

}


fgets.c

/*

 * (c)Copyright Spacesoft corp., 2006 All rights reserved.

*/

#include <stdio.h>

 

int main(void)

{

    FILE* fp ;

    char buff[256];

 

    /* オープン */

    if( (fp = fopen( "test.txt", "rt" )) == NULL )

        return -1;

 

    /* 読み込み */

    while( NULL != fgets(buff,sizeof(buff),fp))

        printf("%s",buff);

 

    /* クローズ */

    fclose( fp ) ;

 

    return 0;

}


fwrite.c

/*

 * (c)Copyright Spacesoft corp., 2006 All rights reserved.

*/

#include <stdio.h>

#include <string.h>

 

struct _t_rcd

{

    char    name[16];

    char    hobby[32];

    int     id;

    char    sex;

};

 

 

intmain(void)

{

    FILE* fp ;

    struct _t_rcd record;

 

    /* オープン */

    if( (fp = fopen( "test.bin", "wb" )) == NULL )

        return -1;

 

    strcpy(record.name, "sato");

    strcpy(record.hobby, "play tennis");

    record.id=0x01020304;

    record.sex='M';

    fwrite((void*)&record, sizeof(record),1,fp);

 

    strcpy(record.name, "tanaka");

    strcpy(record.hobby, "watch television");

    record.id=1;

    record.sex='F';

    fwrite((void*)&record, sizeof(record),1,fp);

 

    /* クローズ */

    fclose( fp ) ;

 

    return 0;

}


fread.c

/*

 * (c)Copyright Spacesoft corp., 2006 All rights reserved.

*/

#include <stdio.h>

 

struct _t_rcd

{

    char    name[16];

    char    hobby[32];

    int     id;

    char    sex;

};

 

int main(void)

{

    FILE* fp ;

    struct _t_rcd record;

 

    /* オープン */

    if( (fp = fopen( "test.bin", "rb" )) == NULL )

        return -1;

 

    /* 読み込み */

    while( fread((void*)&record, sizeof(record),1,fp) ==1)

    {

        printf("record.name=%s\n",record.name);

        printf("record.hobby=%s\n",record.hobby);

        printf("record.id=0x%08X\n",record.id);

        printf("record.sex=%C\n",record.sex);

        printf("\n");

    }

 

    /* クローズ */

    fclose( fp ) ;

 

    return 0;

}


fwrite02.c

/*

 * (c)Copyright Spacesoft corp., 2006 All rights reserved.

*/

#include <stdio.h>

 

struct _t_rcd

{

    char    name[16];

    char    hobby[32];

    int     id;

    char    sex;

};

 

 

int main(void)

{

    FILE* fp ;

    static struct _t_rcd record[] ={

        {"sato","play tennis",0x01020304,'M'},

        {"tanaka","watch television",1,'F'},

        {"suzuki","listen to music",0x12345678,'M'},

        {"taro","skiing",2,'F'}

    };

 

    /* オープン */

    if( (fp = fopen( "test.bin", "wb" )) == NULL )

        return -1;

 

    fwrite((void*)record, sizeof(record),1,fp);

 

    /* クローズ */

    fclose( fp ) ;

 

    return 0;

}

もどる