Mark Felone
Mark Felone

Reputation: 11

Problems with fstream

I'm writing a vector array to an ofstream file, however certain values aren't getting written, I.E.:

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

Within fx, the second half is all equal to 0. (fx[1024] through fx[2048]==0). Within test.txt however, none of these 0 values are present, on the carriage return is applied. Any thoughts? Thanks! (New to the formatting of these questions... any tips to make this more understandable would be appreciated.)

Note: I realize this program is rather redundant. The actual program has a great deal more functionality to it, this is just an area that is working incorrectly.

Upvotes: 1

Views: 721

Answers (2)

jthill
jthill

Reputation: 60497

Why screw with handroll buffers for one-offs? You can't save a millionth of what it costs to think about cycles here, there's not enough waste to recoup.

Think about eliminating needless statements and unchecked failures first.

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

Upvotes: 0

Nayana Adassuriya
Nayana Adassuriya

Reputation: 24756

Try this

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

Upvotes: 1

Related Questions