如何使用文件名将文件从一个文件夹移动到另一个文件夹? [英] How to move a file from one folder to another with its filename?

查看:114
本文介绍了如何使用文件名将文件从一个文件夹移动到另一个文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想使用tcp / ip从客户端向服务器发送文件及其文件名。如何实现上述任务。我已经开发了代码,我使用了字符串标记。我提供的代码供参考。

Hi,

I want to send a file from client to server using tcp/ip, along with its file name. How to achieve the above task. I have developed the code and i have used string token. I am giving the code for reference.

#include<stdio.h>
 #include<unistd.h>
 #include<string.h>    
 #include<stdlib.h>
 #include<sys/ioctl.h>
 #include<sys/types.h>
 #include<arpa/inet.h>
 #include<sys/socket.h>
int receive_text(long long int socket)
{ 
	long int buffersize = 0, recv_size = 0, size = 0, read_size, write_size; 
	char verify = '1',c; 
	int errno;
	FILE *text;
	char *pch;
	char *str="/home/sosdt009/Documents";
	char *fname[2];
	char *filename[10];
	char *filebody[1024];
  	int i=0;
 
	//Find the size of the text
	recv(socket, (char *)&size, sizeof(int), 0);
 	printf("Size value is:%ld\n",size);
	//Send our verification signal
	send(socket, &verify, sizeof(char), 0);
 	printf("Size value is:%ld\n",size);
	//Make sure that the size is bigger than 0
	if (size <= 0)
	{
		printf("Error has occurred. Size less than or equal to 0 \n");
		return -1;
	} 
	
	/*if (text == NULL)
	{
		printf("Error has occurred, file could not be opened \n");
		return -1;
	}*/
			 
	//Loop while we have not received the entire file yet

	while (recv_size < size)
	{
		ioctl(socket, FIONREAD, &buffersize);
 
		//We check to see if there is data to be read from the socket 
		if (buffersize > 0)
		{
			char *pBuf = malloc(buffersize);
			printf("Buffer value is:%s\n",pBuf);
			if (!pBuf)
			{
				fprintf(stderr, "Memory Error. Cannot allocate!\n");
				exit(-1);
			} 
			read_size = recv(socket, pBuf, buffersize, 0);
			printf("read size is:%ld\n",read_size);
			if (read_size  < 0)
			{
				printf("%s", strerror(errno));
			}
			//printf ("Splitting string \"%s\" into tokens:\n");
  			pch = strtok (pBuf,"@");
			printf(" value is:%s\n",pch);
  			while (pch != NULL)
  			{
				filename[i]=pch;
				strcpy(str,filename[i]);
				printf("the string copy is: %s\n",str);
				text = fopen(str, "w"); 
    				/*printf ("filename=%s\n",filename[i]);
 				pch = strtok (NULL, "@");*/
				i++;
				filebody[i]=pch;
    				printf ("filebody=%s\n",filebody[i]);
 				pch = strtok (NULL, "@");
				while ((filebody[i] = strtok(NULL, "@")) != NULL)
				printf("Next: %s\n",filebody[1024]);
				//strcpy(filename[i],"filename");
				//strcpy(filebody[i],pBuf);
				//strcat(filename[i],filebody[i]);
				pBuf=filename[i];
  			}		
 
			//Write the currently read data into our text file
			write_size = fwrite(pBuf, 1, buffersize, text); 
			free(pBuf);
 
			//Increment the total number of bytes read
			recv_size += read_size;
		}
	} 
	fclose(text);
	printf("File successfully Received! \n");
	return 1;
}
int main(int argc , char *argv[])
{
      long long int socket_desc , new_socket, c, read_size, buffer = 0;
      struct sockaddr_in server , client;
      char *readin;

      //Create socket

      socket_desc = socket(AF_INET , SOCK_STREAM , 0);
      if (socket_desc == -1)
      {
         printf("Could not create socket");
      }

      //Prepare the sockaddr_in structure

      server.sin_family = AF_INET;
      server.sin_addr.s_addr = INADDR_ANY;
      server.sin_port = htons( 6777 );

      //Bind

     if( bind(socket_desc,(struct sockaddr *)&server ,sizeof(server)) < 0)
     {
       puts("bind failed");
       return 1;
     }
     puts("Bind completed");

     //Listen

     listen(socket_desc,3);

      //Accept and incoming connection

      puts("Waiting for incoming connections...");
      c = sizeof(struct sockaddr_in);
      if((new_socket = accept(socket_desc,(struct sockaddr *)&client,(socklen_t *)&c)))
      {
		puts("Connection accepted");
      }
      fflush(stdout);
      if (new_socket<0)    
      {
      	perror("Accept Failed");
      	return 1;
      }
    	receive_text(new_socket);
    	close(socket_desc);
    	fflush(stdout);
    	return 0;
}

