[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;
	}




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值