当前位置:首页 » 编程语言 » c语言读取文件read

c语言读取文件read

发布时间: 2023-08-08 07:37:17

1. c语言中如何把一个文件读入内存

用C语言实现将一个文件读入内存方法:

#include <stdio.h>
#include <stdlib.h>
int filelength(FILE *fp);
char *readfile(char *path);
int main(void)
{
FILE *fp;
char *string;
string=readfile("c:/c.c");
printf("读入完毕\n按任意键释放内存资源\n");
//printf("%s\n",string);
system("pause");
return 0;

}
char *readfile(char *path)
{
FILE *fp;
int length;
char *ch;
if((fp=fopen(path,"r"))==NULL)
{
printf("open file %s error.\n",path);
exit(0);
}
length=filelength(fp);
ch=(char *)malloc(length);
fread(ch,length,1,fp);
*(ch+length-1)='\0';
return ch;
}
int filelength(FILE *fp)
{
int num;
fseek(fp,0,SEEK_END);
num=ftell(fp);
fseek(fp,0,SEEK_SET);
return num;
}

2. linux下c语言编程read()函数的问题

返回-1的时候,要根据错误码来判断原因,请看下面的函数说明:

表头文件 #include<unistd.h>
定义函数 ssize_t read(int fd,void * buf ,size_t count);
函数说明 read()会把参数fd 所指的文件传送count个字节到buf指针所指的内存中。若参数count为0,则read()不会有作用并返回0。返回值为实际读取到的字节数,如果返回0,表示已到达文件尾或是无可读取的数据,此外文件读写位置会随读取到的字节移动。
附加说明 如果顺利read()会返回实际读到的字节数,最好能将返回值与参数count 作比较,若返回的字节数比要求读取的字节数少,则有可能读到了文件尾、从管道(pipe)或终端机读取,或者是read()被信号中断了读取动作。当有错误发生时则返回-1,错误代码存入errno中,而文件读写位置则无法预期。
错误代码 EINTR 此调用被信号所中断。 EAGAIN 当使用不可阻断I/O 时(O_NONBLOCK),若无数据可读取则返回此值。 EBADF 参数fd 非有效的文件描述词,或该文件已关闭。

3. 关于C语言中文本文件的逐行读取的实现

若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。

以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。

C语言中文本文件的逐行读取的实现的代码如下:

#include<stdio.h>

main()

{

FILE * fp;

fp=fopen(“noexist”,”a+”);

if(fp= =NULL) return;

fclose(fp);

}

(3)c语言读取文件read扩展阅读

1、如果输入文本每行中没有空格,则line在输入文本中按换行符分隔符循环取值。

2、如果输入文本中包括空格或制表符,则不是换行读取,line在输入文本中按空格分隔符或制表符或换行符特环取值。

3、可以通过把IFS设置为换行符来达到逐行读取的功能。

4. 关于C语言中文本文件的逐行读取的实现

#include
<stdio.h>
int
ReadData(void)
{
unsigned
int
rc,
i,
get;
/*get为读取一个字节*/
FILE
*fp;
fp
=
fopen("helloworld.txt",
"r+");
if
(fp
==
NULL)
{
return
-1;
/*打开文件失败返回-1*/
}
rc=0;
i=0;
while
((get
=
fgetc(fp))
!=
EOF)
{
if((char)get
==
'\n')
/*记录换行符*/
{
rc
=
ftell(fp);
i++;
}
rc++;
}
return
i;
}
int
main()
{
int
row;
row
=
ReadData();
printf("Row
=
%d\n",
row);
/*打印文件行数*/
return
0;
}

5. C语言read函数

read内部是调_read, _read的返回值在msdn中有这样的描述
_read returns the number of bytes read, which might be less than count if there are fewer than count bytes left in the file or if the file was opened in text mode, in which case each carriage return–line feed (CR-LF) pair is replaced with a single linefeed character. Only the single linefeed character is counted in the return value. The replacement does not affect the file pointer.

注意这一段: in which case each carriage return–line feed (CR-LF) pair is replaced with a single linefeed character

就是说如果用text模式打开的话, 文件换行时可能在文本中有2个字符----换行和缩进(CR-LF), 而在return的时候系统是把它作为1个回车符号('\n')所返回的. 所以会导致这个情况

热点内容
安卓如何设置桌面返回键 发布:2025-02-06 13:58:15 浏览:48
bi可视化php 发布:2025-02-06 13:50:15 浏览:931
shell写脚本文件 发布:2025-02-06 13:47:32 浏览:231
健身器材脚本 发布:2025-02-06 13:46:36 浏览:856
怎么从手机里卸载存储卡 发布:2025-02-06 13:35:04 浏览:644
诛仙青云志2ftp 发布:2025-02-06 13:34:48 浏览:34
mill91编程 发布:2025-02-06 13:10:27 浏览:294
华为平板怎么储存服务器文件 发布:2025-02-06 12:49:21 浏览:482
php查询结果数组 发布:2025-02-06 12:31:05 浏览:717
怎样把照片压缩打包 发布:2025-02-06 12:15:19 浏览:498