Troubleshooting Program that Won't Stop - Help Appreciated!

In summary, the programmer is trying to read from a file and is getting an error because the character type fgetc() returns is int and not char. The programmer can either use fgets() which returns a char directly or use ferror() to check for read errors.
  • #1
Nobody
18
1
I have a small code snip as included below, please have a look at it and please tell me why my program doesn't stop...I am really thankful for your answers...

char ch;
while((ch=fget(fp)!=EOF){
// print out values of ch
}
 
Computer science news on Phys.org
  • #2
It works!
Check whether there is some other error!

-- AI
 
  • #3
BTW, I would generally strongly caution against using loop conditions that have side-effects, like this one.

- Warren
 
  • #4
The "error" is that 'ch' is of type char. It might not be what is causing your problems, but it is nevertheless an error.
When fgetc() returns EOF it is an int. You are casting an int to a char, then casting it back to an int to compare with EOF. This is not safe. The original int(the code for EOF) might not be the same after beeing casted from int to char and then back to int.

Here are two ways to deal with fgetc() which returns an int.
Code:
int ch;
while( ( ch = fgetc(fp) ) != EOF )
This is perfectly valid.

This is probably a bit better:
Code:
char ch;
while(1)
{
    ch = fgetc(fp); /* Casting from int to char is ok here */

    if ( feof(fp) )    /* Because we use feof() to check the stream */
      break;

    if ( ferror(fp) )  /* We can also check for read-errors */
        break; /* And handle them if we want */

    /* Work with 'ch' here */
}
 
  • #5
Thank you for your answers, I am still a bit curious about why fgetc() reads only characters but returns an integer ? Do you know why ?
 
  • #6
What I mean to ask is people who created fgets, might also think of making it return a char so that it would be more compatible and easier for programmers to remember, but they didn't do so ? why ?
And EOF is actually a -1 but why -1 exists at the end of tha file ?
 
Last edited:
  • #7
The problem is that there is really no such thing as an "EOF char." All 256 possible values of char are perfectly valid anywhere in a file, and none of them actually mean "end of file." If they did, you would not be able to store the integer -1 in a binary file! Instead, the operating system stores the length of files in a file's metadata, and sets a flag when you attempt to read past the end of an open file. The various file I/O functions have to report to you when an flag (such as EOF) occurs, but they can't do so just by using a special char. Instead, fgetc() mucks about with ints. This is why it's much preferred to actually check the file's status explicitly by using something like ferror().

- Warren
 
  • #8
Thanks a lot for your help, but anyone has any other ideas ? Please tell me..
 
  • #9
1. fgets() returns a (char *), which it obviously must.

If you for some reason suddenly has become affraid of using fgetc() because it returns an integer, you can use fgets() to get that char directly without casting.
But, it's not dangerous to cast the initial integer to a char. It's when you start casting it back and forth that strange things might occur.

char foo;

foo = fgetc(fp);
/* perfectly valid, just feof() to check for eof on the stream */
It's the exact same as foo = (char) fgetc(fp);

Or, you could always do:
char *foo;

while ( (fgets(foo, 1, fp) ) != EOF )


But, that's just silly.

What you really should do is to create a buffer:
char buf[255];

Then read into that buffer:
while( ( fgets(buf, 255, fp) ) != EOF )
{
/* work with buf */
}


The point beeing that reading from a file stream requires a low-level call, and the more you do it, the more resources you use.

Here is some code I wrote to count the number of chars in a file stream using fgets() instead of fgetc():
Code:
#include <stdio.h>

int main(void)
{
  FILE *foo;
  int i = 0;
  char buf[512];

  foo = fopen("/Users/tor/Library/Mail/Mailboxes/Apple Lists/OS X Server.mbox/mbox", "r");

  if(!foo)
    exit(-1);

  while( fgets(buf, 255, foo) != 0 ) /* 0 means error or EOF, check man fgets */
      i += strlen(buf);

  printf("%d\n", ++i);

  return 0;
}
This code is not 100% safe, because there might occur errors after x bytes have been read from the stream. But, a simple call to ferror(fp) will fix that.
 
  • #10
Thank you, I'd like your explanation...
I looked at your code, I like apple, I will save money to buy one then. Thanks again.
 

1. Why is my program not stopping?

There could be several reasons why your program is not stopping. One common reason is that there is an infinite loop in your code, causing it to continuously run without reaching a stopping point. Another possibility is that there is a bug or error in your code that is preventing it from stopping. Additionally, if your program is waiting for a user input or response, it may appear to not be stopping because it is waiting for further instructions.

2. How do I troubleshoot a program that won't stop?

To troubleshoot a program that won't stop, start by checking for any infinite loops in your code. You can also try debugging your code by using print statements or a debugger tool to track the flow of your program and identify any errors. Additionally, make sure all necessary inputs and dependencies are properly defined and accounted for in your code.

3. Can I force my program to stop?

Yes, you can force your program to stop by using the "force quit" or "end task" function on your computer's operating system. However, this should only be used as a last resort and may result in unsaved data or potential damage to your program.

4. How can I prevent my program from getting stuck in an infinite loop?

To prevent your program from getting stuck in an infinite loop, make sure to thoroughly test your code and check for any potential errors or logical mistakes. You can also use conditional statements and break or exit functions to ensure that your program will stop once a certain condition is met.

5. What should I do if I am unable to find the issue causing my program to not stop?

If you are unable to find the issue causing your program to not stop, consider seeking help from a more experienced programmer or consulting online resources and forums. It may also be helpful to take a step back and approach the problem from a different angle, or to break down your code into smaller sections to isolate the issue.

Similar threads

  • Computing and Technology
2
Replies
38
Views
5K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
878
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
3
Replies
73
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
939
  • Programming and Computer Science
Replies
30
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
4K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
32
Views
2K
Back
Top