Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7970465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:24:13+00:00 2026-06-04T07:24:13+00:00

im trying to copy some texts from a file and save them in struct

  • 0

im trying to copy some texts from a file and save them in struct members, i run my program on cmd.exe and it crashed, but when i run it on codeblocks or visual studio it works,

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

struct AMIGOS 
{ 
    char nom[' '];
    char apellido[' '];
    char nompila[' '];
    char tel[' '];
    char correo[' '];
    char dir[' '];
    char fecha[' ']; 
};

int main()
{
    struct AMIGOS reg;
    char registro[128];

    char**datos;
    char*dato;
    datos = (char**)malloc(10*sizeof(char**));
    int tam;
    int i=0;

    FILE* pt = fopen("arch.txt","r");
    if(pt==NULL)
    {
        printf("filenotfound\n");
    }
    else
    {
        while(fgets(registro,128,pt))
        {
            dato = strtok(registro,"|");
            while(dato)
            {
                tam = strlen(dato);
                datos[i] = (char *)malloc(tam);
                memcpy(datos[i],dato,tam);
                datos[i][tam]=0;
                i++;
                datos[i]=0;
                dato = strtok(0,"|");
            }    
        }
        strcpy(reg.nom,datos[0]);
        strcpy(reg.apellido,datos[1]);
        strcpy(reg.nompila,datos[2]);
        strcpy(reg.fecha,datos[3]);
        strcpy(reg.tel,datos[4]);
        strcpy(reg.correo,datos[5]);
        strcpy(reg.dir,datos[6]);

        printf("%s\n",reg.nom);
        printf("%s\n",reg.apellido);
        printf("%s\n",reg.nompila);
        printf("%s\n",reg.fecha);
        printf("%s\n",reg.tel);
        printf("%s\n",reg.correo);
        printf("%s\n",reg.dir);
    }    
}

the text on the file:

kevin|clark|ns|march 15 2001|5555555|l@mail.com|123 street

does anybody know why it crash when i try to run it on cmd.exe ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:24:14+00:00Added an answer on June 4, 2026 at 7:24 am

    This is writing beyond the end of the array:

    tam = strlen(dato);
    datos[i] = (char *)malloc(tam);
    memcpy(datos[i],dato,tam);
    datos[i][tam]=0;                 <---- 'tam -1' is the last element
    

    You need to add an additional character to store the null terminator, or you could just use strdup():

    datos[i] = strdup(dato);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to copy some files from one network share to another using File::Copy.
I'm trying copy a single file from the Plugin directory inside of my Wordpress
I am trying to copy a file from my remote server to my local.
I am trying to run a program from the command prompt in windows. I
Trying to copy some user input from one HTML input to another and my
I got some troubles trying to copy char to char... Here's args[] definition :
I am trying to copy data from my MYSQL table to SQL Server using
Im trying to make some text non-copyable, my aim isn't to stop people from
I'm trying to run an external program in SBCL and capture its output. The
I'm trying to copy some dynamic fields for spellchecking. However, the . Below are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.