Thread: Help with structs and file input/output

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Victoria, BC
    Posts
    2

    Help with structs and file input/output

    Hey,

    I have this assignment that asks us to take an input file, like, say an e-book, and then from that generate an output file that contains output like as follows:

    Code:
    Word count: 1234
    Line count: 1234
    Character count: 1234
    
    A: 50
    ***
    B:90
    ****
    C: 200
    *********
    Where the asterisks represent a normalized count of the letters. (Like a horizontal bar graph.)

    I was having trouble understanding structs and file output and input, so I asked my boyfriend's friend for help, but the code doesn't compile on my end. This isn't finished yet. Actually, I'm pretty lost on the entire assignment, but getting this to compile properly first is kind of a priority for me haha.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <stdbool.h>
    #include <string.h>
    
    #define  MAX_LINE  (200)
    #define  NO_LETTERS (26)
    
    typedef struct{
        int ch; //actual letter
        int acnt; //actual count of letter in a file
        int ncnt; //normalized count of letter in file
    }HistoItem;
    
    typedef struct{
        int letters; //# chars
        int words; //#words
        int lines; //#lines
        HistoItem h[NO_LETTERS]; //letter frequencies
    } Histogram;
    
    void processWord(Histogram * h, char * line){
        printf("Process Word line: %s",line);
        char * pch;
        pch = strtok(line, " \r\n");
        while(pch != NULL)
        {
            h->words ++;
            printf("pch: %s\n",pch);
            pch = strtok (NULL, " \r\n");
        }
        //free(pch);
    }
    
    void processLine(Histogram * h, char * line){
       h->lines ++;
       //printf("Lines: %d\n",h->lines);
       processWord(h, line);
            //read
            //process word
            //write word
    }
    
    void processFile(FILE * ifp, FILE * ofp){
        int c, ln = 0;
        char line[MAX_LINE];
        Histogram h;
        h.letters = 0;
        h.words = 0;
        h.lines = 0;
        while(c != EOF){
            c = fgetc(ifp);
            printf("%d\n",c);
            line[ln] = c;
            ln ++;
            h.letters ++;
            if(c == 10)
            {
                processLine(&h, line);
                int i = 0;
                for(i=0;i<MAX_LINE;i++)
                    line[i] = NULL;
                ln = 0;
            }
            //read char
            //letters++
            //process line
            //write line
        }
        char outline[MAX_LINE];
        sprintf(outline,"Lines: %d\nWords: %d\nLetters:%d",h.lines,h.words,h.letters);
        fputs(outline,ofp);
    }
    
    //copied this stuff from the last assignment. nts: remember to change it and not be a fag
    int main(void) {
        char fnaminp[MAX_LINE];
        char fnamout[MAX_LINE]="output.txt";
        FILE *ifp;
        FILE *ofp;
    
        printf("Enter file name (file extension: .txt): ");
        fflush(stdout);
        scanf("%s", fnaminp);
        printf("Input file name: %s\n", fnaminp);
        printf("Output file name: %s\n", fnamout);
    
        ifp = fopen (fnaminp, "r");
        if (ifp != NULL) {
                ofp = fopen (fnamout, "w");
            if (ofp != NULL) {
                processFile(ifp, ofp);
            } else {
                printf("Cannot open output file %s\n", fnamout);
            }
                fclose(ofp);
        } else {
            printf("Input file %s not found\n", fnaminp);
        }
        fclose(ifp);
        fclose(ofp);
        system("PAUSE");
        return EXIT_SUCCESS;
    } /* main */
    When I try to compile it using gcc in the Eclipse IDE it spits out a bunch of numbers and then an error (*** glibc detected *** /home/jessica/workspace/a62/Debug/a62: double free or corruption (out): 0x0000000000603250 ***)

    Even though that error pops up, the output file is still written to with

    Lines: 1
    Words: 8
    Letters:54

    Which is wrong, but still interesting.

    If you could help me out, it would be greatly appreciated.

    Thanks,

    Jessica

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Jessica, your code is a pleasure to read. I had 1 compile warning and 1 runtime problem.
    And they are both here...
    Code:
     
    
            if(c == 10)  // insufficient logic here : 
                         // will not process last line
            {
                processLine(&h, line);
                int i = 0;
                for(i=0;i<MAX_LINE;i++)
                    line[i] = NULL;  //warning here : integer to pointer
    
                ln = 0;
            }
    I just substituted \0 for NULL for the warning.

    As an aside, int i is superfluous as you already have ln... just walk it backwards.
    Last edited by Tclausex; 11-12-2011 at 07:04 PM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by Tclausex View Post
    Jessica, your code is a pleasure to read.
    LOL of course, I see this after..
    Code:
    //copied this stuff from the last assignment. nts: remember to change it and not be a fag

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    If you can not open a file in the first place, then you do not need to close it. You also call fclose(ofp) twice if ifp != NULL. Think of fopen and fclose kind of like malloc and free. You don't want to free something twice or free something that was never successfully malloced space. Likewise, you don't want to close something that was never opened and you don't want to close something that has already been closed.

    Code:
    if (ifp != NULL) {            
            ofp = fopen (fnamout, "w");
            if (ofp != NULL) {
                processFile(ifp, ofp);
            } else {
                printf("Cannot open output file %s\n", fnamout);
            }
                fclose(ofp); /* Once, as long as ifp isn't NULL */
    
        } else {
            printf("Input file %s not found\n", fnaminp);
        }
        fclose(ifp); /* You call, this even though you may not have opened ifp in the first place */
        fclose(ofp); /* Second time */

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Victoria, BC
    Posts
    2
    Hey, thanks for the help! The errors are gone now. I didn't even notice that fclose was called twice, but that's all fixed up now.

    Now, what's interesting, is that in my output file, there's still no output. I went to
    Code:
        char outline[MAX_LINE];
        sprintf(outline,"Lines: %d\nWords: %d\nLetters:%d",h.lines,h.words,h.letters);
        fputs(outline,ofp);
        printf(outline); // put this here to make sure that outline is actually recording the data
    }
    and decided to print outline to the console to see if it was ever being written to. It is (the output is wrong, but I'll fix that after), but it never actually is written to output.txt.

    Does anyone have any idea why this might be happening? Is fputs not the right function to call in this situation?

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    What changes did you make? Keep your input data file simple - like 2 lines. Try and debug with some simple printf-s inserted in strategic locations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ File Input Output VS C file Input Output
    By forumuser in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2009, 06:46 AM
  2. File Input/output
    By JJFMJR in forum C++ Programming
    Replies: 4
    Last Post: 07-09-2007, 07:50 AM
  3. file input/output
    By Bigbio2002 in forum C Programming
    Replies: 3
    Last Post: 10-29-2003, 01:45 AM
  4. file input/output
    By Raison in forum C Programming
    Replies: 3
    Last Post: 10-12-2003, 10:19 PM
  5. File Input / Output Help?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-18-2002, 10:20 AM