input

I wanna to give two input file to my code.How do it? Why this is not correct?



1
2
3
4
5
  while (input) {

			input.getline(str , 255);
			input.getline(coef, 3);
}
Explain more. What do you mean by two input files? Provide us more code. From what you have given to us, nobody can really tell what is going on.
I have two text files. I wanna use them in my code. But i cant enter these files in code.
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
int main() {
const int NrSamples = 512;
const int NrCoefficient = 220;
char str[255];
int coef[3];
int index = 0;

ifstream input("80event.txt");
ifstream input("filter coefficient.txt ");
string data[NrSamples];
string coeff[NrCoefficient];

while (input) {

			input.getline(str , 255);
			input.getline(coef, 3);
                                data[index] = str;
				coeff[index] = coef;

cout << "index: " << index << ", data: " << data[index] << endl;
cout << "index: " << index << ", coeff: " << coeff[index] << endl;

}
return.;
}
You can use command line arguments (which is probably what your assignment wants you to use).
1
2
3
4
5
6
#include <iostream>

int main(int argc, char* argv[]) {
  ...
  return 0;
}


http://www.cplusplus.com/articles/DEN36Up4/
I use it already.I forget to write it here.
You cannot use the same name for two different variables (line 8/9). You may use input1 and input2 or better but distinct names.

Note that index remains 0 in your loop since it is not increased.
Thanks. First problem is solved. The next is that the size of input1 is greater than input2. When it run ,the code only read to input2 size. I mean for example the size of input1 is 100 and input2 is 50,code only read 50 samples of input1.
Show the modified code. What do you mean by input size?
Do you have to read the lines from the input files in parallel like that, or can you process the first file, and then process the second file?

If you can process the files one at a time, the write a function that processes one file. Then you can call it as many times as you like with whatever files you wish.
@coder777 this is modified code.
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
int main(int argc, char* argv[]) {

const int NrSamples = 10;
const int NrCoefficient = 5;
char str[255];
int coef[3];
int index = 0;

ifstream input("input1.txt");
ifstream input("input2.txt ");
string data[NrSamples];
string coeff[NrCoefficient];

while (input1,input2) {

			input.getline(str , 255);
			input.getline(coef, 3);
   if(input1,input2){
                                data[index] = str;
				coeff[index] = coef;

cout << "index: " << index << ", data: " << data[index] << endl;
cout << "index: " << index << ", coeff: " << coeff[index] << endl;

}
}
return.;
}





input files are text file.
input1:
130
128
127
128
130
130
129
127
128
130
input2:
130
128
127
128
130
The comma operator on line 14/18 does not do what you think it does. See:

http://www.cplusplus.com/doc/tutorial/operators/

You need either && or || operator. Honestly both are not a good idea. index is still not increased. Rather, use two [for] loops.
@coder777 I do it. But the problem not solved. I wanna index be zero.
So please show your modified code.

I wanna index be zero.
What do you mean? If you have two loops you need two indexes as well.
@coder777 Thanks. I find my mistake. Now code work correctly.
Topic archived. No new replies allowed.