Reputation: 13
I know there are probably a crapload of other problems, but I can't really troubleshoot until I get the segmentation fault to go away. I'm pretty sure this has to do with my use of mallocs, but I'm not sure where exactly it's coming from. I'm still new to coding, so any other advice is also appreciated. The test file I'm using is just a single line that reads "The quick brown fox jumps over the lazy old dog," but it should be able to work for anything. All other information should be in the code annotations.
//This program is supposed to act as line justification formater.
//When running the program, the user will specify what justification
//type they want (either left, right, or center) and the column width.
//The program will output the text of the file broken up over several
//and justified appropriately.
#include <stdio.h>
#include "scanner.h"
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){
FILE *fp = fopen("argv[2]","r");
int checkArguments(char[],int); //function declarations
char getString (FILE *);
int justify(char,int,int,char **);
char *justif=argv[4]; //these two variables hold the width and length
char *widthC=argv[3]; //that the user puts in when they run the function
int width=atoi(widthC);
int wordsSum=0; //this variable holds the total number of wordis
int i;
checkArguments(justif,width);
char **words = malloc(sizeof(char *)*1); //this string will hold each word as a token
for (i=0;;i++){
if (feof(fp)) break;
else {
char **temp;
wordsSum++;
temp=realloc(words,(i+1)*sizeof(char*));
if(temp!=NULL){ //this reallocates the memory for words
words=temp; //and if the reallocation was succsesful
char tempString=getString(fp); //gives words a new token
words[i]=&tempString;
i++;
}
else {
free(words);
printf("Malloc error :c\n");
return 0;
}
}
}
justify(*justif,width,wordsSum,words);
fclose(fp);
return 0;
}
/*This program does two simple checks to make sure the user put in valid widths and lengths */
int checkArguments(char a[], int width){
if (!(strcmp(a,"right")==0 || strcmp(a,"left")==0 || strcmp(a,"center")==0)){
printf("You did not enter a possible justification, please try again.");
exit(0);
}
else if(width<=0){
printf("You did not enter a possible width, please try again.");
exit(0);
}
else return 0;
}
/*This program reads the file and stores characters of one word inside a malloc string
It then does a malloc realocation check and, if sucsessful, returns the word*/
char getString (FILE *fp){
char c;
int i;
char *s=malloc(sizeof(int));
char *temp;
for (i=0;;i++) {
c=fgetc(fp);
if (c==' ' || c=='\n')
break;
s[i]=c;
temp=realloc(s,(i+1)*sizeof(int));
if(temp!=NULL){
s=temp;
}
else {
free(s);
printf("Malloc error :c\n");
return 0;
}
}
return *s;
}
/*This function will eventually have another if else statement at the beggining, and
print different amounts of spaces in the beggining of the line depending on what
justification the user wants. For now it assumes left justified.*/
int justify(char justif, int width, int wordsSum, char ** words){
int i=0,j=0,k=0;
while(i<wordsSum) //while there are more words to print
{
while(j<=width) //while not at the maximum width for lines
{
if(j+strlen(words[i])<=width){ //find if adding another word would put
j+=strlen(words[i]); //the line width over the maximum width
i++; //if no, add another word
}
else{ //if yes, print the current line
for (k=0;k<=j;k++)
printf ("%s",words[k]);
printf("\n");
j=0; //reset the line length to zero
}
}
}
return 0;
}
Upvotes: 0
Views: 938
Reputation: 72619
After you fixed the problems pointed out by ameyCu, there's this
if (feof(fp)) break;
which does not do what you think it does since the end-of-file marker is only ever set after an I/O operation. You cannot detect an empty file by checking feof(fp)
immediately after fopen
. You must check the success of each input operation, i.e each fgetc
.
Upvotes: 0
Reputation: 16607
FILE *fp = fopen("argv[2]","r");
Should be -
FILE *fp = fopen(argv[2],"r");
if(fp==NULL)
printf("error in opening file");
You again point words
to temp
hence you loose reference to memory allocated to it using malloc
previously .Take care of this else previously allocated memory will not be freed .
In function char getString (FILE *fp)
you allocate char *s
memory equal to sizeof(int)
. Was it on purpose ?
Upvotes: 2