推荐答案

好的,显然你喜欢重新发明轮子,你可以使用FTP来实现这个目标....我要做的是发送一个固定大小的数据包开始,包含服务器端的信息 - 你可能有类似的事情



文件名 - 512字节

文件大小 - 4个字节长int?

CRC - x个字节



服务器从客户端收到此数据包然后接收文件大小字节的数据流,将其存储在临时文件中,并检查CRC - 如果一切正常,则将临时文件复制到文件名所指示的名称 - 显然是512字节fr文件名,长4个字节对于CRC,int,x字节必须是适合您系统的值



还有其他协议你可以看一下btw,为了灵感/帮助,'kermit'涌到mind
ok, obviously you love re-inventing the wheel, you could use FTP for this .... what I would do is send a fixed size packet to start with, containing information for the server side - you may have things like

Filename - 512 bytes
File Size - 4 bytes long int ?
CRC - x bytes

the server receives this 'packet' from the client, then receives a data stream for File Size bytes storing it in a temporary file, and checks the CRC - if all is ok, it copies the temp file to the name indicated by the Filename - obviously the 512 bytes fr Filename, 4 bytes long int, x bytes for the CRC must be values appropriate for your system

there are other protocols you could look at btw, for inspiration/help , 'kermit' springs to mind


venkat28写道:
venkat28 wrote:



@sergey。上面的代码是否可用于从客户端读取文件名及其内容,并将其发送到服务器中的所需位置。如果你发现任何错误,请指导我。


@sergey. Will the code above code for work for reading a filename and its contents from client and send it to server, in the desired location in the server. If u find any errors, kindly guide me.

让我们从第一个开始:

Let's start with the first one:

char *str="/home/sosdt009/Documents";



您正在对名为receive_text的函数中的某些路径进行硬编码。如果在任何地方使用此路径,则表示该函数不接收文件;它将一些文件放到一些硬编码的位置。



整个想法都错了。您需要定义一些基于TCP的应用层协议。 这样的协议,根据定义,应该定义两个部分的行为,而不仅仅是一个:服务器端和客户端。



请参阅:

应用层 - 维基百科,免费的百科全书 [ ^ ],

互联网协议套件 - 维基百科,免费的百科全书 [ ^ ],

通讯协议 - 维基百科,免费的百科全书 [ ^ ],

传输控制协议 - 维基百科,fr ee百科全书 [ ^ ]。



-SA


You are hard-coding some path inside the function called "receive_text". If this path is used anywhere, it means the function does not receive file; it puts some file to some hard-coded location.

The whole idea is wrong. You need to define some application-layer protocol based on TCP. Such protocol, by definitions, should define behavior of two parts, not just one: server-side and client-side.

Please see:
Application layer - Wikipedia, the free encyclopedia[^],
Internet protocol suite - Wikipedia, the free encyclopedia[^],
Communications protocol - Wikipedia, the free encyclopedia[^],
Transmission Control Protocol - Wikipedia, the free encyclopedia[^].

—SA


这篇关于如何使用文件名将文件从一个文件夹移动到另一个文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