C 判断字符串是否为txt文件名
时间: 2023-09-24 12:02:59 浏览: 40
可以通过判断字符串是否以 ".txt" 结尾来判断字符串是否为 txt 文件名。可以使用 C 语言中的字符串操作函数 `strstr()` 和 `strlen()` 来实现。代码示例如下:
```c
#include <stdio.h>
#include <string.h>
int main() {
char filename[] = "example.txt";
char *extension = ".txt";
// 判断字符串是否以 ".txt" 结尾
if (strstr(filename, extension) == filename + strlen(filename) - strlen(extension)) {
printf("The file is a txt file.\n");
} else {
printf("The file is not a txt file.\n");
}
return 0;
}
```
在上面的示例中,我们使用 `strstr()` 函数来查找 `filename` 字符串中是否包含 `.txt` 子字符串,如果找到了就返回 `.txt` 在 `filename` 中的指针,否则返回 `NULL`。然后我们使用 `strlen()` 函数来计算 `filename` 和 `.txt` 的长度,如果它们的长度相同并且 `.txt` 在 `filename` 的末尾,就说明该文件是一个 `.txt` 文件。
相关问题
输入一串字符串以txt文本的形式输出的c语言代码
以下是将字符串输出到txt文本的c语言代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!"; // 要输出的字符串
FILE *fp; // 文件指针
fp = fopen("output.txt", "w"); // 打开文件并以写入模式打开
if (fp == NULL) { // 判断文件是否打开成功
printf("打开文件失败!\n");
return 1;
}
fprintf(fp, "%s", str); // 将字符串写入文件
fclose(fp); // 关闭文件
printf("写入成功!\n");
return 0;
}
```
注意,这只是一个简单的示例代码,实际应用中需要添加错误处理和其他必要的功能。同时,需要根据具体的需求修改文件名和要输出的字符串。
EDK2编写UEFI代码,判断指定文件名的文件中是否存在某字符串
以下是一个简单的示例代码,用于在指定文件名的文件中搜索特定字符串:
```c
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/BaseMemoryLib.h>
#include <Protocol/SimpleFileSystem.h>
EFI_STATUS EFIAPI UefiMain(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
{
EFI_STATUS Status;
// Open the root directory
EFI_HANDLE DeviceHandle = NULL;
Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &gEfiDevicePathProtocolGuid, &DeviceHandle);
if (EFI_ERROR(Status)) {
Print(L"Failed to locate device path: %r\n", Status);
return Status;
}
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem;
Status = gBS->OpenProtocol(DeviceHandle, &gEfiSimpleFileSystemProtocolGuid, (VOID **)&FileSystem, ImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if (EFI_ERROR(Status)) {
Print(L"Failed to open protocol: %r\n", Status);
return Status;
}
EFI_FILE_PROTOCOL *Root;
Status = FileSystem->OpenVolume(FileSystem, &Root);
if (EFI_ERROR(Status)) {
Print(L"Failed to open root directory: %r\n", Status);
return Status;
}
// Open the file
CHAR16 *FileName = L"test.txt";
EFI_FILE_PROTOCOL *File;
Status = Root->Open(Root, &File, FileName, EFI_FILE_MODE_READ, 0);
if (EFI_ERROR(Status)) {
Print(L"Failed to open file %s: %r\n", FileName, Status);
return Status;
}
// Read the file contents
UINTN BufferSize = 1024;
CHAR8 Buffer[BufferSize];
Status = File->Read(File, &BufferSize, Buffer);
if (EFI_ERROR(Status)) {
Print(L"Failed to read file %s: %r\n", FileName, Status);
return Status;
}
// Search for the string
CHAR8 *SearchString = "hello";
if (StrStr(Buffer, SearchString) != NULL) {
Print(L"Found \"%a\" in file %s\n", SearchString, FileName);
} else {
Print(L"Could not find \"%a\" in file %s\n", SearchString, FileName);
}
// Clean up
File->Close(File);
Root->Close(Root);
return EFI_SUCCESS;
}
```
该代码首先打开文件系统的根目录,然后打开指定的文件。它将文件内容读入缓冲区,然后在该缓冲区中搜索特定字符串。如果找到了该字符串,则输出一条消息,否则输出另一条消息。最后,文件和根目录被关闭以释放资源。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)