구조체 질문있습니다!

josimhaela의 이미지

#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
 
struct userInfo
{
        char ID[10];
        char pw[15];
};
 
void makeUserInfo(struct userInfo *uinfo, char *ID, char *pw)
{
  memset((void *)uinfo, 0x00, sizeof(struct userInfo));
        strcpy(uinfo->ID, ID);
        strcpy(uinfo->pw, pw);
}
 
int main()
{
  int fd;
  struct userInfo linuxusr;
  int i;
  char ID[10];
  char pw[15];
 
  fd=open("member.txt", O_CREAT|O_WRONLY, S_IRUSR|S_IRGRP|S_IWGRP);
  if(fd<0)
  {
        perror("Error while opening file");
        return 1;
  }
 
  for(i=0;;i++){
  printf("Type the user id: ");
  scanf("%s",ID);
  fflush(stdin);
  printf("Type the password: ");
  scanf("%s",pw);
  fflush(stdin);
 
  makeUserInfo((void *)&linuxusr, ID, pw);
  write(fd, (void *)&linuxusr, sizeof(linuxusr));
  }
 
  close(fd);
  return 0;
}

위에서 출력 결과물 member.txt 를 vi 나 cat으로 보면 전부다 나오는데
strings member.txt 를 하면
처음에 받는 id 랑 pw 는 나오고 다음에 나오는 id는 표시가 안되는데
중간에 널값이나 그런게 들어가서 그런건가여 ?
아니면 구조체 변수 linuxusr 을 배열로 해서 처리해야 하나요 ?
k2hyun의 이미지

k2hyun\0\0\0\0dirmsrmawl\0\0\0\0\0

member.txt 파일에는 이렇게 저장되겠지요.

strings는 문자열 부분만 출력하니 저런 결과가 나올 듯 합니다.

더 이상 없다.