Input an integer array from a file

I can't figure out how to write out the following function correctly::
The data stored is in the form on rows and columns.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void matrix::read_file(char* filename)
{
   ifstream i_f;
   matrix c;
   string o;
   int *v,t(0),x(0),y(0),z(0);


   while(!i_f.eof())
   {

       getline(i_f,o);
       //this o string is not input correctly
       t = o.length();
       v = new int[t];
       for(z=0;z<=t;z++)
       {

           *(v+z) = int(o[z])-48;
           
           
       }
       y++;

   }
   c.allocate(t,y);
   for(z=0;z<(x*y);z++)
   {
       cout<<"putting\n";
       c.set_i(*(v+z),z);
   }
   i_f.close();

}

allocate() allocates the memory for the 2 dimensional array to be stored.It works correctly.
set_i(<value>,<location>)is for putting the individual elements of the array into position.It also works correctly.
You never actually open your input file.
A better way to implement this:
1
2
3
4
5
   while(!i_f.eof())
   {

       getline(i_f,o);

Is like this:

1
2
3
   while(getline(i_f,o))
   {
   }


That is because EOF flag is not set until after the read fails. So doing it your way will execute the loop with invalid data. But if you test the return of getline() in the while condition you can guarantee that the loop only executes if the read was successful.
Last edited on
Could you please explain a bit elaborately?
Only changing the condition of the while loop is returning the same results.
You still need to open your file:
 
i_f.open(filename);

... before you use it.
Last edited on
Alas..the same result again::that was a mistake but there is something else wrong too::posting it again::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void matrix::read_file(char* filename)
{
   ifstream i_f(filename);
   matrix c;
   string o;
   int *v,t(0),x(0),y(0),z(0);


   //while(!i_f.eof())
   while(getline(i_f,o))
   {

       getline(i_f,o);
       
       t = o.length();
       v = new int[t];
       for(z=0;z<=t;z++)
       {

           *(v+z) = int(o[z])-48;


       }
       y++;

   }
   c.allocate(t,y);
   for(z=0;z<(x*y);z++)
   {
       cout<<"putting\n";
       c.set_i(*(v+z),z);
   }
   i_f.close();

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void matrix::read_file(char* filename)
{
   ifstream i_f( "filename.txt" );                 // open the file
   matrix c;                                       // this stores the result and is destroyed at the end of the function
   string o;
   int *v,t(0),x(0),y(0),z(0);                     // generally, declare locals at late as possible


   while(!i_f.eof())                               // as Galik said, move getline here
   {

       getline(i_f,o);
       //this o string is not input correctly
       t = o.length();                             // why?
       v = new int[t];
       for(z=0;z<=t;z++)                           // this goes 1 element beyond the space allocated for v
       {

           *(v+z) = int(o[z])-48;                  // how about: v[z] = o[z] - '0';
           
           
       }
       y++;                                        // counting lines?

   }                                               // v was the "row" but is being leaked each iteration
   c.allocate(t,y);                                // t is the length of the last line, y is the total number of lines
   for(z=0;z<(x*y);z++)
   {
       cout<<"putting\n";
       c.set_i(*(v+z),z);                          // what?
   }
   i_f.close();

}
Last edited on
1.Why? because I put a cout<<o<<"Here"; there ; but nothing except the Here is printed
2.I already put the getline there.Would I have to put it within again?It is becoming confusing
3.If it went 1 element beyond, a segmentation fault would appear.//I use linux, it does show segmentation faults at runtime
4.What?set_i(..........) puts an individual element of the array inside an int * value declared within the class.
the allocate function dynamically allocates x*y spaces to value.
I think you should go back to the drawing board and come up with a plan before coding it. Just a thought.
Most of the rest of the project is already written.Why would I start planning now?
You need to remove that second getline(), you only need it in the while condition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void matrix::read_file(char* filename)
{
   ifstream i_f(filename);
   matrix c;
   string o;
   int *v,t(0),x(0),y(0),z(0);


   //while(!i_f.eof())
   while(getline(i_f,o))
   {

       // getline(i_f,o); // No longer needed here, moved to while condition
       
       t = o.length();
       v = new int[t];
       for(z=0;z<=t;z++)
       {

           *(v+z) = int(o[z])-48;


       }
       y++;

   }
   c.allocate(t,y);
   for(z=0;z<(x*y);z++)
   {
       cout<<"putting\n";
       c.set_i(*(v+z),z);
   }
   i_f.close();

}
Same result again..Perhaps a new logic for this function would really help.
What is it supposed to do? What does the input file look like?
The input file contains ,for example:
1<tab>2
3<tab>4

It is written using:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void matrix::write_file(char* filename)
{

    ofstream o_f(filename);
    int m(0),z(0),count(0);

    cout<<"The value are..\n";
    for(z=0;z<x;z++)
        {
            for(m=0;m<y;m++)
            {
                o_f<<*(value+count)<<"\t";
                count++;
            }
            o_f<<endl;
        }
        o_f.close();
}


The problem is that reading the file like this -> getline(i_f,o) also puts the tab characters in your string. This is something you don't want. Why don't you try doing it using the >> operator? And in order to know the size of the matrix, you can just store it in the file before the actual matrix data.
Good idea, but something went wrong again..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
void matrix::write_file(char* filename)
{

    ofstream o_f(filename);
    int m(0),z(0),count(0);

    //cout<<"The value are..\n";
    o_f<<x<<endl;
    o_f<<y<<endl;

    for(z=0;z<x;z++)
        {
            for(m=0;m<y;m++)
            {
                o_f<<*(value+count)<<'|';
                count++;
            }
            o_f<<endl;
        }
        o_f.close();
}
//```````````````````````````````````````````````
void matrix::read_file(char* filename)
{
   ifstream i_f;
   i_f.open(filename,ios::in);
   matrix c;
   string o;
   int *v,t(0),x(0),y(0),z(0);
   i_f>>x;
   i_f>>y;
    v = new int(x*y);
   //while(!i_f.eof())
   while(getline(i_f,o))
   {

       //getline(i_f,o);

       for(z=0;z<t;z++)
       {
           if(o[z]=='|')
           {continue;}
           else
           {
               *(v+z) = int(o[z]);

               z++;

           }


       }


   }


   c.allocate(x,y);
   for(z=0;z<(x*y);z++)
   {
       //cout<<*(v+z)<<endl;
       c.set_i(*(v+z),z);
   }
   i_f.close();

}
Use space to separate the elements:

1
2
3
int size=x*y;
for(count=0; count<size; count++)
    o_f << *(value+count) << ' ';

Then use operator >> to get the data:

1
2
3
int size=x*y;
for(count=0; count<size; count++)
    i_f >> *(value+count);
Last edited on
You are a genius; I was making things complicated;
This works very well indeed;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void matrix::read_file(char* filename)
{
    int x,y;//orders
    ifstream i_f;
    i_f.open(filename,ios::in);
    i_f>>x;
    i_f>>y;
    int v,loop;
    //cout<<x<<' '<<y;
    allocate(x,y);
    for(loop = 0;loop<=(x*y);loop++)
    {
        i_f>>v;
        set_i(v,loop);
    }
    i_f.close();

}
Topic archived. No new replies allowed.