rding str from file into chr array help

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 757 Views - Last Post: 31 March 2010 - 06:56 PM Rate Topic: -----

#1 Caonabismo  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 01-March 10

rding str from file into chr array help

Posted 28 March 2010 - 06:31 PM

ok so the task is to get input string from a file, count upper and lower case, digits and blanks all using call by reference effect, and then output the results to an output file.

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


Is This A Good Question/Topic? 1
  • +

Replies To: rding str from file into chr array help

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: rding str from file into chr array help

Posted 28 March 2010 - 06:54 PM

Where do you null-terminate the string in the file reading function? You're checking for it in the counters function, but you never set it in the reading function.
Was This Post Helpful? 0
  • +
  • -

#3 Caonabismo  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 01-March 10

Re: rding str from file into chr array help

Posted 28 March 2010 - 07:19 PM

i thought all char arrays end with a null by default.

i tried changing it and still doesnt work. instead of while(*str[i] != '\0'), i made the length variable in input_a_string global and did while (i < length) in counters. still get seg fault.

still i dont even know if this part that reads from file in input_a_string is right as i havent been able to test its behavior:
int lenght;
char ch;
for (lenght = 0; (ch = fgetc(inputFile)) != EOF && lenght < 101; lenght++)
*str[lenght] = ch;

This post has been edited by Caonabismo: 28 March 2010 - 07:20 PM

Was This Post Helpful? 0
  • +
  • -

#4 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: rding str from file into chr array help

Posted 28 March 2010 - 09:00 PM

If you're using a good compiler with an IDE like Visual Studio, click the "Debug" button, then "Start Debugging". Set some breakpoints and use the watch feature to see what the variables contain, it will make trackign down your bugs a lot easier..
Was This Post Helpful? 0
  • +
  • -

#5 Guest_c.user*


Reputation:

Re: rding str from file into chr array help

Posted 28 March 2010 - 11:35 PM

