Writing in a C++ matrix

Asked

Viewed 528 times

-1

I am studying and implementing some C++ schemes but it is causing errors when assigning value to the matrix. Well... I have a txt file that contains lines with "[email protected]" content. My Ret variable is returning the values correctly, but at the time of inserting in the vector the following error is popped:

Exception generated in 0x0F6DE559 (ucrtbased.dll) in Consoleapplication3.exe: 0xC0000005: breach of access when writing in local 0xCCCCCC.

Debugging the code, all values are apparently correct, but the exception is thrown anyway. I previously tried to do the same process without strcpy, inserting directly into the desired index and using Strtok to separate the string, but the value of all index was changed to the last record always. Could someone give me a light to understand where I’m going wrong?

FILE *fp = NULL;

fp = fopen("Emails.txt", "rt");

char* matriz[99][2];

int index = 0;
char str[256];
char nome[20];
char email[60];

while (1)
{
    char *ret = fgets((char*)str, 255, fp);

    if (ret == NULL)
        break;

    sscanf(str, "%s %s", &nome, &email);

    strcpy(matriz[index][0], nome); // Acusa erro aqui!
    strcpy(matriz[index][1], email);

    index++;
};
  • But if it’s C++ why did you make a code in C? https://answall.com/q/392951/101

  • You are not allocating space for the string. You may have up to 99 index, it seems to me, but you do not allocate in matrix. Anyway, you tag C++, so you should use containers and Std:string:.

  • Thanks for the answers! So, I’m actually studying a C++ program ready, I ended up following his standards that seem to be in C... Anyway, I was kind of lost anyway! All the research I did on the net regarding C++ returned these methods and this way of doing the code... hehehe

2 answers

0

I was wondering if you are using C or C++, why the question speaks C++ and the code is in C.

I did here an example of how to read a file using C++.

Suppose your Emails.txt file is as follows:

nome1 [email protected]
nome2 [email protected]
nome3 [email protected]
nome4 [email protected]
nome5 [email protected]

main.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <map>

int main() {
  std::string filename = "Emails.txt";

  // Abre o arquivo.
  std::ifstream infile(filename);

  std::map<std::string, std::string> lines;

  if(infile.is_open()){
    std::string name, email;

    // Ler o arquivo linha por linha.
    while(infile >> name >> email) {
      lines[name] = email;
    }
  }else{
    std::cerr << "Não foi possível abrir o seguinte arquivo: " << filename << std::endl;
  }

  // Fecha o arquivo.
  infile.close();

  // Exibe a lista.
  for(auto const& line : lines) {
    std::cout << line.first << " " << line.second << std::endl;
  }

  return 0;
}
  • Thank you for the reply Vinicius! So, I’m actually studying a C++ program ready, I ended up following his standards that seem to be in C... Anyway, I was kind of lost anyway! I will study your code thoroughly to understand and implement it! Thank you!

0


There is no need to use a bi-dimensional matrix for this case. It is much easier to understand (and more natural) using a vector of structures. Much less necessary is the use of a two-dimensional array of pointers!! This knots anyone in the head.

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

typedef struct Pessoa
{
  char nome[20];
  char email[60];
} Pessoa;

int main(void)
{
  char str[256];
  Pessoa pessoas[100];
  int i, nPessoas = 0;

  FILE* fp = fopen("emails.txt", "r");
  if (fp == NULL)
  {
    printf("* erro na abertura do arquivo\n");
    exit(1);
  }

  printf("*\n");

  while (nPessoas < 100)
  {
    if (fgets((char*)str, 255, fp) == NULL)
    {
      printf("* fim de arquivo\n");
      break;
    }

    if (sscanf(str, "%19s%59s", pessoas[nPessoas].nome, pessoas[nPessoas].email) != 2)
    {
      printf("* erro, linha mal formatada\n");
      printf("*\n");
      continue;
    }

    nPessoas++;
  }

  printf("*\n");
  printf("* numero de pessoas: %d\n", nPessoas);
  printf("*\n");

  for (i = 0; i < nPessoas; i++)
  {
    printf("* pessoa %d\n", i);
    printf("*   nome:[%s]\n", pessoas[i].nome);
    printf("*   email:[%s]\n", pessoas[i].email);
  }

  printf("*\n");
}
  • Thank you so much for the answer! I will study your code right to implement it! I thank you for the help!!

Browser other questions tagged

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