当前位置:首页 » 操作系统 » linuxc读写文件

linuxc读写文件

发布时间: 2022-06-27 18:25:27

c语言如何读写 linux文本文件

Linux下C语言的文件(fputc,fgetc,fwrite,fread对文件读写操作)

//

fputc 向文件写入字符

#include <stdio.h>

#include <stdlib.h>

main()

{

FILE *fp;

char ch;

if((fp=fopen("test.txt","w"))==NULL)

{

printf("不能打开文件 ");

exit(0);

}

while ((ch=getchar())!=' ')

fputc( ch, fp );

fclose(fp);

}

-------------

小提示:

fp=fopen("test.txt","w") ,把"w"改为 "a" 可以创建文件并且追加写入内容

exit(0); 需要包含 stdlib.h 头文件,才能使用

//

fgetc 读取字符

#include <stdio.h>

#include <stdlib.h>

main( int argc, char *argv[] )

{

char ch;

FILE *fp;

int i;

if((fp=fopen(argv[1],"r"))==NULL)

{

printf("不能打开文件 ");

exit(0);

}

while ((ch=fgetc(fp))!=EOF)

putchar(ch);

fclose(fp);

}

文件结尾,通过判断 EOF

//

fwrite 的使用

使数组或结构体等类型可以进行一次性读写

#include <stdio.h>

#include <stdlib.h>

main()

{

FILE *fp1;

int i;

struct student{

char name[10];

int age;

float score[2];

char addr[15];

}stu;

if((fp1=fopen("test.txt","wb"))==NULL)

{

printf("不能打开文件");

exit(0);

}

printf("请输入信息,姓名 年龄 分数1 分数2 地址: ");

for( i=0;i<2;i++)

{

scanf("%s %d %f %f %s",stu.name,&stu.age,&stu.score[0],&stu.score[1], stu.addr);

fwrite(&stu,sizeof(stu),1,fp1);

}

fclose(fp1);

}

//

fread 的使用

#include <stdio.h>

#include <stdlib.h>

main()

{

FILE *fp1;

int i;

struct student{

char name[10];

int age;

float score[2];

char addr[15];

}stu;

if((fp1=fopen("test.txt","rb"))==NULL)

{

printf("不能打开文件");

exit(0);

}

printf("读取文件的内容如下: ");

for (i=0;i<2;i++)

{

fread(&stu,sizeof(stu),1,fp1);

printf("%s %d %7.2f %7.2f %s ",stu.name,stu.age,stu.score[0],stu.score[1],stu.addr);

}

fclose(fp1);

}

//

fprintf , fscanf, putw , getw , rewind , fseek 函数

这些函数的话我就不演示了 ,

这些函数基本都一对来使用,例如 fputc 和 fgetc 一起来用.

❷ linux系统如何读写属性为c的文件

Linux把外部设备也当成文件来管理,这是继承了Unix一切皆文件的设计思想。
/dev目录下的文件皆外部设备,所以你必须有相应的内核模块来驱动相应的设备,否则此设备无法读写。
你是在测试COM口么?COM口有没有连接测试设备?没有的话,就算有相应的内核模块也是白搭的,就象光有电灯开关,没安装灯泡,你怎么按开关都白搭。

❸ linux下如何用C程序读写本地文件

是一样的。如果是同目录则直接写文件名,如果是不同的目录,可以写明路径。

如:
读同目录文件local.txt
fopen("local.txt","r");

读不同目录文件 /home/yourname/otherdir/other.txt
fopen("/home/yourname/otherdir/other.txt","r");

你可以使用pwd命令来获得文件路径

❹ 我要用c编写程序读写磁盘,在linux中磁盘为文件,怎样用c语言获取磁盘的文件描述符,然后对磁盘进行读写

linux下面的概念是一切皆文件。所以没有像c盘d盘这样的东西,有的只是各种各样的文件夹和文件。要读一个linux下面的文件很简单,命令pwd可以得到当前路径,然后路径接上你打开的文件名就可以知道这个文件的详细路径了。直接open就可以了。

❺ 关于linux C的文件读写

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

