Errors when compiling. its seem to be complaining about line 52 and i do not get output of second file.(data.words)
strarray[numStrings++]=strdup(word);
$ gcc -Wall -ansi prt.c -o prt
In function ‘main’:
warning: implicit declaration of function ‘strdup’
warning: assignment makes pointer from integer without a cast
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compare (const void * a, const void * b)
{
const char **ia = (const char **)a;
const char **ib = (const char **)b;
return strcmp(*ia, *ib);
/* return -1;*/
}
int main(int argc, char *argv[])
{
if ( argc != 3)
{
printf("Usage:\n %s CharLimit File \n",argv[0]);
printf("\tCharLimit is Character Limit b/w 25 & 100\n");
printf("\tFile is Input File Name\n");
printf("\n");
return 0;
}
int limit = atoi(argv[1]);
FILE *fp;
FILE *fpout;
char **strarray = NULL;
char word[50];
int str_len=0;
int length = 0;
int count=0,numStrings=0;;
fpout = fopen("data.out", "w");
fp = fopen(argv[2], "r");
if (fp == NULL)
{
printf("file does not exist\n");
return 0;
}
else
{
while (!feof(fp))
{
fscanf(fp,"%s",word);
count = count + 1;
str_len = strlen(word);
strarray = (char **) realloc (strarray, (numStrings+1) * sizeof(char *));
strarray[numStrings++]=strdup(word);
length = length+str_len;
if(length>limit)
{
fprintf(fpout,"%c",'\n');
fprintf(fpout, "%s ", word);
length = str_len;
length++;
}
else
{
fprintf(fpout, "%s ", word);
length++;
}
strcpy(word,"");
}
}
fclose(fpout);
fclose(fp);
qsort (strarray, numStrings, sizeof(int), compare);
int tempindex,i;
char **temp = NULL;
temp=(char **) realloc (temp, (numStrings) * sizeof(char *));
FILE *fout;
fout = fopen("data.words", "w");
for(i=0;i<numStrings;i++){
tempindex=1;
while(1){
if(i+1<numStrings && strcmp(strarray[i],strarray[i+1])==0){
tempindex++;
i++;
}
else{
fprintf(fout,"%s - %d\n", strarray[i],tempindex);
break;
}
}
}
fclose(fout);
return 0;
}