[re-post] why no output

G

Gene

This compiles.  Why no output?

#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdlib.h>

#define my_file "text42.txt"
#define NUMBER 8192

bool get_script(FILE *, char *, int );

int main(void)
{
  FILE *fp;
  char text[NUMBER];
  bool len;

  if ((fp = fopen(my_file, "r")) == NULL )
  {
    fprintf(stderr, "can't open file\n");
    exit(EXIT_FAILURE);
  }
  while((len = get_script(fp, text, NUMBER)) > 0)
  {  
    printf("%s\n", text);
  }
  fclose(fp);
  return 0;

}

// gcc s3.c -Wall -o out

bool get_script(FILE* in, char* result, int size)
{
  int ch;
  int i = 0;
  while (i < size - 1 && (ch = getc(in)) != EOF)
  {
    result[i++] = ch;
    int j = 0;
    for (; i + j < size - 1 &&
    (isdigit(ch) || ch == ':'); ++j)
    {
       if (j + 1 == 10)
       {
          result[i + j] = '\0';
          return true;
       }
       if ((ch = getc(in)) == EOF)
       {
          result[i + j] = '\0';
          return false;
       }
       result[i + j] = ch;
     }
     i += j;
  }
  result = '\0';
  return false;}


A perfect opportunity to learn how to use a debugger! Step through it
and see what's happening.

Also be sure to run with

../out

rather than

out

to make sure you're not picking up some other program in the path.
 
Q

qarnos

This compiles.  Why no output?

<snip code>

I don't have access to a C compiler right this minute, so I can't test
your code but I would ask one question:

What are the contents of the input file? If the first character is
neither a digit or a colon, you will get no output. I have a hunch
your file might begin with a blank line, which would mean the first
character is "\n".
 
F

Franken Sense

This compiles. Why no output?

#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdlib.h>


#define my_file "text42.txt"
#define NUMBER 8192


bool get_script(FILE *, char *, int );

int main(void)
{
FILE *fp;
char text[NUMBER];
bool len;

if ((fp = fopen(my_file, "r")) == NULL )
{
fprintf(stderr, "can't open file\n");
exit(EXIT_FAILURE);
}
while((len = get_script(fp, text, NUMBER)) > 0)
{
printf("%s\n", text);
}
fclose(fp);
return 0;
}

// gcc s3.c -Wall -o out

bool get_script(FILE* in, char* result, int size)
{
int ch;
int i = 0;
while (i < size - 1 && (ch = getc(in)) != EOF)
{
result[i++] = ch;
int j = 0;
for (; i + j < size - 1 &&
(isdigit(ch) || ch == ':'); ++j)
{
if (j + 1 == 10)
{
result[i + j] = '\0';
return true;
}
if ((ch = getc(in)) == EOF)
{
result[i + j] = '\0';
return false;
}
result[i + j] = ch;
}
i += j;
}
result = '\0';
return false;
}
 
D

Darren Cubitt

qarnos said:
I don't have access to a C compiler right this minute, so I can't test
your code but I would ask one question:

What are the contents of the input file? If the first character is
neither a digit or a colon, you will get no output. I have a hunch
your file might begin with a blank line, which would mean the first
character is "\n".

Actually, scratch that comment (I'm the same poster - just posting from
my home account now).

I got my closing curly braces mis-matched when I made that comment.

