기본 콘텐츠로 건너뛰기

c json 형식으로 저장

https://dojang.io/mod/page/view.php?id=728
#define _CRT_SECURE_NO_WARNINGS    // fopen 보안 경고로 인한 컴파일 에러 방지
#include      // 파일 처리 함수가 선언된 헤더 파일

int main()
{
    // JSON 문서에 저장할 데이터
    char *title = "Inception";
    int year = 2010;
    int runtime = 148;
    char *genre = "Sci-Fi";
    char *director = "Christopher Nolan";
    char actors[5][30] = {
        "Leonardo DiCaprio",
        "Joseph Gordon-Levitt",
        "Ellen Page",
        "Tom Hardy",
        "Ken Watanabe"
    };
    double imdbRating = 8.8;

    FILE *fp = fopen("example.json", "w");    // 쓰기 모드로 파일 열기

    // JSON 문법에 맞춰서 fprintf 함수로 값 출력
    fprintf(fp, "{\n");
    fprintf(fp, "  \"Title\": \"%s\",\n", title);
    fprintf(fp, "  \"Year\": %d,\n", year);
    fprintf(fp, "  \"Runtime\": %d,\n", runtime);
    fprintf(fp, "  \"Genre\": \"%s\",\n", genre);
    fprintf(fp, "  \"Director\": \"%s\",\n", director);
    fprintf(fp, "  \"Actors\": [\n");
    fprintf(fp, "    \"%s\", \n", actors[0]);
    fprintf(fp, "    \"%s\", \n", actors[1]);
    fprintf(fp, "    \"%s\", \n", actors[2]);
    fprintf(fp, "    \"%s\", \n", actors[3]);
    fprintf(fp, "    \"%s\" \n", actors[4]);
    fprintf(fp, "  ],\n");
    fprintf(fp, "  \"imdbRating\": %.1f\n", imdbRating);
    fprintf(fp, "}\n");

    fclose(fp);    // 파일 닫기

    return 0;
}

댓글

이 블로그의 인기 게시물

java 특정 디렉토리에 있는 파일 목록을 읽어내기, 정렬해서 가져오기

폴더 리스트 가져오기 String path="C:\"; File dirFile=new File(path); File []fileList=dirFile.listFiles(); for(File tempFile : fileList) {   if(tempFile.isFile()) {     String tempPath=tempFile.getParent();     String tempFileName=tempFile.getName();     System.out.println("Path="+tempPath);     System.out.println("FileName="+tempFileName);     /*** Do something withd tempPath and temp FileName ^^; ***/   } } 정렬해서 가져오기 import java.io.FileFilter; import java.io.IOException; import java.util.Arrays; import java.util.Date; import org.apache.commons.io.comparator.LastModifiedFileComparator; import org.apache.commons.io.filefilter.FileFileFilter; public class LastModifiedFileComparatorTest { public static void main(String[] args) throws IOException { File directory = new File("."); // get just files, not directories File[] files = directory.listFiles((FileFilter) FileFileFilter.FILE); System.out.println("Defaul...

UltraEdit Professional Version 18.20.0.1014 라이센스 키 넣기

Step 1: unpack rar archive      Step 2: run the setup   Step 3: Use the given keygen to activate    Add the line   127.0.0.1 licensing.ultraedit.com         to your %WINDOWS%\system32\drivers\etc\hosts file                 Enter any data for license-id and password (license-id must be a         number)                 Choose Offline activation and generate corresponding activation         codes for your user codes   Step 4: Enjoy and Support Developers, Buy It, They Deserved It!