Home > Mobile >  Dynamic array strings in c
Dynamic array strings in c

Time:01-09

I have a task where I should read a text file line by line, then I should reverse the line (the line contains one string), also at the same time I have to reverse the order of the lines, so the last one should be the first the one before last should be the second and so on and I have to write all these on the standard output.

First I should read an enum from argv[] f which could be "linenums" or "nolinenums", if it is "nolinenums" I have nothing to do, but with "linenums" I have to number the lines backwards. Then an int and the other arguments should be the name of the files.

an example: ./main linenums 5 example.txt
input:

  • apple
  • car
  • tower

output:

  • 3 rewot
  • 2 rac
  • 1 elppa

The task says I have to use dynamic array to store the lines, I can't count the lines of the file before reading the lines, if the amount of the lines is bigger then the size of the dynamic array I should double its size.

well I did something, but it's kind of messy, I know this site is not for making the my task completely, but I would much appreciate it, but any help would be nice. (I have very limited time also)

This is what I've done so far:

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

typedef enum Type{
linenums,
nolinenums
}Type;

struct str{
Type type;
int length;
};

#define BUFFERSIZE 50

int main(int argc, char *argv[])
{
struct str Data;

if(argc < 3)
{
    printf("\trev [SHOW LINE NUMBERS] [MAX LINE LENGTH] files...\n");
    exit(1);
}

if(strcmp(argv[1], "linenums") == 0)
{
    Data.type = linenums;
}
else if(strcmp(argv[1], "nolinenums") == 0)
{
    Data.type = nolinenums;
}
else
{
    printf("Wrong format!");
    exit(1);
}

Data.length = atoi(argv[2]);

for(int i=3; i<argc; i  )
{
    FILE *fpin = fopen(argv[i], "r");
    char buffer[BUFFERSIZE];
    
    int lines = 0;

    if(fpin == NULL)
    {
        fprintf(stderr, "File opening unsuccessful: %s", argv[i]);
        exit(1);
    }
    else
    {   
        char **words = malloc(sizeof(char) * 8);
        for(int i = 0; i < 8; i  )
        {
            words[i] = malloc(sizeof(char*) * 1025);
            if(words[i] == NULL)
            {
                printf("Memory allocation unsuccesful");
                exit(1);
            }
        }

        while(fgets(buffer, BUFFERSIZE, fpin) != NULL)
        {
            buffer[strcspn(buffer, "\n")] = '\0';
            strrev(buffer);
            strcpy(words[lines], buffer);
            lines  = 1;
        }
        if(Data.type == 0)
        {
            while(lines>0)
            {
                printf("%d %s\n", lines, words[lines-1]);
                lines -= 1;
            }
        }
        else
        {
            while(lines>0)
            {
                printf("%s\n", words[lines-1]);
                lines -= 1;
            }
        }
        
        printf("\n");
    }
    fclose(fpin);
}

return 0;
}

My main problem is the dynamic array and iterating through the argv[] array

thanks to pm100 my output is right with the example, but I still have the problem when I have to work with more than one file and I still have to do the realloc when the number of lines are more than the already allocated ones (I should double it).

CodePudding user response:

fundamental problem here

 words[lines] = buffer;

this does not copy the string, instead it replaces the pointer in the words array (that points to memory you just allocated above)

you need

  strcpy(words[lines], buffer);
  •  Tags:  
  • c
  • Related