Trying the program now and it seems to work. By "work" I mean, I have no
idea what it supposed to do (since you didn't tell us), but it looks
like you are trying to extract 10-character sequences of digits and colons.

There are problems I can see with your code where I don't think it is
going to do what you expect it to do. But again, I'm not sure what you
are expecting.

Your program wont work if you only input a short (< 10) digit sequence
since you are returning false as soon as an EOF is seen. Is this the
behaivior you wanted?
 
F

Franken Sense

In Dread Ink, the Grave Hand of Mark McIntyre Did Inscribe:
why are you comparing a bool to zero?

Surely you want to loop while get_script is returning data. Currently it
loops as long as false > 0. Is it?

I thought it would work since 1 > 0.
why return false if the function succeeded?

That looked odd to me too. I think I best just start from scratch.
--
Frank

I once asked the most fabulous couple I know, Madonna and Guy Ritchie, how
they kept things fresh despite having been married for almost seven months.
'It's a job, Al,' Guy told me. 'We work at it every day.'
~~ Al Franken,
 
F

Franken Sense

In Dread Ink, the Grave Hand of Darren Cubitt Did Inscribe:
Actually, scratch that comment (I'm the same poster - just posting from
my home account now).

I got my closing curly braces mis-matched when I made that comment.

Trying the program now and it seems to work. By "work" I mean, I have no
idea what it supposed to do (since you didn't tell us), but it looks
like you are trying to extract 10-character sequences of digits and colons.

There are problems I can see with your code where I don't think it is
going to do what you expect it to do. But again, I'm not sure what you
are expecting.

Your program wont work if you only input a short (< 10) digit sequence
since you are returning false as soon as an EOF is seen. Is this the
behaivior you wanted?

Thanks, Darren. Meanwhile the problem has changed somewhat. I'm no longer
interested in the first ten charcters, but only the first, which I can deal
with in a K&R getchar() manner.

I thought I would try to call the boolean routine just for kicks and
giggles. It represents a complication which I don't need.
--
Frank

And just like in 1984, where the enemy is switched from Eurasia to
Eastasia, Bush switched our enemy from al Qaeda to Iraq. Bush's War on
Terror is a war against whomever Bush wants to be at war with.
~~ Al Franken,
 
F

Franken Sense

In Dread Ink, the Grave Hand of rio Did Inscribe:
if i have that problem i compile the file with Borland compiler
bcc32 -v thatfile.c
and use Borland graph, not text only, debugger
or
something equivalent (with option for debug symbols)
with some compiler-graph (not text only) debugger

I think I have a Borland disc. I sure miss having a visual debugger. I
find gdb.exe, gcc's debugger, austere.
--
Frank

[G. W. Bush's] pro-air pollution Clear Skies Initiative is designed to
clear the skies of birds.
~~ Al Franken,
 
J

James Kuyper

Franken said:
In Dread Ink, the Grave Hand of Mark McIntyre Did Inscribe:


I thought it would work since 1 > 0.

But if you're bothering to declare it as returning bool rather than int,
and if all of your return statements use either true or false, you
should be comparing its return value with true or false, rather than
numeric values. This is purely a style issue; but then so is the use of
bool in the first place.
 
K

Keith Thompson

James Kuyper said:
But if you're bothering to declare it as returning bool rather than
int, and if all of your return statements use either true or false,
you should be comparing its return value with true or false, rather
than numeric values. This is purely a style issue; but then so is the
use of bool in the first place.

No, you should never declare a boolean value to true or false. Just
test the value.

Rather than "if (cond == true)", just write "if (cond)".
Rather than "if (cond == false)", just write "if (!cond)".

(But "len" is an odd name for a boolean anyway.)
 
J

jameskuyper

Keith said:
No, you should never declare a boolean value to true or false. Just

declare => compare ?
test the value.

Rather than "if (cond == true)", just write "if (cond)".
Rather than "if (cond == false)", just write "if (!cond)".

Personally, I agree - but some people prefer the other forms for the
same reason they prefer ptr!=NULL. While I don't share their opinion,
I think they've got enough of an argument for their approach that I
usually don't bother disagreeing with them.
 
K

Keith Thompson

jameskuyper said:
declare => compare ?

Yes, thanks.
Personally, I agree - but some people prefer the other forms for the
same reason they prefer ptr!=NULL. While I don't share their opinion,
I think they've got enough of an argument for their approach that I
usually don't bother disagreeing with them.

My own opinion that it's a bad idea is somewhat stronger. In the
absence of an actual boolean type, comparing a condition to true (or
TRUE, or 1) is dangerous; if the value is non-zero but something other
than 1, you'll get the wrong result. Even if you're guaranteed that
the value is either 0 or 1, comparing to true or false is at best
superfluous.

The point of writing "if (ptr != NULL)" rather than "if (ptr)" is that
"ptr" is a pointer expression, while "ptr != NULL" is clearly a
condition. (The language lets you treat "ptr" as a condition, but I
personally prefer not to take advantage of that.) "cond" is *already*
a condition; if "if (cond == true)" is clearer than "if (cond)", then
surely "if ((cond == true) == true)" is even clearer than that.

This is slightly complicated by the fact that, even though C99 has an
actual boolean type, the operators still yield results of tpye int.
But the principle is the same.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top