#define MAX 1024

void lower1(char *p)
{
int i,len;
len = strlen(p);
for(i=0;i<len;i++)
if(p[i] >= 'A' && p[i] <= 'Z')
p[i] += 32;
}

int main(void)
{
FILE *fp,*fpw;
char *p;
char buf[MAX],buf1[MAX]="GAME OVER";
int n,m;
fp = fopen("txt","rw");
if(fp == NULL)
{
perror("Fail to open");
exit(1);
}

while((n = fread(buf,sizeof(char),MAX-1,fp)) > 0)
{
buf[n]='\0';
lower1(buf);
printf("%s",buf);
printf("%d",n);
}
rewind(fp);
while((n = fread(buf,sizeof(char),MAX-1,fp)) > 0) //你这里什么意思?你这里有问题
{
fputs(buf,fp);
}

if(n < 0){
perror("Fail to read");
exit(1);
}

fclose(fp);
return 0;
}

顺便,看样子你也知道,读写无法同时进行的,所以,你读万,一定要rewind一下

❻ Linux c 写文件

这个问题其实是很复杂的。
C语言的字符集包含两个,一个是源码所处在的环境的字符集,一个是运行时环境的字符集。
我光是知道Java如何指定,但却不知道C语言程序输出的是什么,除非蒙上,否则这是行不通的。试试使用gcc中的-finput-charset和-fexec-charset开关来指定字符集,确保输入输出一致

❼ linux系统下,文件存储与数据读写问题(C语言)。

int writeFile(char *path,char *buf)
{
FILE *file;
file = fopen(path,"a");
if (file == NULL)
{
return -1;
}

fwrite(buf,strlen(buf),1,file);

if (file)
fclose(file);
return 0;
}

❽ c语言如何读写 linux文本文件

你说的应该是FILE IO吧,建议自己学习下
http://wenku..com/view/6b921360ddccda38376bafb4.html
http://blog.csdn.net/hack_47/archive/2008/12/19/3556211.aspx
你直接搜索Linux file io就可以了
另外,Linux下有一些用于文本操作的工具,你不妨用脚本实现你的操作
祝好运

❾ 用linux下的c语言读取txt文件中的列数据

1.用fgets函数可以读取文件中某行的数据,某列数据就必须一个一个读入每行的第几个字符,再存入到一个字符串当中。

2.例程:

#include<stdio.h>
#include<string.h>
voidmain()
{
chara[100],b[100],c[100];
inti=3,j=4,k=0;//第三行,第四列
FILE*fp=fopen("data.txt","r");
while(fgets(c,100,fp)){//读入每行数据
i--;
if(i==0)strcpy(a,c);//读到第三行数据
b[k++]=c[j-1];//把每行的那列字符拷到b中
}
b[k]=0;
printf("第%d行数据:%s ",i,a);
printf("第%d列数据:%s ",j,b);
fclose(fp);
}

❿ linux下c语言 读取文件内容

没测试过,不过问题应该是fgetc这里
fgetc获取到第一个字符,比如第一行的'#'号,然后fgets获取到后面的字符,打印当然就没有第一个字符了,解决方式要么只用fgets,要么把fgetc获取的字符也打印出来

热点内容
roblox跑酷脚本怎么做 发布:2024-05-05 03:57:35 浏览:701
捷径清理缓存 发布:2024-05-05 03:57:35 浏览:478
ftputility哪里下载 发布:2024-05-05 03:47:13 浏览:1000
雷凌运动版如何连接安卓手机导航 发布:2024-05-05 03:42:48 浏览:267
自动鬼使黑脚本 发布:2024-05-05 03:10:49 浏览:880
游戏脚本编程书籍推荐 发布:2024-05-05 02:59:13 浏览:72
编译器书籍推荐 发布:2024-05-05 02:57:02 浏览:56
电池存储温度 发布:2024-05-05 02:53:07 浏览:207
安卓在美国怎么下载 发布:2024-05-05 02:31:06 浏览:925
黑莓存储空间 发布:2024-05-05 02:19:50 浏览:275