reading input files

closed account (jGAShbRD)
Hello,
suppose i have a test.txt file that contains
1 0 3
2 1 4
3 2 5

and i want this to be taken into my main file which reads the first column and stores it in its own array. then second column in its own array. then third column in its own array. so in my main it should be

int a[100]={1,2,3};
int b[100]={0,1,2};
int c[100]={3,4,5};

is there a way to do this using fstream?
is there a way to do this using fstream?


Did you even try?

But to answer the no work question with a no work answer, yes it can be done with an fstream/istream.

closed account (jGAShbRD)
i wrote this code which had a logical error but then i was thinking would it be better to use a vector instead.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include<fstream>

int main(){
  std::ifstream file("test.txt");
  if(file.is_open()){
    int pid[9];
    for(int i=0;i<9;i++){
      file>> pid[i];
    for(auto x:pid){
      std::cout<<x;
    }
    }
  }
}


my test.txt
1 0 3
2 1 4
3 2 5


closed account (jGAShbRD)
i was thinking of using a vector that took vectors of ints and every number will pushback and then every 3 read numbers the vector would move back to vector[0] and push_back the element
Last edited on
there are lots of ways to do it.
there is very likely a slick way to read the whole mess into a valarry and slice on it.
it is also trivial to do a transpose in place on a 2-d vector.
I have a habit of reading and processing apart, rather than read a little process a little strategy. I find it works more efficiently (granted we don't care for 9 items). Lines of random text processed line-wise are an exception for a number of reasons.
Last edited on
I agree that vectors would probably be a good idea. However look at your first snippet, how many arrays to you propose to use there? How many arrays are you using in your more complete snippet?


By the way what seems to be the "logical error"?

What does your program output?

What is your desired output?

closed account (jGAShbRD)
so my output should be 103214325 but i got a bunch of random values for the arrays
but i made one using vectors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<vector>
#include<fstream>

int main(){
  std::vector<int> v;
  std::ifstream file("test.txt");
  if(file.is_open()){
    int value;
    while(file>>value)
      v.push_back(value);
    }
  for(int i=0;i<v.size();i++)
    std::cout<<v[i];
  return 0;
}


the problem with this code is if i use std::vector<std::vector<int>> it wont work
i want my program to have like
at vector 0 to have 1 0 3
vector 1 to have 2 1 4
vector 2 to have 3 2 5
but my program is just having 1 0 3 2 1 4 3 2 5 like a 1 d vector instead of 2 d
Last edited on
Why do you think a vector<vector<int>> is the correct solution?

at vector 0 to have 1 0 3
vector 1 to have 2 1 4
vector 2 to have 3 2 5


That looks like three vectors, not two.

Perhaps you should consider sticking with a single vector for each column for now, instead of trying to bring in the complexity of multidimensional vectors/arrays.

but my program is just having 1 0 3 2 1 4 3 2 5 like a 1 d vector instead of 2 d

That's because you're only using a single single dimensional vector. By the way your "array" is a three dimensional "array" not 1 or 2.

Use three single dimensional vectors and read each element into the correct element of the correct vector (think about using multiple loops for your reading).


Here. assuming the text file is as your first post:
you can add fluff like checking file there, figuring out how many things per row, whatever details... sorry used v1-3 for your a,b,c whatever you want to name them.

its harder to read the file than to get your 3 containers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
	ifstream f("x.txt");
	valarray<int> v(9);
	for(int i = 0; i < 9; i++)
	 f>> v[i];
	
	valarray<int> v1 = v[slice(0,3,3)]; //start at 0, get 3, 3 apart
	valarray<int> v2 = v[slice(1,3,3)]; //start at 1, get 3, 3 apart
	valarray<int> v3 = v[slice(2,3,3)]; //start at 2, get 3, 3 apart
	for(int i = 0; i < 3; i++) cout << v1[i] << endl;
	for(int i = 0; i < 3; i++) cout << v2[i] << endl;
	for(int i = 0; i < 3; i++) cout << v3[i] << endl;
	
}


1
2
3
4
5
6
7
8
9
1
2
3
0
1
2
3
4
5
Last edited on
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
#include <iostream>
#include <fstream>

/*
 test.txt
 
 1 0 3
 2 1 4
 3 2 5
 
 */

void display(int*, int);

int main()
{
    const int SIZE{100};
    
    // READ FROM .txt FILE
    std::ifstream read_from_file ("test.txt");
    
    int a[SIZE], b[SIZE], c[SIZE];
    
    int COUNT{0};
    
    if (read_from_file.is_open())
    {
        while ( read_from_file >> a[COUNT] >> b[COUNT] >> c[COUNT] )
        {
            COUNT++;
        }
        read_from_file.close();
    }
    else
    {
        std::cout << "Unable to open file";
        return -99;
    }
    
    display(a, COUNT);
    display(b, COUNT);
    display(c, COUNT);
    
    return 0;
}

void display(int *array, int size)
{
    for(int i = 0; i < size; i++)
    {
        std::cout << array[i] << ' ';
    }
    std::cout << '\n';
}



1 2 3 
0 1 2 
3 4 5 
Program ended with exit code: 0
Topic archived. No new replies allowed.