读取包含小数的文件时出现问题
我在试图弄清楚为什么我的文件返回 0 而不是文件内的数字时遇到问题,这是我在 C++ 中读取文件时所做的代码:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string cfile;
int cnum1,cnum2,cnum3,cnum4;
bool fired = false;
/*
*
*/
void printMatrix(double **x, int n)
{
int size = n;
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
std:: cout << x[i][j] << " " ;
}
std:: cout << std::endl;
}
}
void readFile(string file,double **x, int n)
{
std::ifstream myfile(file.c_str());
int size = n;
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
myfile >> x[i][j];
}
}
}
void GetCommandLineArguments(int argc, char **argv,string &file, int &n, int &k, int &m, int &i)
{
if( argc == 6 )
{
cfile = argv[1];
cnum1 = atoi(argv[2]);
cnum2 = atoi(argv[3]);
cnum3 = atoi(argv[4]);
cnum4 = atoi(argv[5]);
}
file = cfile;
n = cnum1;
k = cnum2;
m = cnum3;
i = cnum4;
}
int main(int argc, char** argv) {
int k; //Factor of n
int m; //Innner matrix size
int i; //Iteration
int n; //Matrix Size
string file;
GetCommandLineArguments(argc, argv, file, n, k, m, i);
double **matrix;
matrix = new double*[n];
for(int i = 0; i<n; i++)
matrix[i] = new double[n];
for(int j=0; j<n; j++)
for(int i=0; i<n;i++)
matrix[i][j] = 0;
readFile(file, matrix, n);
printMatrix(matrix, n);
return 0;
}
这是我的文件的示例,其中包含我想要提取的值来自它:
20.0
20.0
20.0
20.0
20.0
200.0
20.0
200.0
希望有人可以帮助我,因为我研究了一些有关此问题的信息,但没有真正找到解决方案。
Im having a problem trying to figure out why my file is returning 0's instead of the numbers inside the file, here is the code I did in C++ on reading a file:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string cfile;
int cnum1,cnum2,cnum3,cnum4;
bool fired = false;
/*
*
*/
void printMatrix(double **x, int n)
{
int size = n;
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
std:: cout << x[i][j] << " " ;
}
std:: cout << std::endl;
}
}
void readFile(string file,double **x, int n)
{
std::ifstream myfile(file.c_str());
int size = n;
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
myfile >> x[i][j];
}
}
}
void GetCommandLineArguments(int argc, char **argv,string &file, int &n, int &k, int &m, int &i)
{
if( argc == 6 )
{
cfile = argv[1];
cnum1 = atoi(argv[2]);
cnum2 = atoi(argv[3]);
cnum3 = atoi(argv[4]);
cnum4 = atoi(argv[5]);
}
file = cfile;
n = cnum1;
k = cnum2;
m = cnum3;
i = cnum4;
}
int main(int argc, char** argv) {
int k; //Factor of n
int m; //Innner matrix size
int i; //Iteration
int n; //Matrix Size
string file;
GetCommandLineArguments(argc, argv, file, n, k, m, i);
double **matrix;
matrix = new double*[n];
for(int i = 0; i<n; i++)
matrix[i] = new double[n];
for(int j=0; j<n; j++)
for(int i=0; i<n;i++)
matrix[i][j] = 0;
readFile(file, matrix, n);
printMatrix(matrix, n);
return 0;
}
And here is a sample of my file containing the values I want to extract from it:
20.0
20.0
20.0
20.0
20.0
200.0
20.0
200.0
Hope someone can help me out since I researched some info about this and didn't really find a solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的读取和打印代码似乎可以工作,但您的命令行读取代码可能存在一些问题。
我在没有获取命令行参数的情况下运行了您的代码。以下代码几乎是从 main 中复制粘贴的,减去获取命令行参数。
根据您提供的输入,我得到输出:
但是查看您的命令行参数读取代码,我可以看到一些潜在的问题。首先使用 atoi()。当atoi失败时,它返回0。你知道这段代码有效吗?一切都正确初始化了吗?或者 atoi 在输入上失败,导致 n 为 0,从而导致无法读入任何内容? (您可能希望研究 stringstreams 来执行此类操作)。
此外,当 argc 不是 6 时,您会默默地失败并从未初始化的全局内存中读取。这段记忆是垃圾。你知道这不会发生吗?如果你只是这样做:
那么 5 不会从命令行读取,因为 argc 不是 6。你是否总是像测试时那样传递 6 个参数?也许不是,因为在当前状态下,您的代码实际上只需要文件名和大小。
最重要的是,看看您是否从命令行得到了您所期望的结果。
PS 这是 g++:
Your reading and printing code appears to work, but your command line reading code may have some problems.
I ran your code without getting command line arguments. The following code, is pretty much copy-pasted from your main minus getting command line args.
With the input you provide, I get the output:
However looking at your command line arg reading code, I can see some potential problems. First you use atoi(). When atoi fails, it returns 0. Do you know that this code is working? Is everything getting initialized correctly? Or is atoi failing on the input, causing n to be 0 and therefore causing nothing to be read in? (You may wish to look into stringstreams for doing this kind of thing).
Moreover, when argc is not 6, you're silently failing and reading from uninitialized global memory. This memory is garbage. Do you know that this is not happening? If you're just doing:
then 5 isn't going to be read from the command line because argc is not 6. Are you always passing 6 arguments like you should when testing? Maybe not, cause in its current state your code really only needs the file name and size.
Most important thing, see if you're getting what you expect from the command line.
PS This is g++:
我建议像这样进行修复,使事情变得更加健壮:
I suggest a fixup like this, making things more robust:
您的输入运算符将读取数字,但在文件中保留换行符。
请参阅此答案,了解如何在读取值后
ignore()
行尾为什么我们会在读取后调用 cin.clear() 和 cin.ignore()输入?
Your input operator will read the numbers but leave the line breaks in the file.
See this answer about how to
ignore()
the end-of-line after reading your valuesWhy would we call cin.clear() and cin.ignore() after reading input?