File processing filtering negative and positive numbers

Hi, i was wondering if you guys can help me my problem is that the numbers in "numbers.txt" wont filtered when i try to run the program the numbers inside the numbers.txt has been copied all in positive.txt, help me pls

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
  #include<iostream>
#include<stdio.h>
#include<ctype.h>
using namespace std;

int main(){
	
	FILE *pOriginal;
	FILE *pPositive;
	FILE *pNegative;
	char num;
	
	pOriginal=fopen("numbers.txt","r");
	
	pPositive=fopen("positive.txt","w");
	
	pNegative=fopen("negative.txt","w");
	
	num=fgetc(pOriginal);
	
	
	while (num!=EOF){
		
		if(num>0)
		fputc(num,pPositive);
	
		
		if(num<0)
		fputc(num,pNegative);

	
		
		num=fgetc(pOriginal);
	}
	
	fclose(pOriginal);
	fclose(pPositive);
	fclose(pNegative);
	
	system ("pause");
	return 0;
}
Why do you put the numbers in a char?

You open the file in text mode and read only one char, even if the number is negative it will read only the - sign and the ascii code is greater than zero so all will be positive.

Better you use fscanf.
http://www.cplusplus.com/reference/cstdio/fscanf/

Here is a little example that reads and displays numbers from a file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  FILE *src = fopen("numbers.txt", "r");
  if (src == NULL)
  {
    fprintf(stderr, "Can't open file");
    return -1;
  }

  int num;
  while(!feof(src))
  {
    fscanf(src, "%d", &num);
    printf("%d ", num);
  }
  fclose(src);

  system("pause");
  return 0;
  
}
Last edited on
The numbers was given is about 1-10 only 4 negative and positive numbers can you correct the code for me? I dont understand clearly was have you said
I dont understand clearly was have you said


Ok, maybe it was too abstract. Let me give you a complete example. Image your file contains a number -6. fgetc reads one character - in this case '-'. Since the ASCCI code for - is > 0 the program writes it to the file pPositive. Then it reads the next char which is 6. Since it is > 0 it writes it to pPositive. pPositive contains now -6 which is wrong.

Try this 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
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdlib.h>

#pragma warning(disable: 4996)

int main(void)
{
  FILE *src = fopen("numbers.txt", "r");
  FILE *positive = fopen("positive.txt", "w");
  FILE *negative = fopen("negative.txt", "w");

  if (src == NULL)
  {
    fprintf(stderr, "Can't open file");
    return -1;
  }
  if (positive == NULL)
  {
    fprintf(stderr, "Can't open file positive.txt");
    return -1;
  }
  if (negative == NULL)
  {
    fprintf(stderr, "Can't open file negative.txt");
    return -1;
  }
  int num;
  while(!feof(src))
  {
    fscanf(src, "%d", &num);
    if (num > 0)
      fprintf(positive, "%d ", num);
    else if (num < 0)
      fprintf(negative, "%d ", num);
  }
  fclose(src);
  fclose(negative);
  fclose(positive);

  system("pause");
  return 0;
  
}
Topic archived. No new replies allowed.