1
votes

I want to have the function fopen's filename parameter as a dynamic variable that takes in a ftp client input for my ftp server. I have tried numerous different ways both on this forum and on google but "filename" will still not be recognized by fopen.

else if (strncmp(client->input, "retr", 4) == 0) {

            char fname[1024];  
            // COMMAND LINE: retr filePATHNAME thats why +5       
            strcpy(fname, client->input+5); 

        if(fopen(fname, "r") != NULL) {
        ...

If I put fopen("/pub/test.txt" , "r"), it works so it has to do with something with spaces or quotations or type.

But if i try to do it on the client command line with retr /pub/test.txt or even retr "/pub/test.txt" fopen does not work and will not open the file.

Been stuck on this for the longest time, any help is appreciated.

Thanks

1
Please try to provide a minimal reproducible example.David Grayson
I suggest you log the file name your server tries to open. It's probably not what you think. Be sure to use a log format that reveals trailing whitespace / newlines, as the presence of such additional characters is one of the more likely problems.John Bollinger
@JohnBollinger Im new to C and all this stuff so Im not quite sure how to log it. I've tried printing what fname gives, and it is just printing exactly what I am inputting.pkmangg
Also if you are on a platform that has valgrind you should check for memory problems like unintialized bytes or invalid reads or writes.Iharob Al Asimi
@pkmangg, at its simplest, logging the filename could just mean printing it, in a suitable format, to stdout.John Bollinger

1 Answers

1
votes

I found the answer.

Thanks to @user3386109 for the hint.

I basically had to clear all the spaces, new lines for the input

strtok(client->input+5,"\r\n\t");