1. 编写一个程序,将一段文字写入磁盘文件《姓名》.dat保存起来。
时间: 2024-03-20 09:40:17 浏览: 20
好的,以下是一个简单的 Python 代码示例,可以将用户输入的文本写入到指定的文件中:
```python
filename = input("请输入文件名:") + ".dat" # 拼接文件名,添加后缀 .dat
text = input("请输入要写入文件的文本:")
with open(filename, 'w', encoding='utf-8') as f:
f.write(text)
print("文件已成功写入!")
```
这个程序会先要求用户输入文件名和要写入的文本,然后将文本写入到指定文件中。注意,在打开文件时,我们使用了 `w` 模式,这意味着会清空文件中的内容并重新写入新的内容。如果你希望将新内容追加到文件末尾,可以使用 `a` 模式。
相关问题
1. 编写一个程序,将一段文字写入磁盘文件《姓名》.dat保存起来。要求文件的第一部分保存文件的结构(含密钥的变形码)
好的,我了解了你的需求。下面是一个基本的实现思路:
1. 定义一个文件结构体,包含文件名、文件长度、加密标志、密钥等信息。
2. 将要写入文件的文本进行加密处理。
3. 将加密后的文本和文件结构体写入文件中。
下面是一个简单的代码示例,供你参考:
```python
import os
import struct
# 加密函数,这里用简单的异或操作
def encrypt(text, key):
encrypted = ''
for c in text:
encrypted += chr(ord(c) ^ key)
return encrypted
# 定义文件结构体
class FileHeader:
def __init__(self, filename, length, encrypted, key):
self.filename = filename
self.length = length
self.encrypted = encrypted
self.key = key
def pack(self):
# 使用struct模块将结构体打包成二进制数据
return struct.pack('128sih16s', self.filename.encode('utf-8'), self.length, self.encrypted, self.key.encode('utf-8'))
# 文件名和要写入的文本
filename = 'test.dat'
text = 'Hello, world!'
# 加密文本
key = 0x88
encrypted_text = encrypt(text, key)
# 构造文件头
header = FileHeader(filename, len(encrypted_text), 1, str(key))
# 写入文件头和加密后的文本
with open(filename, 'wb') as f:
f.write(header.pack())
f.write(encrypted_text.encode('utf-8'))
```
这个程序会将加密后的文本和文件结构体写入磁盘文件test.dat中。文件的第一部分是文件结构体,包含文件名、文件长度、加密标志和密钥等信息。
用c语言1. 编写一个程序,将一段文字写入磁盘文件《姓名》.dat保存起来。要求文件的第一部分保存文件的结构(含密钥的变形码),第二部分是文字用密钥加密的密文。文件被重新打开时要求输入密码并验证密码并限定密码的输入次数。
以下是一个简单的示例程序,实现了将一段文字写入磁盘文件并进行加密:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TEXT_LEN 1024
#define MAX_PASS_LEN 32
#define FILE_NAME "姓名.dat"
// 加密函数,对文本进行加密
void encrypt(char* text, char* password) {
int i, j;
int text_len = strlen(text);
int pwd_len = strlen(password);
for (i = 0, j = 0; i < text_len; i++, j++) {
if (j == pwd_len) j = 0;
text[i] = text[i] ^ password[j];
}
}
int main() {
char text[MAX_TEXT_LEN]; // 存储要加密的文本
char password[MAX_PASS_LEN]; // 存储密码
char filename[64]; // 存储文件名
FILE* fp;
int i, pwd_attempts = 3;
// 读取要加密的文本
printf("请输入要加密的文本:\n");
fgets(text, MAX_TEXT_LEN, stdin);
text[strlen(text) - 1] = '\0';
// 读取密码
printf("请输入密码:\n");
fgets(password, MAX_PASS_LEN, stdin);
password[strlen(password) - 1] = '\0';
// 加密文本
encrypt(text, password);
// 创建文件
sprintf(filename, "%s", FILE_NAME);
fp = fopen(filename, "wb");
if (fp == NULL) {
printf("创建文件失败!\n");
return 1;
}
// 写入密码和加密后的文本
fwrite(&pwd_attempts, sizeof(int), 1, fp); // 写入密码输入次数
fwrite(password, sizeof(char), MAX_PASS_LEN, fp); // 写入密码
fwrite(text, sizeof(char), MAX_TEXT_LEN, fp); // 写入加密后的文本
// 关闭文件
fclose(fp);
// 重新打开文件并验证密码
fp = fopen(filename, "rb");
if (fp == NULL) {
printf("打开文件失败!\n");
return 1;
}
// 读取密码输入次数
fread(&pwd_attempts, sizeof(int), 1, fp);
// 循环输入密码进行验证
for (i = 0; i < pwd_attempts; i++) {
printf("请输入密码:\n");
fgets(password, MAX_PASS_LEN, stdin);
password[strlen(password) - 1] = '\0';
// 读取密码并验证
char pwd[MAX_PASS_LEN];
fread(pwd, sizeof(char), MAX_PASS_LEN, fp);
if (strcmp(pwd, password) == 0) {
// 验证成功,读取加密后的文本并解密
char enc_text[MAX_TEXT_LEN];
fread(enc_text, sizeof(char), MAX_TEXT_LEN, fp);
encrypt(enc_text, password);
printf("解密后的文本为:%s\n", enc_text);
break;
} else {
printf("密码错误!\n");
}
}
// 关闭文件
fclose(fp);
return 0;
}
```
该程序首先读取要加密的文本和密码,然后对文本进行加密,并将加密后的文本和密码写入文件。写入文件时,先写入密码输入次数(默认为3次),然后写入密码和加密后的文本。程序重新打开文件并验证密码时,先读取密码输入次数,然后循环输入密码进行验证。如果输入正确,则读取加密后的文本并解密输出。如果输入错误,则继续循环,最多可输入密码输入次数次。注意,为了保证文件的安全性,实际应用中应该使用更加安全的加密算法。