ソートプログラムでの出力ファイルへの書き込みエラー

このQ&Aのポイント
  • 自作のソートプログラムで、出力ファイルに結果を書き込めません。原因を教えてください。
  • ソート後の結果を出力ファイルに書き込みたいのですが、エラーが発生します。
  • どこが問題なのかわからず、ソートプログラムでの出力ファイルへの書き込みができません。
回答を見る
  • ベストアンサー

Sortプログラムについて

自分で作成したプログラムなのですが、ソートしたあと、出力ファイルに書き込みたいのですが、書き込めません。 どこが、悪いのかわからないので教えてください。 ところどころに、出力がありますが、それはトレースしたやつなので、それはほっといてくださって構いません。 下にプログラムを載せます。 よろしくお願いします。 #include <iostream> #include <fstream> #include <list> #include <string> using namespace std; int main() { char Str[255]; list<string> str; ifstream in("ttest", ios::binary | ios::in); if (!in){ cout << "入力ファイルが読み込めない" << endl; exit(1); } ofstream out("out", ios::binary | ios::out); if (!out){ cout << "出力ファイルが読み込めない" << endl; exit(1); } while (!in.eof()){ //!!!! in.getline(Str,255); str.push_back(Str); } str.sort(); list<string>::iterator p; p = str.begin(); while (!in.eof()){ cout << *p; out << *p++; } in.close(); out.close(); getchar(); return 0; }

  • RJMS
  • お礼率70% (58/82)

質問者が選んだベストアンサー

  • ベストアンサー
  • f272
  • ベストアンサー率46% (8012/17125)
回答No.1

> list<string>::iterator p; > p = str.begin(); > while (!in.eof()){ //------------ここを、よく考えてみたらどうかな > cout << *p; > out << *p++; > }

RJMS
質問者

お礼

わかりました!!! なぜか、入力ファイルをやっていたんですね。 正解は while(p!=str.end())ですね。 ありがとうございました!!

RJMS
質問者

補足

実行してみて、そこが確かに実行されてないのは分っているのですが…。 お手上げですね…。

関連するQ&A

  • Sortプログラムについて2

    えっと、前回もSortのプログラムについて、質問させていただいたのですが、このプログラムでは、二つのファイルを使ってソートしてるのですが、一つのファイルだけを使って、それに上書きするためにはどうするればいいでしょうか? 下に、ソースを貼っておきます。 #include <iostream> #include <fstream> #include <list> #include <string> using namespace std; int main() { char Str[255]; list<string> str; int count = 0; ifstream in("ttest", ios::binary | ios::in); if (!in){ cout << "入力ファイルが読み込めない" << endl; exit(1); } ofstream out("out", ios::binary | ios::out); if (!out){ cout << "出力ファイルが読み込めない" << endl; exit(1); } while (!in.eof()){ //!!!! in.getline(Str,255); str.push_back(Str); count++; } str.sort(); list<string>::iterator p; p = str.begin(); while(p!=str.end()){ out << *p << endl; p++; } in.close(); out.close(); getchar(); return 0; } よろしくお願いします。

  • 自分で作成したプログラムについて…。

    visual stdio 2013 デスクトップ版のものを使い、下に書くプログラムを作ったのですが、 visual stdio 2013では、ファイルが開けませんとなり、途中で、終了してしまうのですが、mingwでは、ちゃんとファイルが開けて、プログラムが、最後まで動作するのですが、なぜでしょうか? 原因を詳しく教えてください。また、改善方法も教えてください。 プログラム #include <iostream> #include <fstream> #include <ctime> #include <cstdlib> #include <list> #include <string> using namespace std; int main() { srand((unsigned)time(NULL)); list<string> str; char sstr[255]; int count = 0; int i; ifstream in("ttest", ios::in | ios::binary); if (!in){ cout << "入力ファイルが開けません\n"; getchar(); return 1; } while (!in.eof()){ in.getline(sstr, 255); str.push_back(sstr); count++; } i = rand() % count ; list<string>::iterator p; p = str.begin(); for (int j = 2; j <= i; j++)p++; cout << *p; cout << endl; getchar(); return 0; }

  • [C++]ファイル出力について

    教えてください。 コマンドプロンプトから何行か書いた文章をファイルにしたいのですが、 うまくいきません。 作ったものの結果は1行1文字で出力されて、eofでうまく 終わってくれません。 コマンドプロンプトで入力(改行)された通りにファイルに出力 できるようにしたいので、どこがおかしいのか指摘お願いします。 #include <iostream> #include <fstream> #include <stdlib.h> #include <string.h> #include <string> using namespace std; int main(int argc, char *argv[]) { char str[100]; int i; i = 0; if (argc <= 1) { cerr << "Need filename" << endl; exit (1); } ifstream ifs(argv[1], ios::in); if (ifs) { cerr << "Caution" << argv[1] << " already exists " << endl; cerr << " Specify a different filename " << endl; exit (2); } ofstream ofs(argv[1], ios::out); if (!ofs) { cerr << " Unable to write to " << argv[1] << endl; exit (3); } while(str[i] !=EOF) { cin >> str[i]; ofs << str[i] << endl; i++; } return 0; }

  • プログラムの動作の仕方

    この下のプログラムは、WRITE <ファイル名>をコマンド行で入力すると、動作するプログラムなのですが、この通りにWRITE <test>としてもできません。 やり方を教えてください. #include <iostream> #include <fstream> using namespace std; int main(int argc,char *argv[]) { if(argc!=2){ cout << "使い方:WRITE<ファイル名>" << endl; return 1; } ofstream out(argv[1]); //出力ファイル if(!out){ cout << "出力ファイルが開けません" << endl; return 1; } char str[80]; cout << "文字列をディスクに書き込み、$で停止します" << endl; do{ cout << ": "; cin >> str; out << str << endl; }while(*str!='$'); out.close(); return 0; } お願いします。

  • C++文字列の挿入、結合のコードについて

    実行結果のような出力をするためには、 以下のコードの(ウ)(エ)(オ)の部分には何を入れたらよいのでしょうか? よろしくお願いします。 #include <iostream> #include <string> using namespace std; int main( ) { string str1="ABCDEF"; string str2="0123"; string str3; string q; do { (ウ) ; cout << str3 << endl; (エ) ; cout << str1 << endl; cout << "quit?"; cin >> q; } while ( (オ) ); cout << "終了" << endl; return 0; } <実行結果(出力結果)> ABCDEF0123 ABC123DEF quit?q ABC123DEF0123 ABC123123DEF quit?qu ABC123123DEF0123 ABC123123123DEF quit?quit 終了

  • ofstream::getがおかしい!

    ofstream::getの動作がおかしいのです #include <string> #include <iostream> #include <fstream> using namespace std; void main(void) { ifstream ifs; ofstream ofs; string str; char c; str="abc"; cout<<"before str: "<<str<<endl<<endl; ofs.open("gomi");ofs<<str;ofs.close(); ifs.open("gomi");for(str="";ifs.eof()==0;str+=c)ifs.get(c);ifs.close(); cout<<"after str: "<<str<<endl<<endl; } の結果が before str: abc after str: abcc になります 最後のcは何でつくのでしょうか? 回避する方法を教えてください

  • プログラムの連続実行(VC++ 2010)

    こんにちは。 現在、「Visual C++ 2010 Express」を使って、コンソールアプリケーションの開発について勉強しています。 どうしてもわからないことが出てきたので、質問させて下さい。 今、Sという名前のソリューションに、P1とP2という、2つのプロジェクトが含まれているとします。 P1をビルドして作成されたP1.exeは、1つのファイルに、適当なデータを出力します。 P2をビルドして作成されたP2.exeは、そのファイルの内容を読み取って、標準出力に出力します。 Visual C++ 2010 Expressでは、[Ctrl]+[F5]で、作成されたexeファイルを実行できるのですが、 どうやら、スタートアッププロジェクトのexeファイルしか実行されないようなんです。 [Ctrl]+[F5]によって、P1.exeが実行された後に、P2.exeが実行されるようにするには、どうすればよいのでしょうか? プロジェクトやソリューションのプロパティをいじってみたのですが、 どうも上手くいかなかったので、質問させて頂きました。 一応、P1とP2のソースファイルの内容を、以下に載せておきます。 ・P1.cpp ----------------------------------------------- int main(int argc, char *argv[]) { char str[1000]; //出力用にファイルをオープンする。 ofstream fw("file.txt"); if(!fw){ cout<<"「file.txt」が開けない!\n"; exit(1); } //ファイルにデータを書き込む。 cout<<"出力ファイルに書き込む文字列を入力せよ。"<<endl; cin>>str; fw<<str<<endl; fw<<100<<endl; fw<<200<<endl; fw.close(); return 0; } ----------------------------------------------- ・P2.cpp ----------------------------------------------- int main(int argc, char *argv[]) { char ch; //読み取り用にファイルをオープンする。 ifstream fr("file.txt"); if(!fr){ cout<<"「file.txt」が開けない!\n"; exit(1); } cout<<"file.txtの内容を読み込み、以下に表示する。\n"; while( fr.get(ch) ){ cout<<ch; } return 0; } ----------------------------------------------- ちなみに、各プロジェクトの「作業ディレクトリ」は、 共通のディレクトリに設定しているので、 「file.txt」は、そこで入出力されるようになっています。 以上、よろしくお願い致します。

  • wstringの主力

    wstring (または wchar_t)の出力がうまくいかず困っています。 基本的な質問になるかと思いますがよろしくお願いします。 #include <string> #include <wchar.h> using namespace std; int main() {  wstring str = L"test";  wprintf(L"%s \n", str.c_str());//(1)  wprintf(L"%s \n", L"test");//(2)  wprintf(L"test \n");//(3)  // cout << str <<endl; //(4)  cout << str.c_str() <<endl;//(5) } 出力結果 (1) t (2) t (3) test (4) コンパイルエラー (5) 望むのは当然(3)の出力です。 web上で(1)や(4)のような記述はみかけたのですが、(4)に関してはなぜコンパイルエラーになるかもわからず。。 os linux. gcc 4.1.1

  • ソートプログラム

    ファイルから10個の整数が 29 45 11 2 86 91 33 62 77 59 のように一行に1個の形式で格納されている。このファイルから10個の整数を読み込み、選択法でソートし、この数字を小さい順に表示するプログラムを作成したのですが、ソート部分がうまくできません。どなたかどうすれば改善できるか教えてください。 <プログラム> #include<stdio.h> #include<stdlib.h> #define MAXN 100 int A[MAXN]; main() { _inputdata(); _selectionsort(1,10); _printdata(); } selectionsort(p,q) ___int p, q; { _int i, j, cmin, temp; _for(j = p; j <= q; j++){ __cmin = j; /* A[cmin]が現在の最小値 */ __for(i = j+1; i <= q; i++) ___if(A[cmin] > A[i]) cmin = i; __/* A[j]とA[cmin]との入れ換え操作 */ __swap(j, cmin); _} } swap(int x, int y) { _int temp; _temp = x; _x = y; _y = temp; } inputdata() { _FILE *fp; _if((fp=fopen("integers10.dat", "r"))==NULL) { __printf("ファイルが見つかりません: integers10.dat\n"); __exit(EXIT_FAILURE); _} _printf("データを入力\n"); _while(fscanf(fp, "%s", A)!=EOF) { ____printf("%s\n",A); _} _fclose(fp); } <コンパイル→実行> % gcc -o sort1 sort1.c % ./sort1 データを入力 29 45 11 2 86 91 33 62 77 59 ソート済みデータ 0 0 0 0 0 0 0 0 0 0 %

  • このプログラムを...

    現在テスト用のプログラムを書いているのですが、なかなか動いてくれません。 ソース: #include <iostream> #include <iomanip> #include <string> using namespace std; struct MovieData { string title; string director; int year; int time; }; const int ARRAY_SIZE = 2; void showItem(MovieData[ARRAY_SIZE]); int main() { MovieData part[ARRAY_SIZE]; for (int cnt = 0; cnt < ARRAY_SIZE; cnt++) { cout << "Enter the movie's name: "; cin >> part[cnt].title; cout << "Enter the director's name: "; cin >> part[cnt].director; cout << "Enter the year of release: "; cin >> part[cnt].year; cout << "Enter the movie's running time: "; cin >> part[cnt].time; } showItem(part); return 0; } void showItem(MovieData part) { cout << fixed << showpoint << setprecision(2); for (int cnt2 = 0; cnt2 < ARRAY_SIZE; cnt2++) { cout << "Name of movie: " << part[cnt2].title << endl; cout << "Director: " << part[cnt2].director << endl; cout << "Year of released: " << part[cnt2].year << endl; cout << "Running time: " << part[cnt2].time << endl; } } どこが間違っているのでしょうか?宜しくお願いします。

専門家に質問してみよう