Exit error while trying to print contents of.txt file

Asked

Viewed 90 times

1

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

typedef struct{

    char **matriz;

} Dado;


void ler_agumento (Dado *dado, int argc, char **argv){

    int opcao;
    dado -> matriz = NULL;

    while (opcao = getopt (argc,argv, "e::") != -1){

        switch (opcao){

            case 'e':
                dado->matriz = optarg;
                break;
        }
    }
}

void imprimir (FILE *file){

    char *line = (char*) malloc(sizeof(char));

    if(file == NULL) {

        fprintf(stderr, "Erro ao abrir o arquivo.txt.");
    }   

    while(fgets(line, 80, file) != NULL) {

        printf("%s", line);
    }     
}

int main(int argc, char **argv[]){

    int i;
    Dado dado;

    dado.matriz = malloc(sizeof(char *)*8);

        for(i=0; i<8; i++){

            dado.matriz[i] = malloc(8);
        }

    ler_argumento (&dado, argc, argv);

    FILE *file = fopen(dado.matriz,"r");

    imprimir (file);

    fclose (file);

    return 0;
}

I want to print the content that is inside a file. I passed as argument through the terminal opening like this (./executar -e arquivo.txt). The output it generates is the error message from printf() showing that the file is empty and then shows segmenttion fault.

  • Here: malloc(sizeof(char)) you are allocating space for a single character. Maybe it should be: malloc(80*sizeof(char)). The array to contain the string".txt file" should be at least 12 and not 8.

  • Good evening! I made this modification but the exit continued the same.

  • You do dado->matriz = optarg; but char** matriz and char* optarg are uncompromising. argv should be char** argv or char* argv[]

  • Thanks for the tips. I made the changes leaving char *matrix and char **argv. The output continues in the same way

2 answers

2

Do you really need this abstraction for the dice? The main reason for the problem in the code that causes the described error is the lack of memory allocation to store the data. There is also an error in the function signature main(). I improved a few more things, but I seem to have a logic error there (I won’t say because I don’t know the requirement), but I didn’t improve everything I could, for example I didn’t check if the allocation or reading in the file gave error:

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

typedef struct {
    char **matriz;
} Dado;

void ler_agumento (Dado *dado, int argc, char **argv) {
    dado->matriz = NULL;
    while (int opcao = getopt (argc,argv, "e::") != -1) if (opcao == 'e') dado->matriz = optarg;
}

void imprimir (FILE *file) {
    if (file == NULL) fprintf(stderr, "Erro ao abrir o arquivo.txt.");
    char *line = malloc(80);
    while(fgets(line, 80, file) != NULL) printf("%s", line);
}

int main(int argc, char *argv[]) {
    Dado dado;
    dado.matriz = malloc(sizeof(char *) * 8);
    for (int i = 0; i < 8; i++) dado.matriz[i] = malloc(8);
    ler_argumento (&dado, argc, argv);
    FILE *file = fopen(dado.matriz,"r");
    imprimir(file);
    fclose(file);
}

I put in the Github for future reference.

0

Guys, I managed to settle here. I thank everyone who answered me.

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

typedef struct dado{
    // Deixei um asterisco apenas.
    char *matriz;

} Dado;

// Arrumei o parametro argv colocando colchetes.
void ler_agumento (Dado *dado, int argc, char *argv[]){

    int opcao;

   // Fiz alocao dinamica aqui.
   dado -> matriz = (char*) malloc (sizeof(char)); 
   dado -> matriz = NULL;

    while (opcao = getopt (argc,argv, "e:") != -1){

        switch (opcao){

            case 'e':
                dado->matriz = optarg;
                break;
        }
    }
}

void imprimir (FILE *file){

    char *line = (char*) malloc(sizeof(char));

    if(file == NULL) {

        fprintf(stderr, "Erro ao abrir o arquivo.txt.");
    }   

    while(fgets(line, 80, file) != NULL) {

        printf("%s", line);
    }     
}

int main(int argc, char **argv[]){

    Dado dado;

    ler_argumento (&dado, argc, argv);

    FILE *file = fopen(dado.matriz,"r");

    imprimir (file);

    fclose (file);

    return 0;
}
  • But you still have the same problem in the function signature main

Browser other questions tagged

You are not signed in. Login or sign up in order to post.