一、C语言读写文件
C语言中使用fopen函数打开文件,使用fread/fwrite函数进行读写数据。以下是一个简单的代码示例:
#include <stdio.h>
int main()
{
FILE *fp;
char buffer[1024];
int cnt;
// 打开文件
fp = fopen("test.txt", "r");
if(fp == NULL)
{
printf("打开文件失败!\n");
return -1;
}
// 读取文件内容
cnt = fread(buffer, sizeof(char), 1024, fp);
fclose(fp);
// 写入文件
fp = fopen("test_out.txt", "w");
if(fp == NULL)
{
printf("打开文件失败!\n");
return -1;
}
fwrite(buffer, sizeof(char), cnt, fp);
fclose(fp);
return 0;
}
上述代码中,我们先打开名为test.txt的文件,并读取文件内容到buffer中。接着,我们打开名为test_out.txt的文件,并将buffer中的内容写入到该文件当中。
二、C++读写文本文件
C++中可以使用fstream库进行文件读写。以下是一个读写文本文件的示例:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream fin("test.txt");
if(!fin.is_open())
{
std::cout << "打开文件失败!" << std::endl;
return -1;
}
std::string str;
while(std::getline(fin, str))
{
std::cout << str << std::endl;
}
fin.close();
std::ofstream fout("test_out.txt");
if(!fout.is_open())
{
std::cout << "打开文件失败!" << std::endl;
return -1;
}
fout << "hello world" << std::endl;
fout.close();
return 0;
}
上述代码中,我们首先使用ifstream打开test.txt文件,并使用getline函数逐行读取文件内容,并输出到控制台。接着,我们使用ofstream打开名为test_out.txt的文件,并将”hello world”写入到该文件中。
三、C++读写二进制文件
C++中可以使用fstream库进行二进制文件的读写操作。以下是一个读写二进制文件的示例:
#include <iostream>
#include <fstream>
struct Student
{
int number;
char name[10];
};
int main()
{
// 写入文件
std::ofstream fout("test_bin.dat");
if(!fout.is_open())
{
std::cout << "打开文件失败!" << std::endl;
return -1;
}
Student stu = {1, "Tom"};
fout.write((char*)&stu, sizeof(Student));
fout.close();
// 读取文件
std::ifstream fin("test_bin.dat");
if(!fin.is_open())
{
std::cout << "打开文件失败!" << std::endl;
return -1;
}
fin.read((char*)&stu, sizeof(Student));
std::cout << "number: " << stu.number << ", name: " << stu.name << std::endl;
fin.close();
return 0;
}
上述代码中,我们定义了一个Student结构体,包含学号和姓名两个字段。接着,我们使用ofstream打开名为test_bin.dat的二进制文件,并将一个Student对象写入该文件中。然后,我们使用ifstream打开该文件,并读取一个Student对象,输出其学号和姓名。
四、QT读写文件
QT中可以使用QFile进行文件读写,具体操作如下:
#include <QCoreApplication>
#include <QFile>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 读取文件
QFile fin("test.txt");
if(!fin.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "打开文件失败!" << fin.errorString();
return -1;
}
QByteArray content = fin.readAll();
qDebug() << content;
fin.close();
// 写入文件
QFile fout("test_out.txt");
if(!fout.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "打开文件失败!" << fout.errorString();
return -1;
}
fout.write("hello world", 11);
fout.close();
return a.exec();
}
上述代码中,我们使用QFile打开test.txt文件,并使用readAll函数读取文件内容并输出到控制台。接着,我们使用QFile打开名为test_out.txt的文件,并使用write函数写入”hello world”到该文件中。
五、Matlab读写文件
Matlab中可以使用load/save函数进行文件读写。load函数可以读取mat文件格式的数据,save函数可以保存mat文件格式的数据。以下是一个代码示例:
% 读取文件
data = load('test.mat');
disp(data);
% 写入文件
X = magic(5);
save('test.mat', 'X');
上述代码中,我们使用load函数读取名为test.mat的文件,并输出加载的数据。接着,我们使用save函数将一个5*5的魔方阵X保存到test.mat文件中。
六、C#读写xml文件
C#中可以使用XmlDocument类进行文件读写操作。以下是一个代码示例:
using System;
using System.Xml;
class Program
{
static void Main(string[] args)
{
// 读取文件
XmlDocument doc = new XmlDocument();
doc.Load("test.xml");
XmlNode root = doc.SelectSingleNode("root");
foreach(XmlNode child in root.ChildNodes)
{
Console.WriteLine(child.InnerText);
}
// 写入文件
XmlElement book = doc.CreateElement("book");
book.InnerXml = "<title>C# Programming</title><author>Tom</author><price>29.99</price>";
root.AppendChild(book);
doc.Save("test_out.xml");
}
}
上述代码中,我们使用XmlDocument类打开名为test.xml的文件,并输出xml文件中的内容。接着,我们创建一个XmlElement对象book,并使用InnerXml属性设置其内容。最后,我们将book添加到xml文档中,并通过Save函数将其写入到名为test_out.xml的文件中。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/233558.html
微信扫一扫
支付宝扫一扫