파일입출력 질문드립니다.

익명 사용자의 이미지

#include <stdio.h>
#include <stdlib.h>
 
main()
{
    FILE *fp, *fp1;
 
    int i;
    char cmd_buffer[300];
    char output_buffer[100];
 
    while(1) {
 
        if( (fp1 = fopen("/tmp/data.txt", "r+")) == NULL ) {
            fp = fopen("/tmp/data.txt", "w+");
            for(i=0; i<16; i++) {
                sprintf(cmd_buffer, "./CLI state %d", i);
 
                FILE *stream = popen(cmd_buffer, "r");
 
                if(stream != NULL) {
                    while(fgets(output_buffer, sizeof(output_buffer), stream) != NULL);
 
                    printf("%s\n", output_buffer);
                    fprintf(fp, "%s\n", output_buffer);
                }
            }
 
            fclose(fp);
            fclose(fp1);
        }
    }
}

구글에서 검색하여 해결해보려 했지만 잘 안되어 질문드립니다.

리눅스에서 코딩하였고, 해당 프로그램이 백그라운드에서 계속 돌아야 하는데, 한번 프로그램을 처리하고 segmentation fault가 뜹니다.

fclose를 지우니 한 번은 동작이 되는데 제가 임의로 해당 파일을 지우면 다시 파일을 쓰지 못합니다.

fp가 계속 물려(?) 있어서 그런 거 같은데.. 조언 부탁드립니다.

bushi의 이미지

fclose(fp1) 의 위치가 ?

익명 사용자의 이미지

정확히 하고 싶은 행동이 무엇인가요?

백연구원의 이미지

close 위치도 문제고, 없는 것도 문제고 등등 총체적인 난국이지만 두 가지를 먼저 살펴보세요. 이것만 해결되도 segmentation fault 는 없어질겁니다.

// 여기 라인. 무엇을 기대한 것인지.
if( (fp1 = fopen("/tmp/data.txt", "r+")) == NULL ) {
    // fopen 이 성공했는지 검사코드 추가.
    fp = fopen("/tmp/data.txt", "w+");

소곤소곤

작성자의 이미지

if( (fp1 = fopen("/tmp/data.txt", "r+")) == NULL ) {

부분은 파일 유무를 확인하려는 구문이었는데,
저 구문을 access 함수를 사용하니 잘 되었습니다..

조언해주셔서 감사합니다.

댓글 달기

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