Read integers from file only once

Hi!
I want to load numbers from a file, but every number only once.
An input, for example:
1 2 2 5 1 7

I want to store:
1 2 5 7

Here's what I've done so far:

if(i < 10){ // I want to store the numbers in an array,size of 10
for(b=0; b<10; ++b){ // *
while (inFile >> n) { // inFile is an ifstream
if(fromfile[b] != n){ // *
fromfile[i] = n; // store the value read from the file
++i; // increase the array's index
}
}
}
}

* I want to parse the whole array, but these lines are wrong.. Somebody got an idea?

Here's what I get as output:
1 2 2 5 7
So it leaves out the first element,which is "doubled", but the others not.

Thanks.
You're not providing much code to help us help you. The basics should look like this:

1
2
3
4
5
6
for(int i = 0; i < inputSize; ++i) {
  for (int j = 0; j < uniqueSize; ++j) {
    if (input[i] == uniques[j]) break;
    uniques[uniqueSize++] = input[i]; 
  }
}

I'm confused about your code, since you're using the input as an inner loop, while it should clearly be the outer loop. The inner loop should check for duplicates (i.e. iterate over already-saved values).

The weirdest thing about your code is the first if(). You're checking whether i < 10, but only once, at the start (when it's most likely 0). i is incremented inside the loops, but never checked again, thus the if() is completely useless.
Sorry, but I can't get it working, please help if you can.

Here's the full 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
31
32
33
#include <iostream>
#include <fstream>

using namespace std;

int main()
{  
  ifstream inFile;
  inFile.open("input.txt");
  if (inFile.fail()) {
      cerr << "unable to open file input.txt for reading" << endl;
      exit(1);
  }

int n, i=0, b=0, j=0;
int fromfile[10];

for(i=0;i<10;++i){
fromfile[i]=0;
}

while (inFile >> n) {
         if(j<10){
 for(b=0; b<10; ++b){
   if(n != fromfile[b]){
    fromfile[j] = n;
    printf("j:%d, value: %d\n",j,fromfile[j]);
    ++j;
    break;
}}}}

return 1;
}


It outputs the input now, but I need every number only once.
What's missing?
Last edited on
Your structure is still wrong.
To put it in words, you do this:

1
2
3
for every input 'n':
  for every saved value 'b':
    if not equal, add 'n'

Don't you see what's wrong there? It doesn't check if there's any value equal to 'n' in your uniques, but it checkes if there''s any value NOT equal. Unless ALL your input it equal, it will always print the full input.

Your logic should be:
1
2
3
4
5
for every input 'n':
  if there is a value 'b' == 'n':
    break;
  else:
    add
Last edited on
Hmm I understand your logic, but still can't get it to work.

1
2
3
4
5
6
7
8
9
10
 while (inFile >> n) {
 if(j<10){
 for(b=0; b<10; ++b){
   if(n == fromfile[b]){
    break;
    }
   else{
    fromfile[j] = n;
    ++j;
 }}}}
Finally, I've managed to finish this, here's the solution, if someone needs it:
Maybe not the simpliest, but it works well.
(I store numbers between 1 and 9, you can change this if you want.)

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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{  
  ifstream inFile;
  inFile.open("data/input.txt");
  if (inFile.fail()) {
      cerr << "unable to open file input.txt for reading" << endl;
      exit(1);
  }

int n, i=0, b=0, j=0;
int fromfile[100];

for(i=0;i<10;++i){
fromfile[i]=0;
}

while (inFile >> n) {
 for(b=0; b<100; ++b){
   if(n != fromfile[b] && n > 0 && n < 10){
    fromfile[j] = n;
    ++j;
    break;
}}}

for(j=0; j<10; ++j){
    for(i=j+1; i<11; ++i){
    if(fromfile[j] == fromfile[i]){
    fromfile[i] = 0;
    }}
}

for(j=0; j<10; ++j){
    if(fromfile[j] == 0){
    fromfile[j] = fromfile[j+1];
    fromfile[j+1] = 0;
    }
}

printf("\n VALUES:\n---------\n");
for(j=0; j<10; ++j){
if(fromfile[j] > 0){
printf("j:%d, value j: %d\n",j,fromfile[j]);
}}

return 1;
}
Last edited on
Topic archived. No new replies allowed.