首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >fstream的问题

fstream的问题
EN

Stack Overflow用户
提问于 2013-03-26 08:38:26
回答 2查看 659关注 0票数 1

我正在向ofstream文件中写入一个向量数组,但是没有写入某些值,例如:

代码语言:javascript
复制
#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。(从fx1024到fx2048==0)。然而,在test.txt中,这两个0值都不存在,在回车时应用。有什么想法吗?谢谢!(不熟悉这些问题的格式...任何使这一点更容易理解的技巧都将不胜感激。)

注意:我意识到这个程序是相当多余的。实际的程序有更多的功能,这只是一个工作不正确的地方。

EN

回答 2

Stack Overflow用户

发布于 2013-03-26 09:26:46

尝尝这个

代码语言:javascript
复制
#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;
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2013-03-26 11:17:34

为什么要一次性使用手卷缓冲呢?你不能节省百万分之一的成本,在这里考虑周期,没有足够的浪费来回收。

首先考虑消除不必要的语句和未检查的失败。

代码语言:javascript
复制
#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';
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15627384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档