-4

I want to read a three column and N row txt file to three different arrays:

    int N=100;
    double x[N], 
    y[N], 
    z[N];

ifstream reading;
reading.open("reading.txt");




reading.close();

What should I write in the empty region? x[j], y[j], z[j] should be element in j'th row and first, second and third column respectively.

  • Imagine you are a computer and the four pieces of papers on your desk are the three arrays and one file respectively. What would you do? –  Oct 27 '17 at 05:06
  • I don't know what you mean. Could you please help me? I have searced a lot but I didn't find anything useful @NickyC – codder code Oct 27 '17 at 05:09
  • I'm describing how to think of what to write. I'm helping you to think of the steps you want your computer to carry out. Searching cannot replace thinking. –  Oct 27 '17 at 05:18

3 Answers3

1

Once you get the input file stream, it will be similar to reading from a standard input.

As a hint I can say, what about reading every integer and then store them appropriately. For example,

1 2 3
4 5 6
7 8 9

Now you read everything like this

while (redaing>> num) {
    // here you would know whether you are reading the first number
    // or second number or third.
    // x[xi] = num or y[yi]=num or z[zi]=num

}

Also you need to do something before you start reading from a file using the input file stream.

Have this check to make the program more safe.

if (!reading) {
    cerr << "Unable to open file reading.txt";
    exit(1);   // call system to stop
}

A solution would be like this:

int xi=0,yi=0,zi=0,iter=0;
while(redaing >>num){
    if(iter%3==0)x[xi++]=num;
    else if(iter%3 ==1)y[yi++]=num;
    else
    z[zi++]=num;
    iter++;
}

More succintly as pointed by user4581301

while(redaing >>x[xi++]>>y[yi++]>>z[zi++]){
    //..do some work if you need to.
}

Also another from comment to limit the 100 line reading is [From comment of user4581301]

int index = 0; 
while(index < 100 && redaing >>x[index]>>y[index]>>z[index] ){ 
 index++; 
}

A better solution:

 vector<int> x,y,z;
 int a,b,c;
 while(reading>>a>>b>>c){
    x.push_back(a);
    y.push_back(b);
    z.push_back(c);
    //..do some work if you need to.
}
user2736738
  • 30,591
  • 5
  • 42
  • 56
  • What should i write in your while loop? – codder code Oct 27 '17 at 05:14
  • @coddercode.: I have given enough hint...just assign them to appropriate variable. Like first time you assign in `x` then next time in `y` and then `z` and this goes on and on and on – user2736738 Oct 27 '17 at 05:15
  • But my question is not about safety. I don't know how to assign them in x,y,z – codder code Oct 27 '17 at 05:18
  • here you would know whether you are reading the first number // or second number or third. how can i know that? – codder code Oct 27 '17 at 05:21
  • @coddercode.: You can do it like this also – user2736738 Oct 27 '17 at 05:24
  • "I don't know how to assign them in x,y,z" then there's nothing we can do for you. Your level of understanding is too low. I strongly recommend reading a good book on C++. Here is a list of good books: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – user4581301 Oct 27 '17 at 05:26
  • 1
    @user4581301.: I guess I shouldn't have answered this? is that why the DV? – user2736738 Oct 27 '17 at 05:27
  • I didn't downvote. I don't downvote over answering bad questions unless bad advice is also given. I think your answer is a bit over complicated, but not bad. `while(redaing >>x[xi++] >> y[yi++] >> z[zi++])` is cleaner. – user4581301 Oct 27 '17 at 05:32
  • @user4581301.: Yes that's a good short solution..I am modifying this answer to incorporate your succint form. Thanks for pointing out. – user2736738 Oct 27 '17 at 05:33
  • @user4581301.: Modified. – user2736738 Oct 27 '17 at 05:35
  • Segmentation fault – codder code Oct 27 '17 at 05:36
  • @coddercode.: what do you mean? The answer given is working. Share your code – user2736738 Oct 27 '17 at 05:37
  • @coddercode Likely what happened is the file is larger than 100 lines long and the arrays ran out of space. `int index = 0; while(index < 100 && redaing >>x[index]>>y[index]>>z[index] && ) { index++; } ` should handle that, but will not read the whole file. You may have to learn about `std::vector` or how to create dynamic arrays. – user4581301 Oct 27 '17 at 05:46
  • @user4581301.: Yes I have added that solution – user2736738 Oct 27 '17 at 05:52
  • You've got my upvote. @coddercode , you can go one more step: The code can get even simpler by aggregating the various `vector`s or arrays with a structure. `struct coordinates {int x; int y; int z};` then later, `std::vector coords; coordinates temp; while(reading>>temp.x>>temp.y>>temp.z){ coords.push_back(temp); }`. The step after that, overloading `>>` for `coordinates`, is probably a bit too much for today. – user4581301 Oct 27 '17 at 06:19
-1

I'm kind of confused on the wording of your question could you try and reword It? Also if you want to read to the nth row I would use a while loop with the condition being while not end of file. Also you may consider using a vector since you don't know how large of an array you want to create.

-1

A trivial way is

  1. Read the file line by line using getline().
  2. Get the line into an istringstream.
  3. Use istringstream as any istream like cin, it will only contain one line of text.

I would suggest you to search for these terms on some website like www.cppreference.com if you don't know them.

Duli
  • 17
  • 2