Inputting and Ordering Numbers in C

  • Thread starter Thread starter rambo3131
  • Start date Start date
AI Thread Summary
The discussion revolves around writing a C program that accepts an uncertain number of integer inputs and sorts them from maximum to minimum. The main challenge highlighted is the inability to determine when to stop reading inputs without using EOF (end-of-file). Suggestions include using a predefined number of inputs, a terminating character, or EOF itself. Several code snippets are provided to demonstrate how to read integers until EOF or an invalid input is encountered. Additionally, users are guided on how to create a file for testing and how to signal EOF in a terminal using Ctrl+D. The conversation emphasizes the importance of understanding EOF and provides examples of robust file handling in C.
rambo3131
Messages
18
Reaction score
0
Hi , I have a question about C. I want to write program that takes numbers as inputs and orders them from maximum to minimum. However, i ve a problem .Count of number is uncertain.I mean ,sometimes i will give the program 5 numbers ,sometimes 10 numbers or another.How can i do this without using eof since i don't know using eof.Please help me ...
 
Last edited:
Technology news on Phys.org


Sorry ,topic must be taking inputs in C .By the way I use linux.
 
You have to tell the program when to stop reading from the file. You can make the first entry in the file the number of numbers, use a terminating number or character in the file, or use EOF. If the file is supplied to you, you'll probably need to use EOF. Good luck, rambo3131.
 
rambo3131 said:
Hi , I have a question about C. I want to write program that takes numbers as inputs and orders them from maximum to minimum. However, i ve a problem .Count of number is uncertain.I mean ,sometimes i will give the program 5 numbers ,sometimes 10 numbers or another.How can i do this without using eof since i don't know using eof.Please help me ...
Can you give us the exact wording of the problem? It would help to know this information, which can help determine the direction to go.
 
rambo3131 said:
Hi , I have a question about C. I want to write program that takes numbers as inputs and orders them from maximum to minimum. However, i ve a problem .Count of number is uncertain.I mean ,sometimes i will give the program 5 numbers ,sometimes 10 numbers or another.How can i do this without using eof since i don't know using eof.Please help me ...

The following code will read numbers until unsuccesful.
That will be at EOF or when a non-number character is encountered.

Code:
#include <stdio.h>
void main(void)
{
    int number;
    while (1 == scanf("%d", &number))
    {
        printf(" %d", number);
    }
}

Is this what you want?
 
rambo3131 said:
I want to write program that takes numbers as inputs

From a file or from the keyboard?
 
Thanks for all of you so much! I used " ı like serena" 's technique(without eof) and understood this. Moreover i want to learn about eof ( i think, this eof was important).How can i use eof?.Should i create file ? but i don't know this.Could you give an example code please?
 
Hi, rambo3131. I think this is correct. It's been awhile.

Code:
#include <stdio.h>
#include <io.h>
#include <stdlib.h>

int main(void)
{
  FILE *fp;
  int number; 

  fp=fopen("test", "r");
 
// A return value of EOF != 1, so this will terminate on EOF.

while (1 == fscanf(fp, "%d", &number)) 
  {
   printf("%d", number);
   }

  return 0;
}
 
rambo3131 said:
Thanks for all of you so much! I used " ı like serena" 's technique(without eof) and understood this. Moreover i want to learn about eof ( i think, this eof was important).How can i use eof?.Should i create file ? but i don't know this.Could you give an example code please?

I'm not sure what you do and do not know, so I'll just start somewhere.

EOF means "end-of-file" and if you try to read beyond the end of a file your reading function will act as if the special EOF-symbol has been read.

To create a file you can use e.g.:
$ echo "1 2 3 4" > test.txt

and you can run the program with
$ ./a.out < test.txt

Alternatively you can run your program without a file, effectively reading from the keyboard. You would do that like this:
$ ./a.out
1 2 3 4
Ctrl+D

The Ctrl+D here signals to Linux that you're at the end of your input, which is treated by your program as if the end-of-file is reached.

Is this the kind of stuff you want to know?
 
  • #10
yes, Ctrl+D is so useful .Thank you so much.
 
  • #11
Besides, ı got your eof example sciurus.thanks a lot :)
 
  • #12
rambo3131 said:
Besides, ı got your eof example sciurus.thanks a lot :)

I have a slight modification of sciurus' code to make it more robust.

Code:
#include <stdio.h>

int main(void)
{
   FILE *fp;
   int number; 

   fp = fopen("test", "r");
   if (NULL != fp)
   {
      /* A return value of 1 means that 1 number has been 
       * succesfully parsed from the input
       */
      while (1 == fscanf(fp, "%d", &number)) 
      {
         printf("%d", number);
      }
      fclose(fp);
   }

   return 0;
}
 
Back
Top