Untitled

mail@pastecode.io avatar
unknown
c_cpp
24 days ago
1.3 kB
1
Indexable
Never
#include <iostream>
#include <cstring>

using namespace std;

void getFileName(char* fileName, size_t maxLenght)
{
    cout << "Wprowadz nazwe pliku (bez rozszerzenia): ";
    cin.getline(fileName, maxLenght);
}

void getExtension(char* extension, size_t maxLenght)
{
    cout << "Wprowadz rozszerzenie (bez kropki): ";
    cin.getline(extension, maxLenght);

    size_t len = strlen(extension);
    if (len > 0 && extension[0] != '.')
    {
        if (len + 1 < maxLenght)
        {
            memmove(extension + 1, extension, len + 1);
            extension[0] = '.';
        }
        else
        {
            cerr << "Rozszerzenie jest za dlugie!" << endl;
            exit(-1);
        }
    }
}

void addExtension(char* fileName, const char* extension, size_t maxLenght)
{
    if (strlen(fileName) + strlen(extension) >= maxLenght)
    {
        cerr << "Nazwa pliku jest za dluga!" << endl;
        exit(-1);
    }

    strcat_s(fileName, maxLenght, extension);
}

int main()
{
    const int MAX_LENGTH = 100;
    char fileName[MAX_LENGTH];
    char extension[MAX_LENGTH];

    getFileName(fileName, MAX_LENGTH);
    getExtension(extension, MAX_LENGTH);

    addExtension(fileName, extension, MAX_LENGTH);

    cout << "Nowa nazwa pliku: " << fileName << endl;

    return 0;
}
Leave a Comment