How to use the "fread" function in c to read a txt file string and store that string in a vector?

2
Hello, I'm trying to read a block of bytes from a .txt file that contains a string. For example, I want to store this string in a vector of char.

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


    char nome_arquivo[25];
    int i, j;
    char vetor[50]; //vetor que receberá a mensagem do arquivo nessa função
    //vetor = malloc(50*sizeof(char));
    int tamanhovetor;
    char cpy[2];
    char str[5][9];

    printf("Informe o nome do arquivo que deseja abrir:\n");
    scanf("%s", nome_arquivo);

    arquivo_txt = fopen(nome_arquivo, "r");

    if(arquivo_txt == NULL){
        fprintf(stderr, "Erro na abertura do arquivo\n");
        fclose(arquivo_txt);
    }

    fread(vetor, sizeof(char), 5, arquivo_txt);

    tamanhovetor = strlen(vetor);

    printf("%s\n%d", vetor, tamanhovetor);  

    /*
    for(i=0; i<tamanhovetor; i++){
        str[i][0]= '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


    char nome_arquivo[25];
    int i, j;
    char vetor[50]; //vetor que receberá a mensagem do arquivo nessa função
    //vetor = malloc(50*sizeof(char));
    int tamanhovetor;
    char cpy[2];
    char str[5][9];

    printf("Informe o nome do arquivo que deseja abrir:\n");
    scanf("%s", nome_arquivo);

    arquivo_txt = fopen(nome_arquivo, "r");

    if(arquivo_txt == NULL){
        fprintf(stderr, "Erro na abertura do arquivo\n");
        fclose(arquivo_txt);
    }

    fread(vetor, sizeof(char), 5, arquivo_txt);

    tamanhovetor = strlen(vetor);

    printf("%s\n%d", vetor, tamanhovetor);  

    /*
    for(i=0; i<tamanhovetor; i++){
        str[i][0]= '%pre%';
        for(j=0; j<9; j++){
            sprintf(cpy,"%d", vetor[i]%2);
            strcat(str[i], cpy);
            vetor[i]/=2;
        }
            printf("%s ", str[i]);
    }*/
}
'; for(j=0; j<9; j++){ sprintf(cpy,"%d", vetor[i]%2); strcat(str[i], cpy); vetor[i]/=2; } printf("%s ", str[i]); }*/ }
    
asked by anonymous 11.06.2017 / 20:09

2 answers

1

You are copying 5 bytes of your file to a buffer ( vetor ) and then calling strlen() in it, which is a function that counts in which position of the vector it receives is first byte ''vetor'' .

But you did not put vetor after the end of the bytes you copied, and since fread() is a local variable, it is allocated in the heap, which does not have its values zeroed out.

So, unless you ensure that one of the 5 (why 5?) bytes is null, you're most likely starting to read the bytes that you loaded and proceeded by reading the garbage in the rest of %code% and then going on reading random values on the stack until it reaches the end of it and takes an access violation ...

So, you have to put the following line after the call to %code% :

vetor[5] = '
vetor[5] = '%pre%';
';

So the string is delimited and the manipulation functions make sense.

    
12.06.2017 / 15:11
-1

This code can help

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

int main()
{
   FILE *fp;
   char c[] = "this is tutorialspoint";
   char buffer[100];

   /* Open file for both reading and writing */
   fp = fopen("file.txt", "w+");

   /* Write data to the file */
   fwrite(c, strlen(c) + 1, 1, fp);

   /* Seek to the beginning of the file */
   fseek(fp, SEEK_SET, 0);

   /* Read and display data */
   fread(buffer, strlen(c)+1, 1, fp);
   printf("%s\n", buffer);
   fclose(fp);

   return(0);
}
    
11.06.2017 / 20:28