简体   繁体   English

如何使用 CPP 在文本文件中写入

[英]How I can write in text file using CPP

Actually, I has four dimensional data with value into a text file "fourdimensionalM.txt" and I want to read data from txt file "fourdimensionalM.txt" which has实际上,我将具有值的四维数据放入文本文件“fourdimensionalM.txt”,我想从 txt 文件“fourdimensionalM.txt”中读取数据,该文件具有

0 0 0 2 1
0 0 2 0 2
0 1 0 0 3
0 1 1 1 4
0 1 2 0 5

and want to write into new txt file the where the four dimensional matrix has no number.并想将四维矩阵没有数字的地方写入新的txt文件。 I write the code but this only save zero value into value position five column but I want to write non zero value which has into txt file "fourdimensionalM.txt" and save it on new txt file.我编写代码,但这仅将零值保存到值位置五列,但我想将非零值写入 txt 文件“fourdimensionalM.txt”并将其保存在新的 txt 文件中。

#include <iostream>
#include<vector>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
    int X=4,Y=4,Z=4,M=4;
    /*printf("Enter size of 1st dimension X= ");
    scanf("%d",&X);
    printf("\n Enter size of 2nd dimension Y= ");
    scanf("%d",&Y);
    printf("\n Enter size of 3rd dimension Z= ");
    scanf("%d",&Z);
    printf("\n Enter size of 4th dimension M= ");
    scanf("%d",&M);
    printf("\n"); */
    srand((unsigned) time(0));
    ofstream fourDtensor;
    fourDtensor.open("fourDtensor.txt");

    ifstream four_dimension_tensor("uber3.txt");
    int a,b,c,d,vl,new_x_1,new_x_2,flag;
    cout<<"Starting"<<endl;


    for (int i = 0; i < X; i++)
    {
        //cout<<i<<endl;
        for (int j = 0; j < Y; j++)
            {
                for (int k = 0; k < Z; k++)
                {
                    for(int l=0; l < M; l++)
                    {   flag=0;
                        cout<<"before"<<endl;
                        while(!four_dimension_tensor.eof()){
                            four_dimension_tensor>>a>>b>>c>>d>>vl;
                            cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<vl<<endl;
                                if(a==i && b==j && c==k && d==l){
                                    flag=1;
                        }

                    }
                    cout<<"after"<<endl;
                    if(flag==1){

                    fourDtensor << i<< " " << j << " " << k << " "<< l << " " << vl ;
                                    fourDtensor << endl;
                                }
                                else{
                                    fourDtensor << i<< " " << j << " " << k << " "<< l << " " << 0 ;
                                    fourDtensor << endl;
                }
            }
    }

}

    }

    return 0;

}

If I understand your question correctly, you are given a file with select points values for R^4 and you need to produce the full hypercube with zeroes for places where you do not have a point.如果我正确理解你的问题,你会得到一个包含 R^4 选择点值的文件,你需要为你没有点的地方生成带有零的完整超立方体。

If you can assume the input file is sorted lexicographically, you can do the following:如果您可以假设输入文件按字典顺序排序,则可以执行以下操作:

int ti, tj, tk, tl, tv; // target coordinates and value

ifstream targets("uber3.txt");
void advance() {
  targets >> ti >> tj >> tk >> tl >> tv;
  if (targets.eof()) {
    ti = tj = tk = tk = -1;
  }
}

int get_value(int i, int j, int k, int l) {
  if (i == ti && j == tj && k == tk && l == tl) {
    // the arguments match the current target
    int ret = tv; // advance() overwrites tv, so take a copy
    advance();
    return ret;
  } else {
    return 0;
  }
}

and then the core of your main loop (ie inside the four nested for -loops) becomes simple:然后你的主循环的核心(即在四个嵌套的for循环中)变得简单:

fourDtensor << i << " " << j << " " << k << " " << l << " " << get_value(i,j,k,l);

Don't forget to call advance() once at the start of the program!不要忘记在程序开始时调用一次advance()

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

相关问题 如何将控制台 output 写入 cpp 中的文本文件? - How to write console output to a text file in cpp? 我如何编写一个脚本来创建一个 .cpp 文件,其中已经包含所有标题内容? - How can i write a script that creates a .cpp file with all the header stuff already in it? 如何在没有缓冲的情况下使用 parquet-cpp 编写流式/面向行的数据? - How can I write streaming/row-oriented data using parquet-cpp without buffering? 如何使用cpp将类的成员写入二进制文件? - How to write members of a class to a binary file using cpp? 每次写入文件时,如何在前面附加文本? - How can i append text in front everytime i write in a file? 如何使用 avrdude 从 cpp 文件生成 hex 文件 - how can i generate hex file from cpp file with avrdude 如何检查是否正在使用预处理器在 Objective-C++ 项目中编译 cpp 文件? - How can I check if a cpp file is being compiled in an Objective-C++ project using the preprocessor? 我可以使用 .cpp 和 .h 文件制作 .pro 文件吗 - Can I make .pro file using .cpp and .h files 如何在另一个 cpp 文件中使用一个 cpp 文件中的 function? - how can I use a function from one cpp file in another cpp file? 我可以执行gzseek更新使用gzwrite(CPP)压缩的文件吗? - can i perform gzseek to update a file compressed using gzwrite (CPP)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM