一つのファイルを使ってソートする方法は?

このQ&Aのポイント
  • 二つのファイルを使ってソートするプログラムについて質問させていただいたのですが、一つのファイルだけを使ってソートして上書きするためにはどうすればいいでしょうか?
  • ソースコードを貼っておきます。
  • #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; }
回答を見る
  • ベストアンサー

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; } よろしくお願いします。

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

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

  • ベストアンサー
  • hitomura
  • ベストアンサー率48% (325/664)
回答No.1

in を close() するタイミングが遅すぎますし、out を生成するタイミングも早すぎます。 これを変えた後で、in の生成で指定しているファイルと out の生成で指定しているファイルを同一にすればお望みの動作となります。

RJMS
質問者

お礼

できました!! 回答ありがとうございました。

関連する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; }

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

    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; }

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

    この下のプログラムは、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++]ファイル出力について

    教えてください。 コマンドプロンプトから何行か書いた文章をファイルにしたいのですが、 うまくいきません。 作ったものの結果は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; }

  • 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」は、そこで入出力されるようになっています。 以上、よろしくお願い致します。

  • プログラムが~~~!!

    こんにちは。今、プログラム書いてるんですけど目的としては、 1、テキストファイルから文字列を読み込む。(大体数万文字) 2、それを100個ずつに区切る。 3、その百個ずつを二文字ずつ読んでいって、それがgcという文字列ならばその 百個の中での割合を示す。 4、さらに次の百個を・・・・ みたいなプログラムを書いてるんですけど一応下のような形までこぎつけましたが もう、お手上げです。 どなたか、助けてください。 #include<iostream.h> #include<stdio.h> #include<string.h> int main() { char pch[3]; int count=0; FILE *fin; fin=fopen('C:\ahowaki.dat','r'); fscanf(fin,'%s',&pch); cout<<"入力\n"; while(pch){ cin.read(pch,2); if((strcmp(pch,"gc"))==0) count++; if(count>=0 && count<=25) cout<<"Under25%\n"; if(count>=26&& count<=50) cout<<"Under50\n"; if(count>=51&& count<=75) cout<<"Under75%\n"; if(count>=76&& count<=100) cout<<"Under99%\n"; else cout<<"Over100%\n"; } return 0; fclose(fin); }

  • C++の問題で質問です。

    C++のストリームを用いたファイルの入出力の問題です。 #include<iostream> #include<fstream> using namespace std; int main() { ifstream fin; fin.open("test.txt"); if(!fin){ cout<<"Cannot open the file."<<"\n"; exit(1); } char str1[16]; char str2[16]; fin>>str1>>str2; cout<<"Tow words have been written!"<<"\n"; cout<<str1<<"\n"; cout<<str2<<"\n"; fin.close(); return 0; } このプログラムを実行しようとすると In function 'int main()'; 'exit' was not declared in this scope と表示されてしまいます。 どこが間違っているのでしょうか? わかる方がいらっしゃいましたらお願いします。

  • ソートプログラム

    ファイルから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 %

専門家に質問してみよう