简体   繁体   English

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

[英]Problem reading a file containing decimals

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:我在试图弄清楚为什么我的文件返回 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;
    } 

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.希望有人可以帮助我,因为我研究了一些关于此的信息并没有真正找到解决方案。

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.我在没有得到命令行 arguments 的情况下运行了您的代码。 The following code, is pretty much copy-pasted from your main minus getting command line args.以下代码几乎是从您的 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;
}

With the input you provide, I get the output:通过您提供的输入,我得到 output:

20 20 20 20 20 20 20 20 20 20
200 20 200 0 0 200 20 200 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 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.但是查看您的命令行 arg 读取代码,我可以看到一些潜在的问题。 First you use atoi().首先你使用atoi()。 When atoi fails, it returns 0. Do you know that this code is working?当atoi失败时,它返回0。你知道这段代码有效吗? 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?还是 atoi 在输入上失败,导致 n 为 0 并因此导致无法读取任何内容? (You may wish to look into stringstreams for doing this kind of thing). (您可能希望研究stringstreams来做这种事情)。

Moreover, when argc is not 6, you're silently failing and reading from uninitialized global memory.此外,当 argc 不是 6 时,您会默默地失败并从未初始化的全局 memory 读取。 This memory is garbage.这个 memory 是垃圾。 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?那么 5 不会从命令行读取,因为 argc 不是 6。你是否总是像测试时一样传递 6 arguments? Maybe not, cause in its current state your code really only needs the file name and size.也许不是,因为在当前的 state 中,您的代码实际上只需要文件名和大小。

Most important thing, see if you're getting what you expect from the command line.最重要的是,看看你是否从命令行得到了你所期望的。

PS This is g++: PS这是g++:

$ g++ --version g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5 Copyright (C) 2010 Free Software Foundation, Inc. This is free software; $ g++ --version g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5 版权所有 (C) 2010 Free Software Foundation, Inc. 这是免费软件; see the source for copying conditions.查看复制条件的来源。 There is NO warranty;没有保修; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.甚至不考虑适销性或特定用途的适用性。

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

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阅读您的值后,请参阅有关如何ignore()行尾的答案

Why would we call cin.clear() and cin.ignore() after reading input? 为什么我们会在读取输入后调用 cin.clear() 和 cin.ignore() ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM