Click here to Skip to main content
15,887,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am changing the signature of a function having default argument char* to vector<string> in C++. The role of function with default argument char* is to take out the image name of an image file, then read that image file and send to the server. This function is only capable of sending one image file. So, I went for vector<string> to send multiple images at once. But, somewhere I am missing on something which I am not able to find out. Please help.

What I have tried:

Function with default parameter of char*:
void fileSend(char *fpath)
{
    // Extract only filename from given path.
    char filename[50];
    int i = strlen(fpath);
    for (; i > 0; i--)
    {
        if (fpath[i - 1] == '\\')
            break;
    }
    for (int j = 0; i <= (int)strlen(fpath); i++)
    {
        filename[j++] = fpath[i];
    }

    ifstream myFile(fpath, ios::in | ios::binary | ios::ate);
    int size = (int)myFile.tellg();
    myFile.close();

    char filesize[10]; itoa(size, filesize, 10);
    send(sock, filename, strlen(filename), 0);
    char rec[32] = ""; 
    recv(sock, rec, 32, 0);

    send(sock, filesize, strlen(filesize), 0);
    recv(sock, rec, 32, 0);

    FILE *fr = fopen(fpath, "rb");

    while (size > 0)
    {
        char buffer[1030];

        if (size >= 1024)
        {
            fread(buffer, 1024, 1, fr);
            send(sock, buffer, 1024, 0);
            recv(sock, rec, 32, 0);
        }
        else
        {
            fread(buffer, size, 1, fr);
            buffer[size] = '\0';
            send(sock, buffer, size, 0);
            recv(sock, rec, 32, 0);
        }
        size -= 1024;
    }
    fclose(fr);
}

Function with default parameter of vector<string>:
void fileSend(vector<string> fnames)
    {
        char *filename;
        for (int k = 0; k < fnames.size(); k++)
        {
            int i = fnames[k].length;

            for (; i > 0; i--)
            {
                if (fnames[k][i - 1] == '\\')
                    break;
            }
            for (int j = 0; i <= fnames[k].length(); i++)
            {
                filename[j++] = fnames[k][i];
            }

            ifstream myFile(fnames[k], ios::in | ios::binary | ios::ate);
            int size = (int)myFile.tellg();
            myFile.close();

            char filesize[10]; itoa(size, filesize, 10);
            send(sock, filename, strlen(filename), 0);
            char rec[32] = "";
            recv(sock, rec, 32, 0);

            send(sock, filesize, strlen(filesize), 0);
            recv(sock, rec, 32, 0);

            FILE *fr = fopen(fnames[k], "rb");

            while (size > 0)
            {
                char buffer[1030];

                if (size >= 1024)
                {
                    fread(buffer, 1024, 1, fr);
                    send(sock, buffer, 1024, 0);
                    recv(sock, rec, 32, 0);
                }
                else
                {
                    fread(buffer, size, 1, fr);
                    buffer[size] = '\0';
                    send(sock, buffer, size, 0);
                    recv(sock, rec, 32, 0);
                }
                size -= 1024;
            }
            fclose(fr);
        }
    }
Posted
Updated 10-Sep-17 9:28am
Comments
Richard MacCutchan 10-Sep-17 14:48pm    
I cannot understand what your code is supposed to be doing. Each string in the vector should contain a file name, so all you need is a loop to extract each file name and pass that to the function that sends it to the server.

1 solution

The straightforward way to do that is overloading the old function (and call it):

C++
void fileSend(vector<string> fnames)
{
   for (int k = 0; k < fnames.size(); k++)
   {
      fileSend( fnames[k].c_str()); // call the original function
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900