Thread: File IO and structs

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    100

    File IO and structs

    I'm trying to pass a filename and go through every character in the file and create an array of 127 nodes and increment the weight of that node's element in the array based on the ASCII value of the character that was read in...But this is the error I'm getting atm:

    test.c: In function âmainâ:
    test.c:16: warning: initialization from incompatible pointer type
    test.c: In function âcreateFreqTableâ:
    test.c:25: error: request for member âweightâ in something not a structure or union
    test.c:34: error: request for member âweightâ in something not a structure or union
    test.c:37: warning: comparison between pointer and integer
    -bash-4.1$


    Here is my code:
    Code:
        #include <stdio.h>
        #include <stdlib.h>
        
            typedef struct tnode {
            double weight;
            int c;
            struct tnode* left;
            struct tnode* right;
            struct tnode* parent;
        } singleNode;
        
        singleNode* createFreqTable(char* filename);
        
        int main(int argc, char *argv[]) {
        
        struct singleNode* leafNodes = createFreqTable("test.txt");
        
        
        return 0;
    }
        
        singleNode* createFreqTable(char* filename) {
            singleNode *freqArray = malloc(127*sizeof(singleNode));
            for(int i = 0; i < 127; i++){
                *(freqArray+i).weight = 0;
            }
            FILE *f;
            f = fopen(filename, "r");
            int temp = 0;
            do {
                temp = fgetc(f);
                for(int i = 0; i <= temp; i++){
                    if(temp == i){
                        *(freqArray+i).weight++;
                    }
                } 
            } while (f != EOF);
        fclose(f);
        return freqArray;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quick guess this is wrong.


    Code:
    struct singleNode* leafNodes = createFreqTable("test.txt");
    This is right, maybe

    Code:
    singleNode* leafNodes = createFreqTable("test.txt");
    Reason singleNode is a typdef NOT a struct.

    Your compiler says this is wrong
    Code:
    *(freqArray+i).weight = 0;
    This might be right; but, most will still say it is not elegant; I suggest -> operator.
    Code:
    (*(freqArray+i)).weight = 0;
    Tim S.
    Last edited by stahta01; 03-02-2013 at 11:57 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    Quote Originally Posted by stahta01 View Post
    Quick guess this is wrong.


    Code:
    struct singleNode* leafNodes = createFreqTable("test.txt");
    This is right, maybe

    Code:
    singleNode* leafNodes = createFreqTable("test.txt");
    Reason singleNode is a typdef NOT a struct.

    Your compiler says this is wrong
    Code:
    *(freqArray+i).weight = 0;
    This might be right; but, most will still say it is not elegant; I suggest -> operator.
    Code:
    (*(freqArray+i)).weight = 0;
    Tim S.
    That solved that problem, but what about on line 37, it says:
    test.c:37: warning: comparison between pointer and integer

    I'm really inexperienced when it comes to file IO in C

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    while (!feof(f)) seems to compile

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by johngoodman View Post
    while (!feof(f)) seems to compile
    Please read FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Also posted here.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary File Structs
    By csharp100 in forum C Programming
    Replies: 9
    Last Post: 04-12-2012, 02:44 PM
  2. Information from a file and structs?
    By equitrainer86 in forum C Programming
    Replies: 0
    Last Post: 03-02-2010, 07:42 PM
  3. Parsing XML file into C structs
    By jaaaaakke in forum C Programming
    Replies: 7
    Last Post: 04-30-2009, 08:40 PM
  4. File I/O and structs
    By Evandb in forum C Programming
    Replies: 6
    Last Post: 10-13-2004, 09:40 PM
  5. Reading from file into structs..
    By dankas in forum C Programming
    Replies: 14
    Last Post: 10-16-2002, 10:33 PM