1

Hi i need to read numbers from file and make a new file with only repeated numbers included.
I run into problem that when i read into array those numbers are not seperated.

So I want to ask how to read numbers from files with spaces or how to know when number ends?

Because if I read like this i can't do any more operations with those numbers.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
  FILE *file = fopen("w.txt", "r");
  int integers[100];

  int i = 0;
  int num;
  while (fscanf(file, "%d", &num) > 0)
  {
    integers[i] = num;
    i++;
    printf("%d", integers[i]);
  }
  fclose(file);
  system("pause");
  return 0;
}
2
  • 1
    I want to ask how to read a question written without any punctuation ... Commented Feb 15, 2014 at 16:09
  • In regards to this question you recently asked: you are supposed to accept the best answer on Stack Overflow, not the quickest: "Don't pick too quickly: Try not to select an answer minutes after posting it. Sometimes a better answer will come hours later, and you might miss out on it if you select your answer too fast. The other problem with this is that other stack overflow users will see the accepted answer, only read that one and not the others, and vote it up. You will have an inflated vote answer for possibly not the best answer." Commented Apr 26, 2014 at 10:37

1 Answer 1

0

I'd increase the index counter after having tried to print the number just read in:

while(fscanf(file, "%d", &num) > 0) {
  integers[i] = num;
  printf("%d\n", integers[i]); /* Printing a \n after each number puts it on a new line. */
  i++;
Sign up to request clarification or add additional context in comments.

5 Comments

It's still doesnt matter imagine .txt file content is 10 99 i read it into array as 1 0 9 9 i need it to be 10 99 but dont know how
@user3313750: The code you show reads 12 34 as 12 and 34. The case you describe in your comment in cannot reproduce. Also please see my updated answer.
Yh little bit retarded just came from work sry ty by the way but still why #include<stdio.h> #include<stdio.h> #include<stdlib.h> #include<string.h> int main() { FILE *file = fopen("w.txt", "r"); int integers[100]; int i = 0,a,b; int num; while(fscanf(file, "%d", &num) > 0) { integers[i] = num; i++; } fclose(file); for (a=0;a<100;a++) { for(b=0;b<100;b++) { if(integers[a]==integers[b]); printf("%d",integers[a]); }} system("pause"); return 0; } ` this brings mass nonsence ?
@user3313750: What is this if(integers[a]==integers[b]); for? Please note the ; at the end!
to compare if 1 element is equal to other element btw always forget those :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.