Like Tree1Likes
  • 1 Post By jimblumberg

File IO and structs

This is a discussion on File IO and structs within the C Programming forums, part of the General Programming Boards category; I'm trying to pass a filename and go through every character in the file and create an array of 127 ...

  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
    2,582
    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 10:57 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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
    2,582
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    Also posted here.

    Jim
    Salem likes this.

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, 06: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

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