0

I tried to write a pice of code that lets the user enter an infinite (limited only by memory) amount of strings where each string can be up to 298 chars long (array of 298 with one '\n' and '\0' in the end). The end signal should be EOF (Strg+Z on Win, Stg+D on Linux). The code down there works just fine when i change EOF to 'a' or any other char but not with EOF. I would love to debugg it, but unfortunately i cannot put an EOF into my terminal in eclipse. So does anyone know why there's a difference between EOF (which should be -1) and a char or how to put in an EOF in eclipse?

while(boolean == 0)
{
fgets(buff_memory, 300, stdin);

for (i = 0; i < 300; i++)
{
  if (buff_memory[i] == EOF)
   {
    all_memory[current_max - 1] = '\0';
    boolean = 1;
    break;
   }
  else if (buff_memory[i] == '\0')
  {
    break;
  }
  else
  {
    all_memory[current_max - 1] = buff_memory[i];
    buff_memory[i] = '\0';
    current_max++;
    all_memory = (char *)realloc(all_memory, current_max * sizeof(*all_memory));
  }
}
BStadlbauer
  • 1,287
  • 6
  • 18

3 Answers3

1

feof never puts EOF in its output. EOF is an int value of -1, which cannot be represented in a char array. EOF is only returned by getc and similar functions.

On end-of-file, fgets will return NULL when it gets to end-of-file and you need to check feof to verify that EOF has been reached.

DoxyLover
  • 3,366
  • 1
  • 15
  • 19
  • Thanks, i knew that EOF should be -1 but i couldn't use an integer array because fgets only accepts strings and getchar() can only process one char at a time. – BStadlbauer Nov 14 '14 at 21:22
0

if you press ctrl+space, can you see EOF in the auto-complete list in your Eclipse? Btw, your EOF has probably nothing to do with Eclipse. You EOF is evaluated as '-1' it's not a char. Again, Eclipse has nothing to do with EOF. It is an IDE and it will do what you ask - made by human after all.

Read this for further clarificatoin - http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1048865140&id=1043284351

ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
  • Thanks i knew that Eclipse is just an IDE but there was a bug that prevented you from entering EOF a while ago: http://stackoverflow.com/questions/5494958/send-an-eof-in-eclipses-debugger-console. Edit: Sadly ctrl+space did not help in my case – BStadlbauer Nov 14 '14 at 21:20
0

I found a solution that fits my problem. With it i can detect an EOF at the beginning of a line but not inbetween or in the end. Here's the code i'll use:

while (1)
{
fgets(buff_memory, 300, stdin);

if (buff_memory[0] == '\0')
{
  break;
}
else
{
  for (i = 0; i < 300; i++)
  {
    if (buff_memory[i] == '\0')
    {
      break;
    }
    else
    {
      all_memory[current_max - 1] = buff_memory[i];
      buff_memory[i] = '\0';
      current_max++;
      all_memory = (char *) realloc(all_memory, current_max * sizeof(*all_memory));
    }
  }
}
BStadlbauer
  • 1,287
  • 6
  • 18