Thread: valid test expression?

  1. #1
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463

    valid test expression?

    I know this is a simple question, but I have tried something similar in a program and doesn't work, only giving me an infinite loop. Is this expression a valid one for controlling loops?

    Code:
    while((c=fgetc(fp)) != '\n')

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    If memory serves me correctly \n is not one character but two.

    \n executes both a line feed and carriage return (10 and 13 I think).

    Edit: Looked at a file, its 13 and 10. And to answer directly no its not a valid control

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    would there be a way to test for reading a file to the end of a line character by character?

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    off the top of my head:
    Code:
    while ( (c=fgetc(fp)) != 10)
    There are better ways to check for the end of a line but if you are set to do it by getting one character then that should work.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Why not just read in the line using fgets( ) and then go through it character by character.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Thantos
    If memory serves me correctly \n is not one character but two.
    No. It's one. \ is an escape character which doesn't actually count. You treat escaped character combos as a single character.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    \n is two characters on a Windows system. With Windows, '\n' is a carriage return followed by a line feed.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by XSquared
    \n is two characters on a Windows system. With Windows, '\n' is a carriage return followed by a line feed.
    Arg! No it isn't! When you use '\n' in C, it is a single character. The only time it's "two characters" is on disk, which is nothing you ever check for anyway. And even then, if you open in binary mode, you can read them as single characters anyway. Once it is read, it is simply treated as a single character. It is a single character, which is why you can stick it in a char. If it wasn't, you couldn't.

    [edit]
    To clarify, in Windows, if you write in text mode, it writes a \n\r pair. However, \n is its own character, and \r is another. \n itself is never more than one character.
    [/edit]

    Quzah.
    Last edited by quzah; 08-06-2003 at 07:32 PM.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    I don't understand. Why isn't
    Code:
     while((c=fgetc(fp)) != '\n')
    valid for controlling loops, yet
    Code:
     while ( (c=fgetc(fp)) != 10)
    is?

    Aren't they exactly the same to the compiler? Also, I can't see anything wrong with the loop (provided that c is declared as an int and not a char, and that EOF is being checked within the loop body at the beginning)

    Cheers,
    Z.
    Beware the fury of a patient man.

  10. #10
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    thanks for the clarification. Here's my final, complete code segment. It looks to me like it's ok, but I don't want to run into any more infinite loops and gigabyte size text files :-D.

    Code:
    while(check=fread(&buf,sizeof(char),3,fpin)!=EOF || 0)
       {
        while(buf=fgetc(fpin) != '\n'|| EOF)
          {
           if(fwrite(&buf,sizeof(char),1,fpout)==0)
             {
              printf("Disk write error");
              exit(EXIT_FAILURE);
             }
          }
       }

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    while(check=fread(&buf,sizeof(char),3,fpin)!=EOF || 0)
    That's wrong. It should be:

    while( fread(...) == 3 )

    First off, you don't actually do anything with 'check', so why bother using it?
    Second, "|| 0" will never evaluate to true, so you don't want that.

    [edit]
    The second loop check is wrong also for the same reason. The logic of the check is wrong.
    [/edit]

    Quzah.
    Last edited by quzah; 08-07-2003 at 05:13 PM.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    thanks for the help, no more infinite loops!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed to verify a new on-line C test
    By JanHruska in forum Projects and Job Recruitment
    Replies: 15
    Last Post: 06-20-2009, 06:48 AM
  2. Creating C/C++ Unit Test Cases
    By chiefmonkey in forum C++ Programming
    Replies: 1
    Last Post: 04-28-2009, 08:29 PM
  3. Expected compile-time constant expression
    By Elysia in forum C++ Programming
    Replies: 4
    Last Post: 09-27-2008, 01:36 AM
  4. Help with making a Math Expression DLL
    By MindWorX in forum C Programming
    Replies: 19
    Last Post: 07-19-2007, 11:37 PM
  5. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM