파일입출력 도와주십쇼 미치겠네요

익명 사용자의 이미지

void update()
{
	FILE *fp;
	int fd;
	int flags = O_WRONLY | O_CREAT;
	mode_t mode = 0644;
	char buf[1024];
	char *line = NULL;
	fp=open("./memo.txt","r");    
	size_t len =0;
	ssize_t read;
	char name;
 
 
	printf("수정할할 값을 입력하세요 : ");
	scanf("%s", &name);
 
	printf("수정할 이름 : ");
	scanf("%s", &card.name);
	printf("수정할 회사 : ");
	scanf("%s", &card.company);
	printf("수정할 연락처 : ");
	scanf("%s", &card.tel);
	printf("\n\n");
 
	sprintf(buf, "%s\t%s\t\t%s\n", card.name, card.company, card.tel);
	fd = open("temp_memo.txt", flags, mode);
 
	while((read=getline(&line,&len,fp)) != -1)
	{
		if(strchr(line,name))
		{
			write(fd, buf, strlen(buf));
			continue;
		}else{
			write(fd,line,strlen(line));
		}
	}
	free(line);
	remove("memo.txt");
	rename("temp_memo.txt","memo.txt");
	remove("temp_memo.txt");
 
	return;
 
}

결과를 보자면
예를 들어

kim sa 101
lee lg 202
park hp 303

이렇게 저장되어 있으면

처음에 lee를 수정하면

kim sa 101
변경된 값 (아주 잘뜸)
park hp 303

이렇게 잘 뜹니다.

근데 여기서 한번더 lee를 변경 시켜주면(이름 : lee 회사 : aa 번호 : aa)

lee aa aa
lee aa aa
lee aa aa

입력받은 값으로 모조리 바뀝니다..

왜이러는 걸까요?

웃긴게

kim sa 101
lee aa aa <--- lee를 변경(lee , aa ,aa) 아주 잘뜸
park hp 303

여기서 park을 변경하면 또 park도 잘 변경되어서 나옵니다.

근데 문제점이

lee나 park이나 kim이나

한번 수정할땐 이상 없는데, 두번 수정하면

최근 수정하려는 입력값으로 모조리 다 바뀐다는겁니다 ㅠㅠ

왜그런지 미치겠습니다.

 의 이미지

	char name;
  
	printf("수정할할 값을 입력하세요 : ");
	scanf("%s", &name);

여기까지 읽고 스크롤 쭉 내렸습니다.
왜 이렇게 짜면 안 되는지 직접 생각해보세요.

익명 사용자의 이미지

아 그건 제가 실수했네요!
오랜만에 C를 하는거라 기본적인 문법이 기억나질 않습니다ㅠ..
근데 그걸 고쳐도 문제는 똑같네요..

 의 이미지

문제가 몇 개나 될런지는 모르겠습니다만 제가 다 찾아서 고쳐 드리기는 좀 그렇군요.

한 가지 더. 함수에서 파일을 두 개 열고 있는데, 저라면 그 파일을 지우거나 옮기기 전에 먼저 닫을 겁니다. 안 그러면 온갖 비직관적인 일이 일어날 수 있거든요. 특히 buffered I/O라면 더더욱.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.