int input_a_string(char *str[]) {
it is not right

use any of these ones
int input_a_string(char str[]) {
int input_a_string(char *str) {

correct all left code accordingly
Was This Post Helpful? 1

#6 Caonabismo  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 01-March 10

Re: rding str from file into chr array help

Posted 29 March 2010 - 03:29 PM

View Postc.user, on 29 March 2010 - 12:35 AM, said:

int input_a_string(char *str[]) {
it is not right

use any of these ones
int input_a_string(char str[]) {
int input_a_string(char *str) {

correct all left code accordingly


thank you. that was the problem.
Was This Post Helpful? 0
  • +
  • -

#7 Caonabismo  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 01-March 10

Re: rding str from file into chr array help

Posted 29 March 2010 - 05:08 PM

i got another question.

when in a file is end of file? for example in my program the input file assign6.dat contains:

"My name is Brian 2." (without quotes)

and i get the following results in the output file assign6.out:

The number of lower case letters in the string is 11 (correct)
The number of upper case letters in the string is 2(correct)
The number of digits in the string is 1 (correct)
The number of blanks in the string is 20 (not correct?)

why is the number of blanks 20? shouldnt it be 4? i assume it is because the part
"ch = fgetc(inputFile)) != EOF" keeps storing the characters until it reaches end of file. when is the end of a file in a file, and how could i fix this.

thanks again.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_c.user*


Reputation:

Re: rding str from file into chr array help

Posted 29 March 2010 - 06:35 PM

    else {
        int lenght;
        char ch;
        for (lenght = 0; (ch = fgetc(inputFile)) != EOF && lenght+1 < 101; lenght++)
            str[lenght] = ch;
        str[length] = '\0'; /* length, did you try stardict ? */
    }


Was This Post Helpful? 1

#9 Caonabismo  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 01-March 10

Re: rding str from file into chr array help

Posted 30 March 2010 - 06:02 PM

View Postc.user, on 29 March 2010 - 07:35 PM, said:

    else {
        int lenght;
        char ch;
        for (lenght = 0; (ch = fgetc(inputFile)) != EOF && lenght+1 < 101; lenght++)
            str[lenght] = ch;
        str[length] = '\0'; /* length, did you try stardict ? */
    }



that doesnt really do anything. length will keep incrementing until EOF, so the null will be just as far as before.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_c.user*


Reputation:

Re: rding str from file into chr array help

Posted 30 March 2010 - 06:28 PM

if you get EOF the line is not terminated
if the line is not terminated then while (*str[i] != '\0') { the correct version of this can't find '\0' in the right place
so it will go out of bounds of str, continuing count characters (not only spaces)
Was This Post Helpful? 1

#11 Caonabismo  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 01-March 10

Re: rding str from file into chr array help

Posted 30 March 2010 - 06:44 PM

i mean i implemented it, and im still getting extra blanks. here hows i currently have it:
int length, ch;
for (length = 0; (ch = fgetc(inputFile)) != EOF && (length + 1) < 101; length++)
  str[length] = ch;
str[length] = '\0';


i changed assign6.dat a bit to see how it works to this:
Hi, my name is Brian 2.
What is yours?
Brian 2, duh.


here is what i got in the output file:
The number of lower case letters in the string is 30 /*correct*/
The number of upper case letters in the string is 4  /*correct*/
The number of digits in the string is 2 /*correct*/
The number of blanks in the string is 53 /*incorrect?*/


i really appreciate u trying to help me by the way :P

This post has been edited by Caonabismo: 30 March 2010 - 06:44 PM

Was This Post Helpful? 0
  • +
  • -

#12 Guest_c.user*


Reputation:

Re: rding str from file into chr array help

Posted 30 March 2010 - 07:13 PM

    int lowerCount, upperCount;
    int digitCount;
    int blankCount;

    lowerCount = upperCount =
    digitCount = blankCount = 0;


Was This Post Helpful? 1

#13 Guest_c.user*


Reputation:

Re: rding str from file into chr array help

Posted 30 March 2010 - 07:19 PM

if it continues, print updated code

This post has been edited by c.user: 30 March 2010 - 07:20 PM

Was This Post Helpful? 1

#14 Caonabismo  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 01-March 10

Re: rding str from file into chr array help

Posted 30 March 2010 - 07:33 PM

still continues :(. here is updated code as requested:
/* header.h */

#include <stdio.h>
#include <stdlib.h>

int input_a_string();
int counters();


/* MAIN FUNCTION */

#include "header.h"

int main ()  {

  char String[101];

  input_a_string(&String);

  int lowerCount, upperCount = 0;
  int digitCount = 0;
  int blankCount = 0;

  counters(&String, &lowerCount, &upperCount, &digitCount, &blankCount);

  FILE *outputFile;

  outputFile = fopen("assign6.out", "w");

  if (outputFile == NULL)  {
    fprintf(stderr, "Error, cannot write to the file assign6.out\n");
    exit(EXIT_FAILURE);
  }
  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);
  }
  fclose(outputFile);
}


/* input_a_string.c */

#include "header.h"

int input_a_string(char *str)  {

  FILE *inputFile;

  inputFile = fopen("test.dat", "r");

  if (inputFile == NULL)  {
    fprintf(stderr, "Error, file not found\n");
    exit(EXIT_FAILURE);
  }
  else  {
    int length, ch;
    for (length = 0; (ch = fgetc(inputFile)) != EOF && (length + 1) < 101; length++)
      str[length] = ch;
    str[length] = '\0';

  }
  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] >= '0' && str[i] <= '9')
      (*count3)++;
    else if (str[i] == ' ');
      (*count4)++;
    i++;
  }
}


Was This Post Helpful? 0
  • +
  • -

#15 Caonabismo  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 01-March 10

Re: rding str from file into chr array help

Posted 30 March 2010 - 08:19 PM

perhaps reading the file in another way will solve. i tried doing it line by line with fgets instead of char by char, but i couldnt get it to work correctly after much trying. like storing several lines in the same array, so that all of the file can be analyzed. ive looked around, dont see what else is there.

This post has been edited by Caonabismo: 30 March 2010 - 08:20 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2