c英语判断文件是否为空
时间: 2023-08-09 13:06:13 浏览: 110
在C语言中,我们可以使用文件指针和文件操作函数来判断一个文件是否为空。具体实现如下:
```c
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("filename.txt", "r");
if (fp == NULL) {
printf("File does not exist.\n");
return 0;
}
fseek(fp, 0, SEEK_END);
if (ftell(fp) == 0) {
printf("File is empty.\n");
} else {
printf("File is not empty.\n");
}
fclose(fp);
return 0;
}
```
首先,我们打开指定的文件,使用fopen()函数,如果文件不存在,fp将会是NULL。然后,我们使用fseek()函数将文件指针移动到文件末尾,使用ftell()函数获取文件长度,如果文件长度为0,则说明文件为空。最后,我们关闭文件指针,使用fclose()函数。
相关问题
用stack判断一个含有大括号中括号小括号和英文字母的文件的括号是不是匹配给我代码
以下是使用Stack判断括号是否匹配的示例代码:
```java
import java.util.Stack;
public class BracketMatcher {
public static boolean isMatched(String input) {
Stack<Character> stack = new Stack<>();
for (char c : input.toCharArray()) {
if (c == '{' || c == '[' || c == '(') {
stack.push(c);
} else if (c == '}' || c == ']' || c == ')') {
if (stack.isEmpty()) {
return false;
}
char top = stack.pop();
if ((top == '{' && c != '}') ||
(top == '[' && c != ']') ||
(top == '(' && c != ')')) {
return false;
}
}
}
return stack.isEmpty();
}
public static void main(String[] args) {
String input = "{[(Hello) World]}";
if (isMatched(input)) {
System.out.printf("%s is matched\n", input);
} else {
System.out.printf("%s is not matched\n", input);
}
}
}
```
在上面的代码中,我们使用了一个Stack来维护括号的匹配情况。我们遍历输入字符串中的每个字符,如果遇到左括号则将其压入栈中,如果遇到右括号则弹出栈顶元素,判断其与右括号是否匹配。如果栈为空或者弹出的栈顶元素与右括号不匹配,则返回false。最后,如果栈为空,则说明所有括号都匹配。
设计一个哈夫曼编码译码系统,对某个英文文本文件(.txt)中的字符进行哈夫曼编码,并给我c语言的代码
好的,我能帮您解答这个问题。首先,哈夫曼编码是一种常用的编码技术,适用于对英文文本文件中的字符进行编码压缩。下面是实现该任务的 C 语言代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TREE_HT 100
// 结构体:哈夫曼树的节点
struct MinHeapNode {
char data; // 节点对应的字符
unsigned freq; // 字符出现的频率
struct MinHeapNode *left, *right; // 左右子节点
};
// 结构体:哈夫曼树
struct MinHeap {
unsigned size; // 堆的大小
unsigned capacity; // 堆的容量
struct MinHeapNode **array; // 节点数组
};
// 函数:创建哈夫曼树的节点
struct MinHeapNode* newNode(char data, unsigned freq)
{
struct MinHeapNode* node = (struct MinHeapNode*)malloc(sizeof(struct MinHeapNode));
node->left = node->right = NULL;
node->data = data;
node->freq = freq;
return node;
}
// 函数:创建空的哈夫曼树
struct MinHeap* createMinHeap(unsigned capacity)
{
struct MinHeap* minHeap = (struct MinHeap*)malloc(sizeof(struct MinHeap));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array = (struct MinHeapNode**)malloc(minHeap->capacity * sizeof(struct MinHeapNode*));
return minHeap;
}
// 函数:交换两个节点的位置
void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b)
{
struct MinHeapNode* t = *a;
*a = *b;
*b = t;
}
// 函数:最小堆的调整
void minHeapify(struct MinHeap* minHeap, int idx)
{
int smallest = idx;
int left = 2 * idx + 1;
int right = 2 * idx + 2;
if (left < minHeap->size && minHeap->array[left]->freq < minHeap->array[smallest]->freq)
smallest = left;
if (right < minHeap->size && minHeap->array[right]->freq < minHeap->array[smallest]->freq)
smallest = right;
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
// 函数:判断是否为叶子节点
int isLeaf(struct MinHeapNode* root)
{
return !(root->left) && !(root->right);
}
// 函数:从最小堆中取出频率最小的节点
struct MinHeapNode* extractMin(struct MinHeap* minHeap)
{
struct MinHeapNode* temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
// 函数:插入节点到最小堆中
void insertMinHeap(struct MinHeap* minHeap, struct MinHeapNode* minHeapNode)
{
++minHeap->size;
int i = minHeap->size - 1;
while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {
minHeap->array[i] = minHeap->array[(i - 1) / 2];
i = (i - 1) / 2;
}
minHeap->array[i] = minHeapNode;
}
// 函数:建立哈夫曼树
void buildMinHeap(struct MinHeap* minHeap)
{
int n = minHeap->size - 1;
int i;
for (i = (n - 1) / 2; i >= 0; --i) {
minHeapify(minHeap, i);
}
}
// 函数:打印编码表
void printCodes(struct MinHeapNode* root, int arr[], int top)
{
if (root->left) {
arr[top] = 0;
printCodes(root->left, arr, top + 1);
}
if (root->right) {
arr[top] = 1;
printCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) {
printf("%c: ", root->data);
int i;
for (i = 0; i < top; ++i) {
printf("%d", arr[i]);
}
printf("\n");
}
}
// 函数:建立哈夫曼树并打印编码表,返回根结点指针
struct MinHeapNode* huffmanCodes(char data[], int freq[], int size)
{
struct MinHeapNode *left, *right, *top;
struct MinHeap* minHeap = createMinHeap(size);
for (int i = 0; i < size; ++i) {
minHeap->array[i] = newNode(data[i], freq[i]);
}
minHeap->size = size;
buildMinHeap(minHeap);
while (minHeap->size != 1) {
left = extractMin(minHeap);
right = extractMin(minHeap);
top = newNode('#', left->freq + right->freq);
top->left = left;
top->right = right;
insertMinHeap(minHeap, top);
}
int arr[MAX_TREE_HT], top = 0;
struct MinHeapNode* root = extractMin(minHeap);
printCodes(root, arr, top);
return root;
}
// 函数:将编码表写入文件
void writeToFile(struct MinHeapNode* root, char data[], int freq[], int size)
{
FILE *fp = fopen("result.txt", "w");
if (fp == NULL) {
printf("Error in opening file!");
return;
}
int arr[MAX_TREE_HT], top = 0;
for (int i = 0; i < size; ++i) {
fprintf(fp, "%c:", data[i]);
printCodes(root, arr, top);
}
fclose(fp);
}
// 函数:读取文件并计算字符频率
void readFromFileAndBuildTree()
{
char filename[100], ch;
int frequency[256] = { 0 }, i = 0;
FILE *fp;
printf("Enter the filename: ");
gets(filename);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error in opening file!");
return;
}
// 计算每个字符出现的频率
while ((ch = fgetc(fp)) != EOF) {
frequency[ch]++;
}
fclose(fp);
int size = 0;
for (int i = 0; i < 256; ++i) {
if (frequency[i] != 0) {
size++;
}
}
// 将字符和频率存入对应的数组中
char data[size];
int freq[size];
int j = 0;
for (i = 0; i < 256; ++i) {
if (frequency[i] != 0) {
data[j] = (char)i;
freq[j] = frequency[i];
j++;
}
}
// 建立哈夫曼树
struct MinHeapNode* root = huffmanCodes(data, freq, size);
// 将编码表写入文件
writeToFile(root, data, freq, size);
}
// 主函数:读取文件并压缩
int main()
{
readFromFileAndBuildTree();
return 0;
}
```
您可以将上述代码保存为 .c 文件,并在已安装 C 编译器的环境下编译运行。注意,本程序默认读取当前目录下的 .txt 文件,编码结果将保存在 result.txt 文件中。
阅读全文