读取包含小数的文件时出现问题

发布于 2024-11-04 09:32:59 字数 2122 浏览 3 评论 0原文

我在试图弄清楚为什么我的文件返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟雨扶苏 2024-11-11 09:32:59

您的读取和打印代码似乎可以工作,但您的命令行读取代码可能存在一些问题。

我在没有获取命令行参数的情况下运行了您的代码。以下代码几乎是从 main 中复制粘贴的,减去获取命令行参数。

int main()
{
    double **matrix;
    std::string file = "test.dat";
    int n = 5;

    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 20 20 20 20
200 20 200 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

但是查看您的命令行参数读取代码,我可以看到一些潜在的问题。首先使用 atoi()。当atoi失败时,它返回0。你知道这段代码有效吗?一切都正确初始化了吗?或者 atoi 在输入上失败,导致 n 为 0,从而导致无法读入任何内容? (您可能希望研究 stringstreams 来执行此类操作)。

此外,当 argc 不是 6 时,您会默默地失败并从未初始化的全局内存中读取。这段记忆是垃圾。你知道这不会发生吗?如果你只是这样做:

  your.exe test.dat 5

那么 5 不会从命令行读取,因为 argc 不是 6。你是否总是像测试时那样传递 6 个参数?也许不是,因为在当前状态下,您的代码实际上只需要文件名和大小。

最重要的是,看看您是否从命令行得到了您所期望的结果。

PS 这是 g++:

$ g++ --版本
g++(Ubuntu/Linaro 4.4.4-14ubuntu5)
4.4.5 版权所有 (C) 2010 Free Software Foundation, Inc. 这是免费的
软件;查看复制来源
状况。没有保修;不是
即使是为了适销性或健身性
出于特定目的。

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.

int main()
{
    double **matrix;
    std::string file = "test.dat";
    int n = 5;

    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;
}

With the input you provide, I get the output:

20 20 20 20 20
200 20 200 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

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:

  your.exe test.dat 5

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++:

$ g++ --version
g++ (Ubuntu/Linaro 4.4.4-14ubuntu5)
4.4.5 Copyright (C) 2010 Free Software Foundation, Inc. This is free
software; see the source for copying
conditions. There is NO warranty; not
even for MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.

[旋木] 2024-11-11 09:32:59

我建议像这样进行修复,使事情变得更加健壮:

#include <fstream>
#include <iostream>
#include <string>
#include <stdexcept>

template <size_t size>
    void readFile(std::string file, double (&x)[size][size])
{
    std::ifstream myfile(file.c_str());

    for(int i=0; i<size; i++)
    {
        for(int j=0; j<size; j++)
        {
            if (!(myfile >> x[i][j]))
                throw std::runtime_error("Couldn't read double from " + file);
        }
    }
}

int main(int argc, const char *const args[])
{
    try
    {
        double data[10][10];
        readFile("input.txt", data);
    } catch(const std::exception& e)
    {
        std::cerr << "Whoops: " << e.what() << std::endl;
        return 255;
    }

    return 0;
}

I suggest a fixup like this, making things more robust:

#include <fstream>
#include <iostream>
#include <string>
#include <stdexcept>

template <size_t size>
    void readFile(std::string file, double (&x)[size][size])
{
    std::ifstream myfile(file.c_str());

    for(int i=0; i<size; i++)
    {
        for(int j=0; j<size; j++)
        {
            if (!(myfile >> x[i][j]))
                throw std::runtime_error("Couldn't read double from " + file);
        }
    }
}

int main(int argc, const char *const args[])
{
    try
    {
        double data[10][10];
        readFile("input.txt", data);
    } catch(const std::exception& e)
    {
        std::cerr << "Whoops: " << e.what() << std::endl;
        return 255;
    }

    return 0;
}
他不在意 2024-11-11 09:32:59

您的输入运算符将读取数字,但在文件中保留换行符。

请参阅此答案,了解如何在读取值后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 values

Why would we call cin.clear() and cin.ignore() after reading input?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文