LoginSignup
2
2

More than 5 years have passed since last update.

【C言語】fscanfとwhileを使ってテキストファイルを読み込む win7 32bit

Last updated at Posted at 2014-06-28
003.c

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

int main ()
{

    char a[20];
    FILE *f;
    char file_name[] = "test3.txt";

    /* ファイルオープン */
    if ((f = fopen(file_name, "r")) == NULL) {
        fprintf(stderr, "%s\n", "error: can't read file.");
        return EXIT_FAILURE;
    }

    while (fscanf(f, "%s", a) != EOF){
        printf("%s\n",a);
    }

    fclose(f);

    return 0;

}

test3.txt
こんにちは
こんばんは
hello world 2010
hello world 2011
hello world 2012
hello world 2013
hello world 2013
hello world 2014

環境

OS:windows7 32bit (自作機)
コンパイラ:Borland C++ 5.5.1 for Win32

解説

fscanfとwhileを使ったテキストファイルの入力
ただし、1文のサイズが20以下とする

コンパイルと実行結果

上記ソースを
C:\2014\0628\003.c
としてコンパイルして実行する。

C:\2014\0628>bcc32 003.c
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
003.c:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
C:\2014\0628>003.exe
こんにちは
こんばんは
hello
world
2010
hello
world
2011
hello
world
2012
hello
world
2013
hello
world
2013
hello
world
2014

C:\2014\0628>

参考文献

C言語関数辞典 fscanf

2
2
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
2
2