[C/C++]读取文件的多种方式

第一种:fgetc

int fopentest()
{
    FILE *file;
    char c;
    file  = fopen("./test.txt", "r");
    if (file == NULL) {
        printf("打开文件失败");
        return -1;
    }
    while(1)
    {
        c = fgetc(file);
        if (feof(file)) {
            break;
        }
        printf("%c", c);
    }
    fclose(file);

    return 0;
}

第二种:fread

#include <stdio.h>
#include <string.h>

int main() {
    FILE *fp = fopen("./test.txt", "r");
    if (fp == NULL) {
        return -1;
    }
    char buf[1024];
    while(1) {
        // 清空缓冲区
        memset(buf, 0, sizeof(buf));
        int fr = fread(buf, sizeof(char), sizeof(buf), fp);
        if (fr <= 0) {
            break;
        }
        printf("%s\n", buf);
    }
    fclose(fp);

    return 0;
}

第三:判断文件夹是否存在

windows:
#include <io.h>
string folder = "d:/pictures";
if (_access(folder.c_str(), 0) == -1)
{
    cout << "没有这个目录" << endl;
    return -1;
}
cout << "目录:" << folder << endl;
linux:
#include <unistd.h>
int access(const char* _Filename, int _AccessMode)
该函数功能为确定文件或文件夹的访问权限,如果指定的访问权限有效,则函数返回0,否则返回-1
Filename可以是文件路径,也可以是文件夹路径,可以使用绝对路径或相对路径
_AccessMode表示要验证的文件访问权限,有可读、可写、可执行以及是否存在四种权限,当Filename表示文件夹时仅能查询文件夹是否存在
_AccessMode:
头文件unistd.h中有如下定义:
#define R_OK 4 /* Test for read permission. */
#define W_OK 2 /* Test for write permission. */
#define X_OK 1 /* Test for execute permission. */
#define F_OK 0 /* Test for existence. */
具体含义如下:
R_OK 只判断是否有读权限
W_OK 只判断是否有写权限
X_OK 判断是否有执行权限
F_OK 只判断是否存在
在宏定义里面分别对应:
00 只存在
02 写权限
04 读权限
06 读和写权限
参考文章: http://blog.csdn.net/u012005313/article/details/50688257

第四:获取文件名和文件路径

// string::find_last_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>         // std::size_t

void SplitFilename (const std::string& str)
{
  std::cout << "Splitting: " << str << '\n';
  std::size_t found = str.find_last_of("/\\");
  std::cout << " path: " << str.substr(0,found) << '\n';
  std::cout << " file: " << str.substr(found+1) << '\n';
}

int main ()
{
  std::string str1 ("/usr/bin/man");
  std::string str2 ("c:\\windows\\winhelp.exe");

  SplitFilename (str1);
  SplitFilename (str2);

  return 0;
}
Splitting: /usr/bin/man
 path: /usr/bin
 file: man
Splitting: c:\windows\winhelp.exe
 path: c:\windows
 file: winhelp.exe

第五: 写入文件
bool WriteLog(const char *log) {
		FILE *pf;
		char filepath[128] = { 0 };

		// 参数验证
		if (!strcmp(log, "") || !log) {
			printf("参数不能为空");
			return false;
		}

		// 创建文件目录
		if (_access("data", 0) != 0) {
			if (_mkdir("data") != 0) {
				Sleep(1000);
				if (_mkdir("data") != 0) {
					printf("目录创建失败\n");
					return 0;  // 目录创建失败
				}
			}
			printf("目录创建成功\n");
		}

		// 组合文件目录
		snprintf(filepath, 128, "%s\\%s", "data", "mylog.log");

		// 写入文件
		pf = fopen(filepath, "a+");
		if (!pf) return false;
		fwrite(log, strlen(log), sizeof(char), pf);
		fclose(pf);
		return true;
	}




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C/C++编程中,判断INI文件中某个节中键值对是否全部读取完毕可以通过以下步骤: 1. 打开INI文件:使用 fopen 函数打开INI文件,获取文件指针。 2. 定位到指定节:通过读取每一行的内容,并判断该行是否是目标节的开始行(以'['开头)来定位到指定节。 3. 读取键值对:在定位到目标节后,可以通过 fgets 函数逐行读取节的内容,判断读取的内容是否是键值对(包含'='字符),如果是,则进行处理。 4. 判断是否读取完毕:判断条件可以有多种方式,例如可以在循环过程中使用计数器记录读取的键值对数量,然后与目标节中的键值对总数进行对比,如果相等则表示读取完毕。 5. 关闭INI文件:使用 fclose 函数关闭打开的INI文件。 以下是一个简单的示例代码: ```c #include <stdio.h> #include <string.h> int main() { FILE *file; char line[256]; int count = 0; int targetCount = 0; int isInTargetSection = 0; // 打开INI文件 file = fopen("config.ini", "r"); if (file == NULL) { printf("无法打开INI文件\n"); return -1; } // 定位到指定节 while (fgets(line, sizeof(line), file) != NULL) { if (line[0] == '[' && strstr(line, "[TargetSection]") != NULL) { isInTargetSection = 1; } if (isInTargetSection && strchr(line, '=') != NULL) { targetCount++; } // 判断是否读取完毕 if (targetCount > 0 && count == targetCount) { break; // 读取完毕 } } // 关闭INI文件 fclose(file); // 输出结果 printf("目标节中的键值对总数:%d\n", targetCount); printf("已读取的键值对数量:%d\n", count); return 0; } ``` 注意:示例代码仅用于演示思路,请根据具体的需求和INI文件格式进行相应的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值