常用文件IO函数 -open/close

1.open函数

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int open(const char *pathname, int flags);

int open(const char *pathname, int flags, mode_t mode);

功能:
打开文件,如果文件不存在则可以选择创建。
参数:
pathname:文件的路径及文件名
flags:打开文件的行为标志,必选项 O_RDONLY, O_WRONLY, O_RDWR
mode:这个参数,只有在文件不存在时有效,指新建文件时指定文件的权限

返回值:
成功:成功返回打开的文件描述符
失败:-1

flags详细说明
必选项:取值含义

O_RDONLY	以只读的方式打开
O_WRONLY	以只写的方式打开
O_RDWR	以可读、可写的方式打开

可选项,和必选项按位或起来
取值 含义

O_CREAT	文件不存在则创建文件,使用此选项时需使用mode说明文件的权限
O_EXCL	如果同时指定了O_CREAT,且文件已经存在,则出错
O_TRUNC	如果文件存在,则清空文件内容
O_APPEND	写文件时,数据添加到文件末尾
O_NONBLOCK	对于设备文件, 以O_NONBLOCK方式打开可以做非阻塞I/O

mode补充说明

1) 文件最终权限:mode & ~umask

2) shell进程的umask掩码可以用umask命令查看

Ø umask:查看掩码(补码)

Ø umask mode:设置掩码,mode为八进制数

Ø umask -S:查看各组用户的默认操作权限

取值 八进制 含义

S_IRWXU	00700	文件所有者的读、写、可执行权限
S_IRUSR	00400	文件所有者的读权限
S_IWUSR	00200	文件所有者的写权限
S_IXUSR	00100	文件所有者的可执行权限
S_IRWXG	00070	文件所有者同组用户的读、写、可执行权限
S_IRGRP	00040	文件所有者同组用户的读权限
S_IWGRP	00020	文件所有者同组用户的写权限
S_IXGRP	00010	文件所有者同组用户的可执行权限
S_IRWXO	00007	其他组用户的读、写、可执行权限
S_IROTH	00004	其他组用户的读权限
S_IWOTH	00002	其他组用户的写权限
S_IXOTH	00001	其他组用户的可执行权限

用这个也可以

默认:
目录0755
文件0644
在这里插入图片描述
一般0777,0755

2.close函数

#include <unistd.h>

int close(int fd);

功能:
关闭已打开的文件
参数:
fd : 文件描述符,open()的返回值

返回值:
成功:0
失败: -1, 并设置errno

说明:
需要说明的是,当一个进程终止时,内核对该进程所有尚未关闭的文件描述符调用close关闭,所以即使用户程序不调用close,在终止时内核也会自动关闭它打开的所有文件

但是对于一个长年累月运行的程序(比如网络服务器),打开的文件描述符一定要记得关闭,否则随着打开的文件越来越多,会占用大量文件描述符和系统资源。
在这里插入图片描述

代码

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>



#if 0
/* Standard file descriptors.  */
#define	STDIN_FILENO	0	/* Standard input.  */
#define	STDOUT_FILENO	1	/* Standard output.  */
#define	STDERR_FILENO	2	/* Standard error output.  */
#endif

int main(void)
{
    int fd = -1; 

    //1. 打开文件
    fd = open("txt", O_RDWR | O_CREAT, 0644);
    if (-1 == fd)
    {
        perror("open"); 
        return 1;
    }


    printf("fd = %d\n", fd);
    printf("打开文件成功....\n");

    //2. 关闭文件 释放资源
    close(fd);

    return 0;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 你可以使用Java中的HttpURLConnection类来发送POST请求。以下是一个简单的示例: ```java import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class PostRequestExample { private final String USER_AGENT = "Mozilla/5.0"; public static void main(String[] args) throws Exception { PostRequestExample http = new PostRequestExample(); // POST请求URL String url = "http://111.203.10.155/MicroApp/nmsmc-system-basic/dict"; // POST请求参数 String urlParameters = "param1=value1&param2=value2"; // 发送POST请求 String response = http.sendPost(url, urlParameters); System.out.println(response); } // 发送POST请求 private String sendPost(String url, String urlParameters) throws Exception { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // 添加请求头 con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); // 发送POST请求 con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); // 读取响应内容 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); } } ``` 在上面的示例中,我们通过设置请求方法为“POST”,添加请求头和发送POST请求参数来发送POST请求。在响应中,我们使用BufferedReader读取响应内容。你需要将示例中的URL和请求参数替换为你自己的URL和请求参数。 ### 回答2: 在Java中使用POST请求访问http://111.203.10.155/MicroApp/nmsmc-system-basic/dict的步骤如下: 1. 创建一个URL对象,将要访问的地址传递给URL构造函数。 ```java URL url = new URL("http://111.203.10.155/MicroApp/nmsmc-system-basic/dict"); ``` 2. 创建一个HttpURLConnection对象,通过调用URL对象的openConnection方法。 ```java HttpURLConnection connection = (HttpURLConnection) url.openConnection(); ``` 3. 设置请求方法为POST。 ```java connection.setRequestMethod("POST"); ``` 4. 设置请求头的内容类型。 ```java connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); ``` 5. 开启输出流,准备发送请求体。 ```java connection.setDoOutput(true); ``` 6. 准备请求参数,并将其写入输出流。 ```java String parameters = "key1=value1&key2=value2"; OutputStream outputStream = connection.getOutputStream(); outputStream.write(parameters.getBytes()); outputStream.flush(); outputStream.close(); ``` 7. 发送请求并获取响应码。 ```java int responseCode = connection.getResponseCode(); ``` 8. 判断响应码是否为200,表示请求成功。 ```java if (responseCode == 200) { // 请求成功,获取响应数据 InputStream inputStream = connection.getInputStream(); // 处理响应数据 inputStream.close(); } ``` 需要注意的是,除了以上步骤,如果服务器要求进行身份验证,还需要添加相应的认证信息,比如用户名和密码。另外,在实际应用中可能会遇到更多的异常情况,需要进行适当的异常处理。以上步骤只是基本的POST请求流程,具体的实现还需要根据实际情况进行调整。 ### 回答3: 在Java程序中使用Post请求访问"http://111.203.10.155/MicroApp/nmsmc-system-basic/dict",可以通过使用Java提供的HttpClient库来实现。 首先,我们需要引入HttpClient库的依赖。在构建工具如Maven或Gradle中,可以添加以下依赖: Maven: ```xml <dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> </dependencies> ``` Gradle: ```groovy dependencies { implementation 'org.apache.httpcomponents:httpclient:4.5.13' } ``` 然后,可以通过以下代码片段使用Post请求访问目标URL: ```java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.util.ArrayList; import java.util.List; public class PostExample { public static void main(String[] args) { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost("http://111.203.10.155/MicroApp/nmsmc-system-basic/dict"); // 设置请求参数 List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("key1", "value1")); params.add(new BasicNameValuePair("key2", "value2")); try { httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); // 处理响应结果 if (httpEntity != null) { String responseString = EntityUtils.toString(httpEntity, "UTF-8"); System.out.println(responseString); } } catch (Exception e) { e.printStackTrace(); } } } ``` 以上代码示例创建一个HttpPost对象,并通过setEntity方法设置请求参数。然后使用HttpClient的execute方法发送请求并获取响应。最后,可以通过解析响应实体中的内容,使用EntityUtils将其转换为字符串并进行处理。 当然,具体的请求参数和响应结果处理需要根据目标URL的要求进行调整,以上代码仅提供了一个Post请求案例的基本骨架。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

打酱油的;

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值