LoginSignup
0
0

C

Last updated at Posted at 2024-04-19
c.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    FILE* fp;
    char buffer[1024];
    char* filename;
    char* context = NULL; // strtok_s用のコンテキストポインタ
    char filepath[1024];
    errno_t err;

    // CSVファイルを開く
    err = fopen_s(&fp, "D:/file_list.csv", "r");
    if (err != 0) {
        fprintf(stderr, "ファイルオープンに失敗しました: %d\n", err);
        return EXIT_FAILURE;
    }

    // CSVファイルから一行ずつ読み込み
    while (fgets(buffer, 1024, fp)) {
        // 改行文字を削除
        buffer[strcspn(buffer, "\n")] = 0;

        // ファイル名を取得(strtok_sを使用)
        filename = strtok_s(buffer, ",", &context);
        if (filename == NULL) {
            continue;
        }

        // .txtファイルパスを構築
        snprintf(filepath, sizeof(filepath), "D:/%s.txt", filename);

        // 新しい.txtファイルを作成して開く
        FILE* txtfile;
        err = fopen_s(&txtfile, filepath, "w");
        if (err != 0) {
            fprintf(stderr, "ファイル '%s' を作成できませんでした。エラーコード: %d\n", filepath, err);
            continue;
        }

        // 例えば、何かの内容を.txtファイルに書き込む
        fprintf(txtfile, "このファイルは%sに基づいて作成されました。\n", filename);

        // テキストファイルを閉じる
        fclose(txtfile);
    }

    // CSVファイルを閉じる
    fclose(fp);

    return EXIT_SUCCESS;
}


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0