我有一个带有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_coordinate数组,我收到了以下输出:
-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_coordinate数组,我得到以下输出:
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。我是一名初学程序员,我花了很长时间才弄明白我做错了什么。任何帮助,将不胜感激。谢谢!
答案 0 :(得分:0)
快速修复:
if (count % 2 == 0)
{
fin >> x_coordinate[count / 2];
}
else
{
fin >> y_coordinate[count / 2];
}
count++;
E.g。当您阅读第三个值count=2
,count % 2 == 0
和count / 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;
}