LoginSignup
4
4

More than 5 years have passed since last update.

XcodeでC言語 ファイル読み込み編

Last updated at Posted at 2016-02-28

C言語の解説本を見ると、ほとんどがWindowsのVisialStduioでの演習がほとんど。Macユーザーにとってみれば困りもの、gccを使えばいいのだが入力が少し大変、そこでXcodeでC言語をやってみた。

OS Xを選択してC Fileを選ぶ。
スクリーンショット 2016-02-28 20.22.23.png
プロジェクトを作成した後、あらかじめテキストファイル(datalist.txt)を作成しAdd to files to "プロジェクト名"を選択
スクリーンショット 2016-02-28 20.29.41.png
追加した後
スクリーンショット 2016-02-28 20.24.55.png

main.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, const char * argv[])
{
    FILE *fp;
    char s[256];

    if ((fp = fopen("/Users/ユーザー名/Desktop/プロジェクト名/datalist.txt", "r")) == NULL) {
        printf("file open error!!\n");
        exit(EXIT_FAILURE);
    }
    while (fgets(s, 256, fp) != NULL) {
        printf("%s", s);
    }
    fclose(fp); 
    return 0;
}
4
4
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
4
4