C 파일입출력 데이터 삭제 기능 (수정)

sori1333의 이미지

파일입출력을 이용하여 삭제기능.
텍스트 파일에 아래 형식의 내용을 읽어서 원하는 줄 삭제.
a-1
b-2
c-3

a 입력시 a-1 삭제되게 하는 기능.

struct CONTE
{
char key[20]; //a b c
char value[20]; // 1 2 3
};
struct CONTE in[20];
main 생략
void Delete()
{
char dkey[20]; //입력받은
int j=0;
int dataLastIndex = 0;
char *son;
char *son2;


fp = fopen("CON.txt","r");

if(fp == NULL)
{
printf("open error\n");
return;
}

while(fgets(in[j].key,20,fp)!=NULL)//한줄 씩 읽기
{

son = strtok(in[j].key,"="); // a b c 나누고
son2 = strtok(NULL,"="); // 1 2 3 으로 나눔
printf("%s\n",son);
printf("%s",son2);

}// 제가 고민하고 있는 부분은
위의 son 과 son2처럼 문자를 나누고 이제 son과 son2의 값을 사용하기 위해서 어떤 방법을 써야 하는지 모르겠습니다.
malloc을 이용하는 방법도 있다는데,
구조체를 동적할당하는 방법을 몰라서요..

printf("Delete Key: "); //삭제하고 싶은 키를 입력하면
scanf("%s", dkey);

for(j=0; j {
printf("여깄다\n");
printf("%s \n", in[j].key);

if(strcmp(son, dkey) == 0) //삭제하고 싶은 키와 key 값이 같으면 (son의 값)
{
for(j; j<=dataLastIndex; j++)
{
strcpy(in[j].key, in[j+1].key);
strcpy(in[j].value, in[j+1].value);
printf("%s \n", in[j+1].key);
}
printf("Delete!!!\n");
}
}

fclose(fp);
printf("여긴?\n");
fp = fopen("CON.txt","w");
printf("여긴2?\n");
for(j=0; j {
fprintf(fp, "%s \n", in[j].key);
fprintf(fp, "%s\n", in[j].value);
}

fclose(fp);
}

익명 사용자의 이미지

전혀 무슨 소리인지 알수 없군요....
일단 동적할당이라 하셧는데 코드에서 malloc, new 같은 키워드는 보이지도 않고요...
코드는 깨졋고...

일단 c++ stl에 map이라는 것이 있다는것을 알려드립니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.