Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在c++;?_C++_File_Text_Line_Edit - Fatal编程技术网

C++ 如何在c++;?

C++ 如何在c++;?,c++,file,text,line,edit,C++,File,Text,Line,Edit,我有一个如下所示的文本文件: 100 50 20 90 4.07498 0.074984 37.1704 28.1704 20.3999 14.3999 48.627 35.627 .... 我需要编辑此文件,以便所有内容都保持不变,但第一行第三项除外。输出应如下所示: 100 50 19 90 4.07498 0.074984 37.1704 28.1704 20.3999 14.3999 48.627 35.627 .... #include <fstream> #inclu

我有一个如下所示的文本文件:

100 50 20 90
4.07498 0.074984
37.1704 28.1704
20.3999 14.3999
48.627 35.627 ....
我需要编辑此文件,以便所有内容都保持不变,但第一行第三项除外。输出应如下所示:

100 50 19 90
4.07498 0.074984
37.1704 28.1704
20.3999 14.3999
48.627 35.627
....
#include <fstream>
#include <ios>
#include <iterator>
#include <vector>

int main() { 
    std::vector<double> data;

    std::fstream file("yourfile.txt", std::ios::in | std::ios::out);
    std::copy(std::istream_iterator<double>(file), 
        std::istream_iterator<double>(),
        std::back_inserter(data));
    file.clear();
    file.seekp(0);
    data[2] = 19.98;
    std::copy(data.begin(), data.end(), std::ostream_iterator<double>(file, " "));
    return 0;
}
如何在C++中完成?有人能帮我吗

谢谢, 黄

#包括
int main()
{
文件*pFile;
pFile=fopen(“example.txt”,“r+”);
fseek(pFile,7,SEEK_SET);
FPUT(“19”,pFile);
fclose(pFile);
返回0;
}
编辑:上述内容当然主要是一个笑话。真正的方法是读第一行,把它分成几个部分,更改所需的数字,写出来,然后跟着所有其他的行。如果我们知道文件的第一行包含四个整数(float?),那么这样的内容就足够了:

#include <fstream>
#include <iostream>
using namespace std;

int main ()
{
    ifstream in("in.txt");
    ofstream out("out.txt");
    float v1, v2, v3, v4;
    in >> v1 >> v2 >> v3 >> v4;
    v3 = 19.1234; // <- Do whatever you need to here.
    out << v1 << " " << v2 << " " << v3 << " " << v4;
    out << in.rdbuf();
    out.close();
    in.close();
    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
ifstream-in(“in.txt”);
输出流(“out.txt”);
浮动v1、v2、v3、v4;
在>>v1>>v2>>v3>>v4中;

v3=19.1234;//只要结果的长度与原始长度相同(或更短,并且您不介意添加空格来弥补差异),就很容易:找到要进行修改的位置,写入新数据,然后完成:

#include <fstream>
#include <ios>

int main() { 
    std::fstream file("yourfile.txt", std::ios::in | std::ios::out);
    file.seekp(7);
    file << "19";
    return 0;
}
#包括
#包括
int main(){
std::fstream文件(“yourfile.txt”,std::ios::in | std::ios::out);
文件。见第7页;

文件您可以使用std::wfstream

#include <fstream>

using namespace std;
int main() {
    wfstream file(L"example.txt", std::ios::in | std::ios::out);
    std::wstring first, second, third; // The extraction operator stops on whitespace
    file >> first >> second >> third;
    third = L"Whatever I really, really, want!";
    file << first << second << third;
    file.flush(); // Commit to OS buffers or HDD right away.
}
#包括
使用名称空间std;
int main(){
wfstream文件(L“example.txt”,std::ios::in | std::ios::out);
std::wstring first、second、third;//提取运算符在空白处停止
文件>>第一>>第二>>第三;
第三个=L“我真的真的想要什么!”;

文件您想从第三项中减去一项吗?第三项可能是负数吗?谢谢您的解决方案。但我正在寻找更“动态”的这意味着我可以将20更改为任何数字,而不覆盖最后一个数字。例如:20,19.9999,19.9998,17.36…@tsubasa:那么你应该在你的问题中指定它。添加了一个解决方案,它实际上满足额外的规范,并且可以处理比前一个更长的新数字。:)这个解决方案非常出色,到底是什么我正在寻找。非常感谢您在编辑后如果您没有添加新的行,请冷静和
file.flush();
您的帐户发生了什么?您所有的答案和问题都不见了。
#include <fstream>

using namespace std;
int main() {
    wfstream file(L"example.txt", std::ios::in | std::ios::out);
    std::wstring first, second, third; // The extraction operator stops on whitespace
    file >> first >> second >> third;
    third = L"Whatever I really, really, want!";
    file << first << second << third;
    file.flush(); // Commit to OS buffers or HDD right away.
}