对于文件的读写不太熟悉,犯了不少错误。

i_f34.gif

比如,对于传送的文件中有各种文件,刚开始时用的r打开,只能实现txt文件的传输,并且传输后得到的文件的格式与源文件不一致。还有对于fgets与fread不太了解。


  • fget:

函数原型:char *fgets(char *string, int n, FILE *fp);

头文件:#include<stdio.h>

是否是标准函数:是

函数功能:从fp所指的文件中读取一个长度为(n-1)的字符串,并将该字符串存入以string为起始地址的缓冲区中。fgets函数有三个参数,其中string为缓冲区首地址,n规定了要读取的最大长度,fp为文件指针。

返回值:返回地址string,若遇到文件结束符或出错,返回NULL。用feof ferror判断是否出错。


  • fread:

函数原型:int fread(void *buf, int size, int count, FILE *fp);

头文件:#include<stdio.h>

是否是标准函数:是

函数功能:从fp指向的文件中读取长度为size count个数据项,并将它输入到以buf为首地址的缓冲区中。此时,文件指针fp会自动增加实际读入数据的字节数,即fp指向最后读入字符的下一个字符位置。

返回值:返回实际读入数据项的个数,即count值。若遇到错误或文件结束返回0


  • fwrite:

函数原型:int fwrite(void *buf, int size, int count, FILE *fp);

头文件:#include<stdio.h>

是否是标准函数:是

函数功能:将buf所指向的count*size个字节的数据输出到文件指针fp所指向的文件中去。该函数与fread相对,输出数据后,文件指针fp自动指向输出的最后一个字符的下一个位置。该函数常用于分批将数据块输出到文件中。

返回值:返回实际写入文件数据项个数。




