#include <stdio.h>


int main(int iarg, char * cparg[])
{
  FILE * src;
  FILE * des;
  int ch;

  if(3!=iarg)
  {
    printf("사용법 : main4.c 원본파일 사본파일 \n");
    return 0;
  }

  src = fopen(cparg[1],"rt"); 읽기모드
  if(src==0)
  {

    puts("can't open a original file");
    return -1;  
  }
  des = fopen(cparg[2],"wt"); 쓰기모드
  
  if(des==0)
  {

    puts("can't open a copyfile");
    fclose(src);
    return -1;
  }

  while((ch=fgetc(src))!=EOF) src에 들어가있는 아스키코드값을 하나하나씩 출력
    fputc(ch, des);
  if(feof(src)!=0)      
    puts("copy completed");    //파일의 끝이면 0이 아닌값 반환
  else
    puts("copy failed");    //파일의 끝이아니면 0값 반환
  fclose(src);
  fclose(des);
}


 #include <stdio.h>

//#pragma pack(1) 프라그마 쓰면 1byte트씩 하나씩하나씩 차곡차곡 들어가는거 아시져?
struct smart
{
  int iNum;
  char A;
  int iNum2;
  char B;
  short C;
};
//#pragma pack(4)

int main()
{
//  int iNum=0x41424344;
  struct smart test;
  struct smart test2;

  FILE * fp;
  fp = fopen("AA.bin","wb");

  test.iNum = 0x41424344;
  test.A = 'Z';
  test.iNum2 = 0x45464748;
  test.B = 'Y';
  test.C = 0x494A;  

//  fwrite(&iNum,4,1,fp);
  fwrite(&test,sizeof(struct smart),1,fp);   //혹은 sizeof(test)로 해도됨
  fclose(fp);

  fp = fopen("AA.bin","rb");    
  fread(&test2,sizeof(struct smart),1,fp);  //fp를 읽어서 
  fclose(fp);


  printf("struct smart'size: %d \n"sizeof(struct smart)); 스마트구조체 크기

  printf("%08X \n", test2.iNum);
  printf("%c \n", test2.A);
  printf("%08X \n", test2.iNum2);
  printf("%c \n", test2.B);
  printf("%08X \n", test2.C);

  return 0;
}




'C언어' 카테고리의 다른 글

C와C++ 컴파일 할때 함수이름 주의  (0) 2014.09.30
20140430 분할컴파일  (0) 2014.04.30
20140428 USART통신  (0) 2014.04.28
20140425 파일열고닫기  (0) 2014.04.25
20140422 링크드함수 삽입.삭제  (0) 2014.04.22
by 날라차숑 2014. 4. 28. 16:49