getc or not getc that is the problem

This is a discussion on getc or not getc that is the problem within the C Programming forums, part of the General Programming Boards category; I have a slight problem trying to read characters from a file on Win. The bit of code I am ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    2

    Talking getc or not getc that is the problem

    I have a slight problem trying to read characters from a file on Win. The bit of code I am using is:

    characterCount = 0;
    ch = getc ( filepointer );

    while ( ch != EOF )
    {
    while ( ch != characterCount )
    {
    if ( ch > characterCount )
    characterCount++;
    else
    if (ch < characterCount )
    characterCount--;
    }
    charNo [ characterCount ]++;

    ch = getc ( filepointer );

    }

    However, this only records ASCII 10 for the carriage return new line combination at the end of text lines not ASCII 10 and ASCII 13. This means that my character count is short some 6000 characters in 380,000.

    Anyone know a safe solution to this (I am told it is an old chestnut on Win...)

    Cheers

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    If you just open a file using "r", then it is a text file, and translation from the operating system line termination (\n\r) to the ANSI-C standard (\n) will apply (you've seen this)

    If you open the file in binary mode ("rb"), then this translation effect is turned off, and you should see every character in the file.

    > while ( ch != characterCount )
    What's this do?
    Apart from an expensive way of saying characterCount = ch;

    This being so, why not do
    charNo [ ch ]++;

    > while ( ch != EOF )
    Try
    Code:
    while ( (ch=fgetc ( filepointer )) != EOF ) {
        charNo[ch]++;
    }
    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
    Nov 2001
    Posts
    2
    Thanks very much for your advice Salem; I will give that a go.

    Does opening the file as read binary mean that as far as C is concerned binary reading and text-reading are basically the same??

    I was just about to write some rubbish about looping through the array -- until I realised that the array pointer is always at the start DOH! Must be too used to linked lists I suppose ^_^

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > Does opening the file as read binary mean that as far as C is concerned binary reading and text-reading are basically the same?
    No

    If you try and read a text file, opened in binary mode, using fgets (which is a text reading function), then there could be some surprises in store.

    If its a binary file, then you use fgetc (single chars) or fread (a fixed block of chars).

    You can also use fgetc to read single chars from a text file, and use fgets to read a whole line (most text file operations work with single lines at a time).

    I think you can read a text file using fread, but I don't know why you would want to.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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