Thread: Reading chars?

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    13

    Reading chars?

    Hey all. Trying to search through a text file and recognize when I hit an 'M', but it continues to read through the entire file. What am I missing?

    Code:
            while  (x = fgetc(fp) != EOF) {
             fseek(fp, (sizeof(char)*offset), SEEK_SET);
              if (x == 'M') {
               break;
              }
             printf("x = %c at pos %ld\n", fgetc(fp), ftell(fp));
             offset++;
            }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > while (x = fgetc(fp) != EOF)
    If you want this to be the char read, you need extra ()

    = has lower precedence than !=

    So
    while ( (x = fgetc(fp)) != EOF)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2019
    Posts
    13
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading chars.
    By cdummie in forum C Programming
    Replies: 8
    Last Post: 04-11-2015, 07:13 AM
  2. reading in chars from file
    By AJOHNZ in forum C++ Programming
    Replies: 2
    Last Post: 08-16-2009, 12:50 AM
  3. reading chars with scanf
    By owi_just in forum C Programming
    Replies: 3
    Last Post: 04-23-2005, 11:48 PM
  4. Re-reading chars from stdin
    By Uncle Albert in forum C Programming
    Replies: 5
    Last Post: 04-01-2005, 11:28 AM
  5. Reading in a String of Chars
    By gqchynaboy in forum C Programming
    Replies: 33
    Last Post: 09-22-2004, 12:33 AM

Tags for this Thread