mardi 6 juin 2017

String length changes suddenly

Here in this code, the character length is changing suddenly. Before introducing char file the strlen(str) was correct. As I introduced the new char file the strlen value of variable str changes.

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

using namespace std;

int main(){

    char buf[BUFSIZ];

    if(!getcwd(buf,BUFSIZ)){
            perror("ERROR!");
    }

    cout << buf << endl;

    char *str;
    str = new char[strlen(buf)];
    strcpy(str,buf);
    strcat(str,"/");
    strcat(str,"input/abcdefghijklmnop");
    cout << str << endl;
    cout << strlen(str) << endl;

    char *file;
    file = new char[strlen(str)];
    cout << strlen(file) << endl;
    strcpy(file,str);
    cout << file << endl;
}

Aucun commentaire:

Enregistrer un commentaire