LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Reading variables from a text file in C++ (https://www.linuxquestions.org/questions/programming-9/reading-variables-from-a-text-file-in-c-753456/)

Mork2k4 09-07-2009 10:20 PM

Reading variables from a text file in C++
 
Forgive me if there is another thread like this one but I did not see a thread that matched what I was looking for.

I can read lines from a text file easy but I do not know how to read a single variable and store it to an array. For example:

1 1 1 0 0 0 1 0 0 1 0 1 1
0 0 1 0 0 1 1 1 0 0 1 0 0

I can read the whole thing but I need to store it into a 2d array. I am having problems reading one number at a time.

alexroux 09-08-2009 02:33 AM

Two common ways:

In C: using fscanf (http://www.cplusplus.com/reference/c...cstdio/fscanf/)
In C++: using ifstream and >> operator (http://www.cplusplus.com/reference/iostream)/istream/operator%3E%3E/)

I advise to use the C++ way since you code in C++.

neonsignal 09-08-2009 03:08 AM

Code:

#include <cstring>
#include <fstream>
int main()
{
        const size_t Sx = 13, Sy = 2;
        int a[Sx][Sy];
        size_t x = 0, y = 0;
        std::ifstream file("test.dat");
        while (file)
        {
                file >> a[x][y];
                if (++x == Sx)
                {
                        x = 0;
                        if (++y == Sy)
                                y = 0;
                }
        }
        file.close();
}


hyperactive22 09-08-2009 05:05 AM

@neonsignal

Could u please simulate ur code for the input given by OP. I found that the contents stored in array are as follows:
1 0 1 0 1 1 0 0 0 0 0 1 1
1 0 1 1 0 0 0 0 0 1 1 1 0

neonsignal 09-08-2009 05:24 AM

Quote:

Could u please simulate ur code for the input given by OP. I found that the contents stored in array are as follows:
1 0 1 0 1 1 0 0 0 0 0 1 1
1 0 1 1 0 0 0 0 0 1 1 1 0
Try switching around the array indexes when you print the array out.

hyperactive22 09-08-2009 05:36 AM

Seems that the problem was due to newline character in file. Modified the code given by neonsignal a little bit and swapped the index checks. Its working now.
int main()
{
const size_t Sx = 13, Sy = 2;
int a[Sx][Sy];
size_t x = 0, y = 0;
std::ifstream file("test.dat");
while (file)
{
file >> a[x][y];
if (++y == Sx)
{
y = 0;
++x ;
file ; //This line is to read the newline char and discard it
}
}
file.close();
return 0;
}

hyperactive22 09-08-2009 05:39 AM

Seems to be a problem with newline char in file. Modified the code posted by neonsignala bit and swapped the indices, now the code is working.
int main()
{
const size_t Sx = 13, Sy = 2;
int a[Sx][Sy];
size_t x = 0, y = 0;
std::ifstream file("test.dat");
while (file)
{
file >> a[x][y];
if (++y == Sx)
{
y = 0;
++x ;
file ; //This line reads and discards the newline character
}
}
file.close();
return 0;
}

Mork2k4 09-09-2009 09:54 AM

That worked perfectly. Thank's


All times are GMT -5. The time now is 07:57 AM.