-1

i have written code for storing data in a text file and i have separated my data into 2 strings . and i did concatenate 2 strings using strcat . but while running its showing segmentation fault(core dumped)..

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

void main()
{
    FILE *fptr;
    char *data="867330029590851,144311,140817,130294,0801536,15,00,-1380021593,,N,,,,,180717034311,,,,,4.18,,,,,,,,NA";
    char *timeStamp="14-08-17,14:45:38";

    char *currentTimeStamp=strcat(data,timeStamp);


    /*  open for writing */
    fptr = fopen("RedisData.txt", "w");

    if (fptr == NULL)
    {
        printf("File does not exists \n");
        return;
    }
    // printf("string \n");
    // scanf("%s", data);

    printf("%s\n",currentTimeStamp);

    fprintf(fptr,"currentTimeStamp= %s\n",currentTimeStamp);


    fprintf(fptr, "Data    = %s\n", data);
    fprintf(fptr, "TimeStamp     = %s\n", timeStamp);
    fclose(fptr);
}
Deepak Pookkote
  • 957
  • 3
  • 14
  • 32
  • `strcat(data,timeStamp)` -> UB detected. – babon Aug 16 '17 at 07:13
  • 1
    In C all string literals are *read-only* arrays with only enough size to fit the string and the string terminator. So you are both attempting to write to read-only data, and write out of bounds. – Some programmer dude Aug 16 '17 at 07:13
  • 1
    Find a tutorial on C strings. They are just null terminated char arrays, and like any other array you must never read or write past the end of array... – Serge Ballesta Aug 16 '17 at 07:16
  • Minor: `fclose(fptr);` might not get executed if `fptr` is `NULL`. – babon Aug 16 '17 at 07:18
  • @ Sourav Ghosh : how come its duplicate mahn?? just check my code ..? wt else we can do to figure out our errors other than this..this is too much – Deepak Pookkote Aug 16 '17 at 07:25

1 Answers1

1

data is a string literal and any attempt to modify it will invoke undefined behavior.

On the other hand, while using strcat there must be enough space in the destination array to hold the source string.

char *data="867330029590851,144311,140817,130294,0801536,15,00,-1380021593,,N,,,,,180717034311,,,,,4.18,,,,,,,,NA";
char *timeStamp="14-08-17,14:45:38";
char *currentTimeStamp = malloc(strlen(data) + strlen(timeStamp) + 1);
strcat(currentTimeStamp, data);
strcat(currentTimeStamp, timeStamp);
haccks
  • 104,019
  • 25
  • 176
  • 264
  • 2
    this one is working for me. char data[500]="867330029590851,144311,140817,130294,0801536,15,00,-1380021593,,N,,,,,180717034311,,,,,4.18,,,,,,,,NA"; char timeStamp[500]="14-08-17,14-45-38,"; char currentTimeStamp[500]; strcat(timeStamp,data); printf("%s\n",timeStamp); – Deepak Pookkote Aug 16 '17 at 07:35
  • @DeepakVijay; Yes, that will work because `timeStamp` is now an array of chars and it has enough space to hold `data` string. – haccks Aug 16 '17 at 07:48
  • 1
    @DeepakVijay : yes it will work and you don't have to store it in an another variable you can simple use like this.. strcat(timeStamp,data); printf("%s\n",timeStamp); //this will work for you ... –  Aug 17 '17 at 05:02