在我的使用过程中感受是对获取的字符串处理则用fgets,连续读取文件则使用fread。



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "sock_class.h"
#define  RESPONSE_BUF_SIZE  4096
#define  DATA_BUF_SIZE      4096
int  FtpPut( const  char * szIP,  const  char * szPath,  const  char * szFile);
int  main( int  argc,  char  *argv[])
{
     char  szData[4096];
     if  (argc < 4)
     {
         printf ( "USAGE: %s IP Path File" , argv[0]);
         return  1;
     }
     if  (WinSockInit() != 0)
     {
         printf ( "WSAStartup failed with error \n" );
         return  1;
     }
     int  nRet = FtpPut(argv[1], argv[2], argv[3]);
                                                                                                                                                                           
     WSACleanup();
     return  nRet;
}
//处理响应信息
int  GetFtpReponse(tcp_sock& sockCtrl,  char * szRespBuf,  int  nLen)
{
     int  nRetCode;
     if  (sockCtrl.read_line(szRespBuf, nLen) == SOCKET_ERROR)
     {
         printf ( "GetFtpReponse error!\n" );
         return  0;
     }
     printf (szRespBuf);
     if  ( sscanf (szRespBuf,  "%ld" , &nRetCode) != 1)
     {
         printf ( "GetFtpReponse error(%s)!\n" , szRespBuf);
         return  0;
     }
     if  (szRespBuf[3] ==  '-' )
     {
         do
         {
             if  (sockCtrl.read_line(szRespBuf, nLen) == SOCKET_ERROR)
             {
                 printf ( "GetFtpReponse error!\n" );
                 return  0;
             }
             if  ((szRespBuf[0] >=  '0' ) && (szRespBuf[0] <=  '9' ))
             {
                 break ;
             }
         }
         while  (1);
     }
                                                                                                                                                                            
     return  nRetCode;
}
//发控制信息
int  SendFtpCmd(tcp_sock& sockCtrl,  const  char * szCmd)
{
     if  (sockCtrl.write_line(szCmd,  strlen (szCmd)) == SOCKET_ERROR)
     {
         printf ( "SendFtpCmd error(%s)!\n" , szCmd);
         return  0;
     }
     return  1;
}
//连接Ftp服务器
int  ConnectFtp(tcp_sock& sockCtrl,  const  char * szIP)
{
     char     szRespBuf[RESPONSE_BUF_SIZE + 1];
     if  (!sockCtrl.connect(szIP,  "21" ))
     {
         printf ( "connect error!\n" );
         return  1;
     }
                                                                                                                                                                            
     if  (GetFtpReponse(sockCtrl, szRespBuf, RESPONSE_BUF_SIZE) != 220)
     {
         printf ( "GetFtpReponse 220 error!\n" );
         return  1;
     }
     return  0;
}
//登陆Ftp服务器
int  LoginFtp(tcp_sock& sockCtrl)
{
     char     szRespBuf[RESPONSE_BUF_SIZE + 1];
     //User Name
     if  (!SendFtpCmd(sockCtrl,  "USER Anonymous" ))
     {
         printf ( "SendFtpCmd error!\n" );
         return  1;
     }
     if  (GetFtpReponse(sockCtrl, szRespBuf, RESPONSE_BUF_SIZE) != 331)
     {
         printf ( "GetFtpReponse 331 error!\n" );
         return  1;
     }
     //Pass
     if  (!SendFtpCmd(sockCtrl,  "PASS asdfa@sina.com" ))
     {
         printf ( "SendFtpCmd error!\n" );
         return  1;
     }
     if  (GetFtpReponse(sockCtrl, szRespBuf, RESPONSE_BUF_SIZE) != 230)
     {
         printf ( "GetFtpReponse 230 error!\n" );
         return  1;
     }
     return  0;
}
//上传文件
int  UpLoadFile(tcp_sock& sockCtrl,  const  char * szPath,  const  char * szFile)
{
     tcp_sock sockData;
     char     szRespBuf[RESPONSE_BUF_SIZE + 1];
     //切换到根目录
     if  (!SendFtpCmd(sockCtrl,  "CWD /" ))
     {
         printf ( "SendFtpCmd CWD error!\n" );
         return  1;
     }
     if  (GetFtpReponse(sockCtrl, szRespBuf, RESPONSE_BUF_SIZE) != 250)
     {
         printf ( "GetFtpReponse 250 error!\n" );
         return  1;
     }
                                                                                                                                                                            
     //调试,显示当前目录
     if  (!SendFtpCmd(sockCtrl,  "PWD" ))
     {
         printf ( "SendFtpCmd PWD error!\n" );
         return  1;
     }
     if  (GetFtpReponse(sockCtrl, szRespBuf, RESPONSE_BUF_SIZE) != 257)
     {
         printf ( "GetFtpReponse 257 error!\n" );
         return  1;
     }
                                                                                                                                                                            
     //设置传送类型为I
     if  (!SendFtpCmd(sockCtrl,  "TYPE I" ))
     {
         printf ( "SendFtpCmd \"TYPE I\" error!\n" );
         return  1;
     }
     if  (GetFtpReponse(sockCtrl, szRespBuf, RESPONSE_BUF_SIZE) != 200)
     {
         printf ( "GetFtpReponse 200 error!\n" );
         return  1;
     }
                                                                                                                                                                                
     //PASV
     if  (!SendFtpCmd(sockCtrl,  "PASV" ))
     {
         printf ( "SendFtpCmd error!\n" );
         return  1;
     }
     if  (GetFtpReponse(sockCtrl, szRespBuf, RESPONSE_BUF_SIZE) != 227)
     {
         printf ( "GetFtpReponse 227 error!\n" );
         return  1;
     }
                                                                                                                                                                            
     char * pPos =  strchr (szRespBuf,  '(' );
     if  (pPos == NULL)
     {
         printf ( "GetFtpReponse Pasv error!\n" );
         return  1;
     }
     int  ip1, ip2, ip3, ip4, port1, port2;
     if  ( sscanf (pPos,  "(%d,%d,%d,%d,%d,%d)" , &ip1, &ip2,
         &ip3, &ip4, &port1, &port2) != 6)
     {
         printf ( "GetFtpReponse Pasv Address error!\n" );
         return  1;
     }
                                                                                                                                                                            
     //生成ip与端口
     char  szIP[50];
     char  szPort[10];
     sprintf (szIP,  "%d.%d.%d.%d" , ip1, ip2, ip3, ip4);
     sprintf (szPort,  "%d" , port1*256 + port2);
                                                                                                                                                                            
     //数据连接
     if  (!sockData.connect(szIP, szPort))
     {
         printf ( "Data connect error!\n" );
         return  1;
     }
                                                                                                                                                                            
     //STOR
     char  szCmdSTOR[100];
     sprintf (szCmdSTOR,  "STOR %s" , szFile);
     if  (!SendFtpCmd(sockCtrl, szCmdSTOR)) //发送文件的命令
     {
         printf ( "SendFtpCmd \"STOR %s\" error!\n" ,szFile);
         return  1;
     }
     if  (GetFtpReponse(sockCtrl, szRespBuf, RESPONSE_BUF_SIZE) != 150)
     {
         printf ( "GetFtpReponse 150 error!\n" );
         return  1;
     }
                                                                                                                                                                                
     //把文件读入缓冲区
     FILE * fp =  fopen (szFile,  "rb" ); //必须是rb:二进制打开,只读
     if (fp == NULL)
     {
         printf ( "Open file %s Error! \n" , szFile);
         return  1;
     }
                                                                                                                                                                                
     char  szBuf[DATA_BUF_SIZE];
     int  nReaded = 0;
     while (1)
     {
         nReaded =  fread (szBuf, sizeof ( char ), DATA_BUF_SIZE, fp); //读入到szBuf,最多读入DATA_BUF_SIZE*sizeof(char)
         //printf("--nReaded:%d\n",nReaded);//测试
         if  (nReaded == 0)
             break ;
         //szBuf中数据写入套接字缓冲区并发送
         sockData.write(szBuf, nReaded);
         memcpy (szBuf, "\0" ,DATA_BUF_SIZE);
         //printf("sended\n\n");//测试
     }
     //printf("out\n");//测试
     sockData.close();
     fclose (fp);
     //接收成功吗?
     if  (GetFtpReponse(sockCtrl, szRespBuf, RESPONSE_BUF_SIZE) != 226)
     {
         printf ( "GetFtpReponse 226 error!\n" );
         return  1;
     }
     return  0;
}
//退出登录
int  LogoutFtp(tcp_sock& sockCtrl)
{
     char     szRespBuf[RESPONSE_BUF_SIZE + 1];
                                                                                                                                                                            
     //QUIT
     if  (!SendFtpCmd(sockCtrl,  "QUIT" ))
     {
         printf ( "SendFtpCmd Quit error!\n" );
         return  1;
     }
     if  (GetFtpReponse(sockCtrl, szRespBuf, RESPONSE_BUF_SIZE) != 221)
     {
         printf ( "GetFtpReponse 221 error!\n" );
         return  1;
     }
     return  0;
}
//向ftp服务器发送文件
int  FtpPut( const  char * szIP,  const  char * szPath,  const  char * szFile)
{
     tcp_sock sockCtrl;
     char     szRespBuf[RESPONSE_BUF_SIZE + 1];
                                                                                                                                                                            
     if  (ConnectFtp(sockCtrl, szIP) != 0)
         return  1;
     if  (LoginFtp(sockCtrl) != 0)
         return  1;
     if  (UpLoadFile(sockCtrl, szPath, szFile) != 0)
         return  1;
     LogoutFtp(sockCtrl);
     return  0;
}