fputs関数は、ファイルに文字列を出力します。引数に指定したchar型の配列のヌル文字(’\0’)の前までの内容を出力します。
#include <stdio.h>
int fputs(const char *s, FILE *stream);
sは出力するデータが格納されている領域を指定します。
*streamはfopen関数で取得した、ファイルポインタを指定します。
戻り値として、正常に出力できた場合は負ではない値を、エラーの場合はEOFの値を返します。
プログラム 例
#include <stdio.h>
#define SIZE 1024
int main(int argc, char **argv)
{
FILE *fp_in;
FILE *fp_out;
char buff[SIZE];
int return_code = 0;
if (argc == 3) {
if ((fp_in = fopen(*(argv + 1), 'r')) != NULL) {
if ((fp_out = fopen(*(argv + 2), 'w')) != NULL) {
while(fgets(buff, SIZE, fp_in) != NULL) {
fputs(buff, fp_out);
}
fclose(fp_in);
fclose(fp_out);
}
else {
printf('出力ファイルのオープンに失敗しました\n');
return_code = 1;
}
}
else {
printf('入力ファイルのオープンに失敗しました\n');
return_code = 1;
}
}
else {
printf('実行時引数の数が不当です\n');
return_code = 2;
}
return return_code;
}
例の実行結果
$ cat temp.txt
#include <stdio.h>
int main()
{
printf('Hello World!!.\n');
return 0;
}
$
$ ./fputs.exe temp.txt temp2.txt
$
$ cat temp2.txt
#include <stdio.h>
int main()
{
printf('Hello World!!.\n');
return 0;
}
$