File scan not working

This is a discussion on File scan not working within the C Programming forums, part of the General Programming Boards category; I want this program to scan a text file (given as an argument) and just put the alpha characters it ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    182

    File scan not working

    I want this program to scan a text file (given as an argument) and just put the alpha characters it finds. Why isn't it working? Thanks!

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(int argc, char *argv[])
    {
        int ch;
        FILE *input = NULL;
        
        if(!(input = fopen(argv[1], "r"))) {
            printf("Error opening file!\n");
            return 1;
        }
        
        while(ch = fgetc(input) != EOF) {
            if(isalpha(ch))
                putchar(ch);
        }
        
        return 0;
    }
    Last edited by samus250; 09-27-2008 at 12:49 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    You've written this
    while(ch = (fgetc(input) != EOF))

    You want this
    while((ch = fgetc(input)) != EOF)

    Note the use of ( )
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    It works now. Thanks!

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Well that program works fine to me!! may be place a getchar before the return 0.

    EDIT: Perhaps, I did place the brakets when i tested that code. But dint mentioned it here lol. YES you need to brakets!

    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21