i have some code, but i am getting the nasty segmentation fault. this lil error alone makes me hate C. everytime im working with arrays seg fault gives me a headache. this time i cant find a solution for it. i know it happens if you try to access an array beyond the end of the array, but i dont see where im doing that. appreciate any help with this.
program is multimodule.
/* MAIN FUNCTION */
#include "header.h"
int main () {
char String[101];
input_a_string(&String);
int lowerCount, upperCount;
int digitCount;
int blankCount;
counters(&String, &lowerCount, &upperCount, &digitCount, &blankCount);
FILE *outputFile;
outputFile = fopen("assign6.out", "w");
if (outputFile == NULL) {
fprintf(stderr, "Error, cannot open the file\n");
exit(-1);
}
else {
fprintf(outputFile, "The number of lower case letters in the string is %d\n"
"The number of upper case letters in the string is %d\n"
"The number of digits in the string is %d\n"
"The number of blanks in the string is %d\n", lowerCount, upperCount, digitCount, blankCount);
}
}
i think my problem lies in here. i dont even know if the way im reading the string and putting it into the array is correct. have no way of knowing. compiles fine, but when i run all i get is segmentation fault. is it correct?
/* input_a_string.c */
#include "header.h"
int input_a_string(char *str[]) {
FILE *inputFile;
inputFile = fopen("assign6.dat", "r");
if (inputFile == NULL) {
fprintf(stderr, "Error, file not found\n");
exit(-1);
}
else {
int lenght;
char ch;
for (lenght = 0; (ch = fgetc(inputFile)) != EOF && lenght < 101; lenght++)
*str[lenght] = ch;
}
fclose(inputFile);
}
/* counters.c */
#include "header.h"
int counters(char *str[], int *count1, int *count2, int *count3, int *count4) {
int i = 0;
while (*str[i] != '\0') {
if (*str[i] >= 'a' && *str[i] <= 'z')
++(*count1);
else if (*str[i] >= 'A' && *str[i] <= 'Z')
++(*count2);
else if (*str[i] >= '1' && *str[i] <= '9')
++(*count3);
else if (*str[i] == ' ');
++(*count4);
i++;
}
}
/* header.h */ #include <stdio.h> #include <stdlib.h> int input_a_string(); int counters();
This post has been edited by Caonabismo: 28 March 2010 - 06:36 PM

New Topic/Question
Reply




MultiQuote






|