简体   繁体   中英

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:

    #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. 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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