1

ベクトル配列を ofstream ファイルに書き込んでいますが、特定の値が書き込まれません。IE:

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main (){
    char * hold = new char [100];
    vector<double> fx(2049);
    ifstream inputFile;
    ofstream myFile;
    inputFile.open("data.txt");
    myFile.open("test.txt");
    for (int c=0; c<2049; c++){
         inputFile.getline(hold, 100);
         fx[c] = atof(hold);
    }
    for (int c=0; c<2049; c++){
         myFile << fx[c] << "\n";
    }
}

fx 内では、後半はすべて 0 です (fx[1024] から fx[2048]==0)。ただし、test.txt 内では、これらの 0 値は存在せず、キャリッジ リターンが適用されます。何かご意見は?ありがとう!(これらの質問の形式は初めてです...これをより理解しやすくするためのヒントをいただければ幸いです。)

注: このプログラムはかなり冗長であることを認識しています。実際のプログラムにはさらに多くの機能がありますが、これは単に正しく機能していない領域です。

4

2 に答える 2

1

これを試して

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cstdlib>

#define MAX_FILE_LINES 2048


using namespace std;

//genarate random double number
double fRand()
{
    double fMin = 100, fMax = 200;
    double f = (double)rand();    
    return fMin + (f / (fMax - fMin));  
}

//init file (if you need to create sample file with list of double numbers, you can use this function)
void fileInit(){    
    ofstream sourceFile;    
    sourceFile.open("D:\\source.txt");
    if (sourceFile.is_open())
    {
        for (int i=0; i<MAX_FILE_LINES; i++){
            sourceFile << fRand() << endl;
        }
    }
}


int main (){
    string buffer;
    vector<double> fx(MAX_FILE_LINES);  
    ifstream sourceFile;
    ofstream destinationFile;
    sourceFile.open("D:\\source.txt");
    destinationFile.open("D:\\destination.txt");

    //reading file lines to vector
    int lineCount =0;
    if (sourceFile.is_open())
      {
        while ( sourceFile.good() )
        {
          getline (sourceFile,buffer);
          fx[lineCount] = atof(buffer.c_str());
          lineCount++;
          if (lineCount == (MAX_FILE_LINES-1)){
              break;
          }
        }
        sourceFile.close();
      }

    //write lines to new file
    if (destinationFile.is_open())
    {
        for (int i=0; i<MAX_FILE_LINES; i++){
         destinationFile << fx[i] << endl;
        }
    }
}
于 2013-03-26T01:26:46.217 に答える
0

1 回限りのハンドロール バッファーを使用する理由は何ですか? ここでサイクルについて考えるのにかかる費用の 100 万分の 1 を節約することはできません。回収するのに十分な無駄がありません。

まず、不要なステートメントとチェックされていない障害を排除することを考えてください。

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
    vector<float> data;
    {
        ifstream ids("source.txt",ios_base::in);
        int linenr = 0;
        for ( string line ; getline(ids,line) ; ) {
            ++linenr;
            decltype(data)::value_type x;
            istringstream s(line);
            if ( s >> x ) 
                data.push_back(x);
            else
                cerr << "crap line "<<linenr<<" ignored: " << line << '\n';
        }
    }
    ofstream ods("object.txt");
    for ( auto x : data )
        ods << x << '\n';
}
于 2013-03-26T03:17:33.637 に答える