Thread: C Program Help Reading in a file of Chars until EOF

  1. #1
    Registered User
    Join Date
    Apr 2013

    C Program Help Reading in a file of Chars until EOF

    Hi, I'm working on a homework project, and it requires me to read in a file of chars into an array, and then do stuff with that array. I've been in java programming mode for a while, so forgive me for my C's been a while...

    Anyways, I have the first part written, where I'm just trying to read in my data.txt file, and I thought I had it written well. It compiles, but then it seg faults, and I'm not sure why. I used calloc for the array, but maybe I misused it? Or is it in my EOF statement? I'm still not sure if that's coded correctly. I need to get past this so I can start testing the other parts of my code. Below is my code. Any help would be appreciated!

    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char* argv[] )
    /* Local Declarations*/
    int i;
    int *ptr;
    char tempc;
    /*Allocate enough space to hold each char*/
    int BigArray[26] = {0};
        if(!(ptr=(int*)calloc (26, sizeof(char))))
    /* Open the file to read*/
        FILE *f = fopen(argv[1], "r");
    /*Fill the Array with data from the file*/
        while(1 == 1)
            fscanf(f, "%c", &tempc);
            if(tempc == EOF)
            for(i = 0; i < 26; i++)
                if(BigArray[i] == tempc)
            else if(BigArray[i] == 0)
                BigArray[i] == tempc;
            } /*End for to check if in the array */
        } /* End while to load the array */
    /*Print statement to check that my array was filled correctly */
    for(i = 0; i < 26; i++)
    {printf("%c", BigArray[i]);}
    return 0;
    } /* End of Program */

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > if(!(ptr=(int*)calloc (26, sizeof(char))))
    What is this for? It is never used.

    > fscanf(f, "%c", &tempc);
    > if(tempc == EOF)
    > break;
    if ( fscanf(f, "%c", &tempc) == EOF ) break;

    The end of file indication from file reading functions comes from checking the return result of a function, not checking the data.

    > BigArray[i] == tempc;
    You really need to watch the use of = vs ==.

    Turning up the warning level on most compilers will now show this problem.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in select chars from a file
    By B.Grills in forum C Programming
    Replies: 6
    Last Post: 09-30-2012, 03:35 PM
  2. Reading chars from file into array
    By AJOHNZ in forum C++ Programming
    Replies: 1
    Last Post: 08-19-2009, 03:37 PM
  3. reading in chars from file
    By AJOHNZ in forum C++ Programming
    Replies: 2
    Last Post: 08-16-2009, 12:50 AM
  4. reading and writing unsigned chars from a file
    By yahn in forum C++ Programming
    Replies: 17
    Last Post: 02-20-2006, 09:42 PM
  5. Re-reading chars from stdin
    By Uncle Albert in forum C Programming
    Replies: 5
    Last Post: 04-01-2005, 11:28 AM