前提
C++にて、txtファイルへの文字列の書き込みをしようとしています。ライブラリのfstream.hを取り入れているのですが、ofstreamやfoutが認識されません。
実現したいこと
pmonf-1.txtというファイルへ、単に以下の数値を入力するだけです。
long型整数172250,112007,111539,88032,63753,161787,275029,62895,160550,126734
発生している問題・エラーメッセージ
以下の通り、ofstreamやfoutが認識されません。
PS C:\Users\motch\java> g++ filecontrol_C++_20230102.cpp -o filecontrol_C++_20230102 ; .\filecontrol_C++_20230102 filecontrol_C++_20230102.cpp: In function 'int main()': filecontrol_C++_20230102.cpp:10:17: error: 'endl' was not declared in this scope fout<<x[i]<<endl; ^~~~ filecontrol_C++_20230102.cpp:10:17: note: suggested alternative: In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\istream:39:0, from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\fstream:38, from filecontrol_C++_20230102.cpp:1: c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ostream:590:5: note: 'std::endl' endl(basic_ostream<_CharT, _Traits>& __os) ^~~~ .\filecontrol_C++_20230102 : 用語 '.\filecontrol_C++_20230102' は、コマンドレット、関数、スクリプト ファイル、または操作可能なプログラムの名前として認識されません。名前が正しく記述されて いることを確認し、パスが含まれている場合はそのパスが正しいことを確認してから、再試行してください。 発生場所 行:1 文字:64 + ... 20230102.cpp -o filecontrol_C++_20230102 ; .\filecontrol_C++_20230102 + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (.\filecontrol_C++_20230102:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\motch\java>
該当のソースコード
C++
1#include<fstream> 2#include<stdlib.h> 3 4int main(void){ 5 long x[]={172250,112007,111539,88032,63753,161787,275029,62895,160550,126734}; 6 char fname[]="pmonf-1.txt"; 7 std::ofstream fout(fname); 8 int i,n=10; 9 for(i=0;i<n;i++) 10 fout<<x[i]<<endl; 11 fout.close(); 12}
試したこと
教科書のサンプルソースコードの写しに間違いがないか確認したり、試しにstdlib.hを入れたりしてみたのですがなかなかうまくいきません。
以上よろしくお願いいたします。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。