输入坐标分为与文件故障的两个单独的数组

Input coordinates into two separate arrays from file trouble

本文关键字:两个 数组 单独 输入 文件 故障 坐标      更新时间:2023-10-16

我有一个带有x和y坐标的文件,并且正在尝试将x坐标输入到一个1D数组中,然后将y坐标坐落到另一个1D数组中。

数据文件处于以下格式

(x coordinate)(y coordinate)
(x coordinate)(y coordinate)
(x coordinate)(y coordinate) (x coordinate)(y coordinate) 
(x coordinate)(y coordinate) (x coordinate)(y coordinate) 

文件看起来像这样。这只是文件的一小部分,因为我们从未获得5,000点的任何东西。

5.675207 -0.571210
0.728926 0.666069
2.290909 0.751731 2.004545 0.907396
0.702893 0.646427 5.909504 -0.365045
2.082645 0.871841 5.597107 -0.633507
6.117769 -0.164663 6.091736 -0.190282
5.571074 -0.653433 4.503719 -0.978307
3.983058 -0.745620
3.670661 -0.504729
5.857438 -0.413001

到目前为止,我已经完成了以下代码:

#define _CRT_NONSTDC_NO_DEPRECATE
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char * argv[])
{
int count = 0;
ifstream fin;
ofstream fout;  
double points[5000]; 
double x_coordinate[5000] = { 0 };
double y_coordinate[5000] = { 0 };
if (argc < 3)
{
    cout << "Incorrect usage:  prog.exe filenname number" << endl;
    cout << "Exiting now, please try again." << endl;
    return -1;
}
fin.open(argv[1]);
if (!fin)
{
    cout << "Error opening file "" << argv[1] << "", exiting." << endl;
    return -1;
}
fout.open(argv[2]); 
while (fin >> points[count]) 
{ 
    if (count % 2 == 0)
    {
        fin >> x_coordinate[count]; 
    }
    else
    {
        fin >> y_coordinate[count]; 
    }
    count++; 
}
fin.close();
fout.close(); 
return 0;
}

我输出了数组的内容,只是为了确保它们被正确输入,并且对于x_coorcory数组,我收到了以下输出:

 -0.57121 0 0.751731 0 0.646427 0 0.871841 0 -0.164663 0 -0.653433 0 -0.74562 0 -0.413001 0 0.990358 0 -0.892387 0 -0.77929 0 0.835618 0 -0.999672 0 0.129798 0 -0.340688 0 -0.728578 0 -0.388408 0 0.420644 0 0.999065 0 0.556654 0 -0.435838 0 -0.779798 0 -0.710501 0 0.995461 0 -0.138933 0 0.875928 0 -0.972772 0 -0.527719 0 0.956751 0 0.372859 0 -0.987763 0 0.845169 0 -0.613152 0 0.703984

,对于y___coorcory数组,我将获得以下输出:

0 0.666069 0 0.907396 0 -0.365045 0 -0.633507 0 -0.190282 0 -0.978307 0 -0.504729 0 0.858796 0 -0.994541 0 -0.459839 0 0.849633 0 -0.996983 0 -0.99692 0 0.599134 0 0.93742 0 -0.983368 0 -0.63288 0 0.976531 0 0.34858 0 0.103944 0 -0.240329 0 0.961729 0 -0.914335 0 0.768643 0 -0.112302 0 -0.672316 0 0.954271 0 -0.89202 0 0.181224 0 0.785033 0 0.356447 0 0.467288 0 0.474704 0 -0.728022

我的鳍声明中有问题吗?我该怎么做才能修复它,并摆脱阵列中的所有其他0。我是一名初学者程序员,我花了很长时间才能弄清楚我在做什么错。任何帮助,将不胜感激。谢谢你!

如果您需要所有的值才能进入points x_coordinatey_coordinate之间分解的这些值,那么类似的事情将对您的循环主体有所作为:

if (count % 2 == 0)
{
    x_coordinate[count / 2] = points[count];
}
else
{
    y_coordinate[count / 2] = points[count];
}
count++; 

如果您只需要进入coordinate数组,请在使用的任何地方用简单的double变量替换points[count]

快速修复:

if (count % 2 == 0)
{
    fin >> x_coordinate[count / 2]; 
}
else
{
    fin >> y_coordinate[count / 2]; 
}
count++; 

例如。当您读取第三个值时,count=2,因此count % 2 == 0count / 2 == 1,它是x_coordinate[1]

更新。fin >> points[count]注意到了奇怪的事情。我会做以下操作:

while (fin >> x_coordinate[count]) {
  fin >> y_coordinate[count];
  count++;
}

完整代码(要清楚):

#define _CRT_NONSTDC_NO_DEPRECATE
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char * argv[])
{
ifstream fin;
ofstream fout;  
double x_coordinate[5000] = {}; // is enough to zeroify, but is it necessary?
double y_coordinate[5000] = {};
if (argc < 3)
{
    cout << "Incorrect usage:  prog.exe filenname number" << endl;
    cout << "Exiting now, please try again." << endl;
    return 1; // usually only small non-negative numbers like 0-127 are valid
}
fin.open(argv[1]);
if (!fin)
{
    cout << "Error opening file "" << argv[1] << "", exiting." << endl;
    return 1;
}
fout.open(argv[2]); 
if (!fout) // also needed
{
    cout << "Error opening file "" << argv[2] << "", exiting." << endl;
    return 1;
}  
int count = 0; // declaration closer to the place where the variable is used
while (fin >> x_coordinate[count]) {
    fin >> y_coordinate[count];
    count++;
}
for (int i = 0; i < count; i++) {
    fout << x_coordinate[i] << ' ' << y_coordinate[i] << endl;
}
fin.close();
fout.close(); 
return 0;
}