간단한 txt파일 read 코드인대 한번 살펴봐 주실수 있을까요?

익명 사용자의 이미지

안녕하세요

리눅스에서 txt파일 안에 특정 값을 불러오기 위한 코드를 찾던 도중

괜찮은 샘플을 하나 발견해서 윈도우 환경에서 실행해 봤습니다

실험에 사용한 텍스트 파일은 test.txt 이며 파일의 내용은 이게 전부입니다

[test]
value=1

보시면 아시겠지만 내용이 window의 ini파일 처럼 생겼습니다.

이 텍스트 파일의 [test] 항목의 ‘value’ 값 즉 1을 불러오기 위한 코드입니다.

코드는 다음과 같습니다.

#include

int iFoundTopic;
int iFoundItem;
int iError;
long lTopicFilePos;
char acTopicHeading80;
char acLastTopicHeading80;
char acItemHeading80;
char acIniLine160;
char acIniPath160;
char acTempPath160;
FILE *pFIniFile;
FILE *pFTempIni;

// readinis
// read configuration string
int readinis(char *pacPath, char *pacTopic, char *pacItem, char *pacValue) { int iItemLength; int iValueLength; char *pcLastCharacter;

iError = 0;

strcpy(acIniPath, pacPath);

// add brackets to topic strcpy(acTopicHeading, “[”); strcat(acTopicHeading, pacTopic); strcat(acTopicHeading, “]\n”);

strcpy(acItemHeading, pacItem); strcat(acItemHeading, “=”);

iItemLength = strlen(acItemHeading);

iFoundTopic = 0; iFoundItem = 0;

// try to open current config file for reading if ((pFIniFile = fopen(acIniPath, “r”)) != NULL) { // has the topic been found before if (strcmp(acLastTopicHeading, acTopicHeading) == 0) { iFoundTopic = 1; fseek(pFIniFile, lTopicFilePos, SEEK_SET); }

// read a line from the config file until EOF while (fgets(acIniLine, 159, pFIniFile) != NULL) { // topic has not been found yet if (iFoundTopic 0) { if (strcmp(acTopicHeading, acIniLine) 0) { // found the topic iFoundTopic = 1; lTopicFilePos = ftell(pFIniFile); strcpy(acLastTopicHeading, acTopicHeading); } continue; } // the item has not been found yet if ((iFoundItem 0) && (iFoundTopic 1)) { // if newline or [, the end of the topic has been reached // no config value in file yet if ((acIniLine0 '\n') || (acIniLine[0] ‘[’)) { iFoundItem = 1; break; }

if (strncmp(acItemHeading, acIniLine, iItemLength) 0) { // found the item iFoundItem = 1; strcpy(pacValue, &acIniLine[iItemLength]); continue; } } } fclose(pFIniFile); } // remove trailing comment iValueLength = strlen(pacValue); while (iValueLength) { if (*(pacValue + iValueLength) ‘#’) { *(pacValue + iValueLength) = ‘\0’; } iValueLength—; } // remove trailing white space while ((iValueLength = strlen(pacValue)) > 0) { pcLastCharacter = (pacValue + iValueLength – 1); if ((*pcLastCharacter ' ') || (*pcLastCharacter ‘\n’) || (*pcLastCharacter '\r') || (*pcLastCharacter ‘\t’) || (*pcLastCharacter '\v') || (*pcLastCharacter ‘\a’) || (*pcLastCharacter '\b') || (*pcLastCharacter ‘\f’)) { *pcLastCharacter = ‘\0’; } else { break; } }

return (iError);
}

int main()
{ char acValue160; acLastTopicHeading0 = ‘\0’; // initialize readinis(“test.txt”, “test”, “value”, acValue); printf(”%s\n”, acValue);

getchar();

return 0;
}

main 함수에서 test.txt 파일의 [test]에 있는 value 값을 읽는 readinis 함수를 호출하고 함수 안은 그냥 텍스트 파일을 읽어서 파싱하는 내용입니다

윈도우에서는 정상동작을 확인했는대 코드만 긁어서 리눅스에서 해보려 하니 파일은 읽지만 값을 못 읽는것 같더군요

혹시 리눅스에서는 개행문자로 ‘\n’ 을 사용하지 않는것인가요?

의심되는 부분이 현재 거기밖에 없어서

아니면 다른부분이 문제라면 지적해 주신다면 감사드리겠습니다

글쓴이의 이미지

코드가 개행처리가 안되어서 들어갔네요;;

삭제하고 다시 올리겠습니다

세벌의 이미지

삭제는 안 됩니다.

https://kldp.org/node/158191
참고해서 다시 쓰면 됩니다.

글쓴이의 이미지

실수로 로그인을 하지 않고 익명으로 글을 써서 수정이 안되어서

부득이하게 새로 게시글을 작성하였습니다.

미련한 행동 사과드립니다 ㅠㅠ

댓글 달기

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