简体   繁体   English

在C ++中将整数写入.txt文件

[英]Writing Integers to a .txt file in c++

I am new to C++ and want to write data(integers) on a .txt file. 我是C ++的新手,想在.txt文件中写入数据(整数)。 The data is in three or more columns that can be read later for further usage. 数据位于三列或更多列中,以后可以读取以进一步使用。 I have created successfully a reading project but for the writing one the file is created but it is blank. 我已经成功创建了一个阅读项目,但是对于写作一个项目,该文件已创建,但是它是空白的。 I have tried code samples from multiple sites but is not helping. 我尝试了来自多个站点的代码示例,但没有帮助。 I have to write results from three different equations as can be seen from the code. 从代码可以看出,我必须根据三个不同的方程式编写结果。

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

int main ()
{
    int i, x, y;
    ofstream myfile;
    myfile.open ("example1.txt");
    for (int j; j < 3; j++)
    {
        myfile << i ;
        myfile << " " << x;
        myfile << " " << y << endl;
        i++;
        x = x + 2;
        y = x + 1;
    }
    myfile.close();
    return 0;
}

Kindly point out the error or suggest a solution. 请指出错误或提出解决方案。

std::ofstream ofile;
ofile.open("example.txt", std::ios::app); //app is append which means it will put the text at the end

int i{ 0 };
int x{ 0 };
int y{ 0 };

for (int j{ 0 }; j < 3; ++j)
   {
     ofile << i << " " << x << " " << y << std::endl;
     i++;
     x += 2; //Shorter this way
     y = x + 1;
   }
ofile.close()

Try this: it will write the integers how you want them, i tested it myself. 试试看:它将以您想要的方式写入整数,我自己对其进行了测试。

Basically what I changed is firstly, I initialized all the variables to 0 so you get the right results and with the ofstream, I just set it to std::ios::app, which stands for appending (it basically will write the integers at the end of the file always. I also just made the writing into one line only. 基本上我所做的更改是,首先,我将所有变量初始化为0,以便获得正确的结果,并使用ofstream,我将其设置为std :: ios :: app,它表示追加(它基本上会在以下位置写入整数)文件的末尾,我也只写了一行。

Initialize j before use. 使用前初始化j

So, your for loop will be: 因此,您的for循环将是:

for (int j = 0; j < 3; j++)
{
    // ...
}

You need to initialize i , x , and y with default values eg 0 . 您需要使用默认值(例如0初始化ixy
Otherwise, you'll get garbage values. 否则,您将获得垃圾值。

Your problem is not concerning "writing integer to a file". 您的问题与“将整数写入文件”无关。 Your problem is that j is not initialized and then the code never enters the loop. 您的问题是j未初始化,因此代码从不进入循环。

I modified you code by initializing j at the start of the loop and the file is written succesfully 我通过在循环开始时初始化j修改了您的代码,文件成功写入

#include<iostream>
#include<sstream>
#include<fstream>
#include<iomanip>


using namespace std;

int main ()
{
    int i=0, x=0, y=0;
    ofstream myfile;
    myfile.open ("example1.txt");

    for (int j=0; j < 3; j++)
    {
        myfile  << i ;
        myfile  << " " << x;
        myfile  << " " << y << endl;
        i++;
        x = x + 2;
        y = x + 1;
    }
    myfile.close();
    return 0;
}

It outputs a file named "example 1.txt" and containing the following: 它输出一个名为“ example 1.txt”的文件,其中包含以下内容:

0 0 0
1 2 3
2 4 5

If it happens that you do not initialize i, x and y. 如果发生这种情况,请不要初始化i,x和y。 The code will write into the file anyway, but it will write garbage values as below: 该代码无论如何都会写入文件,但是它将写入如下的垃圾值:

1984827746 -2 314951928
1984827747 0 1
1984827748 2 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM