[英]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.