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