C++,Python,Javascripts操作文件读写,字符串分割

C++

读文件:

前提是需要包含头文件

cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>

逐行读取

cpp 复制代码
std::ifstream file("xxx/");
if(!file)
{
    std::cout<<"file don not open"<<std::endl;
}
else {
    std::string line;
    while(getline(file,line))
    {
        std::cout<<"content:"<<line<<std::endl;
    }
    file.close();
}

逐字

cpp 复制代码
std::ifstream file("xxx/");
if (!file) {
   std::cout << "无法打开文件!" << std::endl;
   return 1;
}
char character;
while (file.get(character)) {
     std::cout << character;
}
file.close();
return 0;

写文件

cpp 复制代码
  std::ofstream outfile.open("xxx/文件名", std::ios::app);
  if(!outfile.is_open ()) cout << "Open file failure!" << endl;
  else{
    outfile<<ask;    
  }
  outfile.close();
  std::cout << "Write file success!" << endl;
  • **ios::app:**模式用于在文件末尾追加数据,而不会覆盖原有内容。
  • ios::out: 打开文件进行写操作。如果文件不存在,则创建新文件;如果文件存在,则清空文件内容。这是默认模式。
  • ios::in: 打开文件进行读操作。
  • **ios::in | ios::out:**同时读写
  • ios::binary: 以二进制模式打开文件。这会影响数据的读取和写入方式,特别是对于非文本数据。
  • ios::ate: 打开文件并将文件指针定位到文件末尾。这样可以直接从文件末尾开始读写操作
  • ios::nocreate: 不创建新文件。如果文件不存在,则打开失败。

Python

读文件

  • 'r':以只读方式打开文件(默认)。如果文件不存在,会抛出FileNotFoundError异常。
  • 'w':以写入方式打开文件。如果文件已存在,则清空文件内容;如果文件不存在,则创建新文件。
  • 'a':以追加方式打开文件。如果文件已存在,则在文件末尾追加内容;如果文件不存在,则创建新文件。
  • 'x':如果文件不存在,则以写入方式创建一个新文件;如果文件已存在,则抛出FileExistsError异常。
  • 'b':以二进制模式打开文件。
  • 't':以文本模式打开文件(默认)。
  • '+':以读写模式打开文件。

读取一行

python 复制代码
with open('example.txt', 'r') as file:
    for line in file:
        print(line.strip())

一次读取所有

python 复制代码
with open('example.txt', 'r') as file:
    content = file.read()
    print(content)

写文件

写入前清空

cpp 复制代码
with open('example.txt', 'w') as file:
    file.write("Hello, World!\n")

追加

python 复制代码
with open('example.txt', 'a') as file:
    file.write("Hello word\n")

JavaScripts

读文件(二进制格式)

javascript 复制代码
//创建读文件对象
const reader = new FileReader();
//读取文件内容,二进制格式
reader.readAsArrayBuffer(file);
//二进制格式读取完成后触发
reader.onload = function(e) {
    //获取文件内容
    const filedata = e.target.result;
    console.log(filedata)

};

读文件(URL格式)

javascript 复制代码
//创建读文件对象
const reader = new FileReader();
//读取文件内容,URL格式
reader.readAsDataURL(file);

//URL格式读取完成后触发
reader.onload = function(e) {
        const picdata = e.target.result;
        //可以将URL存入到img对象其中的src属性
        

写文件(保存图像)

FileSaver.js库保存图片

html 复制代码
   <script src="https://cdn.bootcdn.net/ajax/libs/FileSaver.js/2.0.5/FileSaver.js"></script>

SaveAs函数使用

假设服务器端发来了图片的二进制数据rece_picblob,则他是一个Blod对象

html 复制代码
saveAs(rece_picblob,"output.png");

字符串分割

python

直接使用内置函数split,返回值是一个列表,可以根据索引获取分割项。

python 复制代码
str =  "Hello word"
list = str.split(' ')
list_one = str.split(' ')[0]
list_two = str.split(' ')[1]

c++

cpp 复制代码
std::vector<std::string> split(const std::string &str,char splitchar)
{
    std::istringstream stream(str);
    std::string ele;
    std::vector<std::string> output;
    while(std::getline(stream,elemsplitchar))
    {
        output.push_back(ele);
    }
    return output;
}
相关推荐
你怎么知道我是队长3 小时前
C语言---枚举变量
c语言·开发语言
李慕婉学姐3 小时前
【开题答辩过程】以《基于JAVA的校园即时配送系统的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·开发语言·数据库
吃茄子的猫3 小时前
quecpython中&的具体含义和使用场景
开发语言·python
じ☆冷颜〃4 小时前
黎曼几何驱动的算法与系统设计:理论、实践与跨领域应用
笔记·python·深度学习·网络协议·算法·机器学习
云栖梦泽4 小时前
易语言中小微企业Windows桌面端IoT监控与控制
开发语言
数据大魔方4 小时前
【期货量化实战】日内动量策略:顺势而为的短线交易法(Python源码)
开发语言·数据库·python·mysql·算法·github·程序员创富
APIshop4 小时前
Python 爬虫获取 item_get_web —— 淘宝商品 SKU、详情图、券后价全流程解析
前端·爬虫·python
fpcc4 小时前
C++编程实践——链式调用的实践
c++
风送雨4 小时前
FastMCP 2.0 服务端开发教学文档(下)
服务器·前端·网络·人工智能·python·ai
XTTX1104 小时前
Vue3+Cesium教程(36)--动态设置降雨效果
前端·javascript·vue.js