c언어 파일 입출력 도와주세요

yg0585의 이미지

15::4973::excellent!::121518463020
텍스트 파일의 형식이 이렇게 되어있습니다.
:가 두개라서 strtok을 두번 쓰고 하려고 하는데 어디서 문젠지 모르겠는데구조체에 잘 안들어가는거 같습니다.
어떻게 해야될까요...?
struct _tag {
int userID;
int movieID;
char tag[50];
int timestamp;
};
tag *tags;
void tagsearch() {
int i = 0;
FILE *f;
char line[256];
char *lines;
char *ptr;
int count = 0;

tags = (tag *)malloc(sizeof(tag) * 1024);

f = fopen("C:/Users/abc/Desktop/data/tags.dat", "r");

while (!feof(f)) {
lines = fgets(line, sizeof(line), f);
/*printf("%s", lines);*/
ptr = strtok(lines, ":");

tags[i].userID = ptr;
ptr = strtok(NULL, ":");
ptr = strtok(NULL, ":");

tags[i].movieID = ptr;
ptr = strtok(NULL, ":");
ptr = strtok(NULL, ":");

tags[i].tag = ptr;
ptr = strtok(NULL, ":");
ptr = strtok(NULL, ":");

tags[i].timestamp = ptr;

i++;
/*char buf = fgetc(f);
if (buf == ':') {
count++;
}*/
}

printf("%d", tags[0].userID);

free(tags);
fclose(f);
}

????의 이미지

소스코드 넣는 방법
https://kldp.org/node/158191

세벌 https://sebuls.blogspot.kr/

==================================================

농담은 여기까지.

tags[i].userID = ptr;

이 줄에만 벌써 잘못된 부분이 두 개나 있습니다.

1. ptr은 char *인데 tags[i].userIDint입니다.
2. ptr은 lines 배열 내부를 가리키는데, 이 배열은 루프를 돌 때마다 내용이 바뀝니다. 어디다가 저장해 봤자 의미가 없다는 뜻입니다. strtok가 어떻게 동작하는지 잘 모르겠으면 레퍼런스를 찾아보세요: https://en.cppreference.com/w/cpp/string/byte/strtok

문제를 지적해 드렸으니 해결하는 방법은 직접 찾아보시길 바랍니다. 요즘 과제물 코드가 하도 많이 올라와서 다 풀어드리기는 귀찮군요.

guest의 이미지

kldp에 소스 넣기

yg0585의 이미지

그럼 tags[i].userID =(int)* ptr; 이렇게 하면 되는건가요?

라스코니의 이미지

디버그를 해 보세요.
line by line으로 실행하면서 각각 변수에 어떤 값이 들어가는지 보면 대부분의 문제 해결 실마리가 보이게 되죠.
만약 생각 이외의 값이 할당되면 어떻게 그 값을 넣을 수 있는지 고민한다면 스스로 도움이 되실 겁니다.
스스로 해결이 안되면 질문을 다시 하시면 되고.

익명 사용자의 이미지

그게 될 것 같은지, 만약 된다면 userID에 어떤 값이 들어갈 것 같은지 직접 한 번 설명해보시죠.

기초 C언어 수준에서 자기가 어떤 코드를 작성하고 있는지 스스로 설명할 수 없는 상황이면 그 자체로 문제가 있는 겁니다. 그런 상황을 벗어나기 위해 공부를 더 해야 합니다.

C언어는 대충 될 것처럼 짜면 대충 잘 돌아갈 정도로 친절한 언어가 아니라서요.

댓글 달기

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