首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >CSV文件未从buffor获取数据

CSV文件未从buffor获取数据
EN

Stack Overflow用户
提问于 2019-11-13 11:24:45
回答 1查看 17关注 0票数 0

我正在学习用C++编写代码,并且正在做一些文件操作的示例。将文本数据另存为.txt文件时,将其发送到文件不会出现问题。

然而,我更进一步,在csv文件上进行了实验:

代码语言:javascript
代码运行次数:0
运行
复制
#include <iostream>
#include <string>
#include <fstream>
#include <math.h>
#include <stdio.h>

using namespace std;

 int main ()
{
int num = 0;

fstream file;

string file_name;

cout << "Input file name: ";
cin>>file_name;
file.open(file_name.c_str());

if (file.fail())
{
    cout << "Unable to open file - try again" << endl;
    main ();
} else
{

file << "VALUE" << "\t" << "1/x" << "\t" << "sqrt" << endl;


while (num < 200)
{
    file << num << "\t" << 1/num << "\t" << sqrt(num) << endl;
    num ++;
}

file.close();
return 0;
}
}

当我检查文件时-只有标题:

enter image description here

我应该怎么做才能将这些数学运算的结果写入这个csv文件?

EN

回答 1

Stack Overflow用户

发布于 2019-11-13 11:33:52

请看我关于被零除的评论。另外,不要使用整型除法。变化

代码语言:javascript
代码运行次数:0
运行
复制
while (num < 200)
{
    file << num << "\t" << 1/num << "\t" << sqrt(num) << endl;
    num ++;
}

代码语言:javascript
代码运行次数:0
运行
复制
while(num < 200)
{
    num++;
    file << num << "\t" << 1.0 / num << "\t" << sqrt(num) << endl;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58829874

复制
相关文章

相似问题

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