-1

I am trying to create a program which does:

  1. Find the length of the largest word, the lines & the number of letters
  2. Store the data from a file to string (str)
  3. Store the data from string (str) to a dynamic allocated array

text:link

My program does not store the words in the array

I used a debugger link and I notice that the problem was in str variable (error reading variable str (value requires 146297 bytes more than max value - size) What does that mean and how could I fix it?

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

int main()
{
    FILE *fp;
    fp = fopen("C:\\Downloads\\AlicesAdventuresInWonderland.txt", "r+");
    if (fp == NULL)
    {
        printf("cannot open the file\n");
        exit(8);
    }
    char ch;
    int len = 0, counter = 0, thelongest = 0, letters = 0, i = 0, j = 0, k = 0;
    for (;;)
    {
        ch = fgetc(fp);
        letters++;
        if (ch == '\n' || ch == EOF)
        {
            if (thelongest < len)
            {
                thelongest = len;
            }
            if (len > 0)
            {
                counter++;
            }
            len = 0;
            if (ch == EOF)
            {
                break;
            }
        }
        else
        {
            len++;
        }
    }

    printf("the number of lines is : %d", counter);
    printf("\nthe length of the longest word is : %d", thelongest);
    printf("\nthe number of letters is %d", letters);

    char str[letters + 1];

    rewind(fp);

    char **arr;

    arr = (char **)malloc(sizeof(char*) * (letters + 1));

    for (ch = fgetc(fp); ch != EOF; ch = fgetc(fp))
    {
        if (!ispunct(ch))
        {
            str[i++] = ch;
        }
    }
    str[i]= '\0';
    rewind(fp);
    i = 0, k = 0, j = 0;

    char tempstr[thelongest + 1];

    for (;;)
    {
        if (str[k] == '\n' || isspace(str[k]) || str[k] == '\0')    
        {
            if (k != 0)
            {
                tempstr[k] = '\0';
                arr[j++] = strdup(tempstr);
                k = 0;
            }
            if (tempstr[k] == '\0')
            {
                break;
            }
        }
        else
        {
            tempstr[k++] = str[i++];
        }
    }

    arr[j] = NULL;

    for (i = 0; i < counter + 1; i++)
    {
        printf("\n the words are %s", arr[i]);
    }
    return 0;
}
chqrlie
  • 114,102
  • 10
  • 108
  • 170
up10388
  • 1
  • 1
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Jun 02 '22 at 19:16
  • 1
    This program does not even compile ;) Where is `str` coming from? – majusebetter Jun 02 '22 at 19:17
  • @majusebetter in the block is the part of the code which creates the problem in the link is the code from scratch – up10388 Jun 02 '22 at 19:19
  • 1
    Questions seeking debugging help should generally provide a [mre] of the problem, which includes the exact input required to reproduce the problem. I doubt that it is necessary to provide more than 1600 lines of input in order to reproduce the problem. – Andreas Wenzel Jun 02 '22 at 19:21
  • @AndreasWenzel Excuse me , I edit my question I used a debugger and the problem was in str variable (error reading variable str (value requires 146297 bytes more than max value - size) – up10388 Jun 03 '22 at 08:39
  • @majusebetter I added all the code now – up10388 Jun 03 '22 at 08:50
  • I was able to reproduce the error message that you are quoting using the GNU debugger. You can ignore that error message. You are getting an error message for `str` because it cannot display the content of `str`, because it does not exist yet. That error message will disappear after the line `char str[letters + 1];`, in which `str` is created. The debugger seems to only provides this misleading error message with [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array) that have not been created yet. – Andreas Wenzel Jun 03 '22 at 09:59
  • @AndreasWenzel The problem (which does not print the words ) can be created by the tempstr [link](https://imgur.com/a/XeLNj0U) – up10388 Jun 03 '22 at 15:03
  • @up10388: I did not understand your previous comment. Please elaborate. – Andreas Wenzel Jun 03 '22 at 15:09
  • @AndreasWenzel I am trying to find why my program does not print the words . I used the debugger of codeblocks and the only error I receive was about `str` and for `tempstr` is a message `Γ\220\220\220\220\220\220\220\220` – up10388 Jun 03 '22 at 15:14
  • @up10388: That value probably simply means that you have not written anything meaningful into the array yet. It is normal for arrays to contain values that are not meaningful, until you write something into the array. The question you should always ask yourself is: Does the value of the variable deviate from my expectation at this point in the program? If you have not written any value to a variable, then you should not expect it to have a meaningful value. – Andreas Wenzel Jun 03 '22 at 15:28

0 Answers0