linuxc文件读取
没测试过,不过问题应该是fgetc这里
fgetc获取到第一个字符,比如第一行的'#'号,然后fgets获取到后面的字符,打印当然就没有第一个字符了,解决方式要么只用fgets,要么把fgetc获取的字符也打印出来
⑵ LINUX 下C语言从文件读取数据到数组的问题
文件中保存的数据有实型有整型,则在读取数据时,均按浮点数据读取就好了。
参考代码:
#include<stdio.h>
intmain()
{
FILE*fp;
inti,n;
doublea[100];
fp=fopen("data.txt","r");
if(!fp)
{
printf("openfileerror");
return-1;
}
i=0;
while(i<100)//控制不能超过数组的大小
{
if(fscanf(fp,"%lf",&a[i])!=1)//按浮点数读取数据
break;
i++;
}
n=i;
for(i=0;i<n;i++)//输出读到的结果
printf("%g ",a[i]);
fclose(fp);
return0;
}
⑶ Linux C语言怎么读取文件指定行内容
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编程读取文件中每行的数据
实话是,使用C如此操作是比较复杂的,步骤如下:
1、自我实现
首先需要将所有的文件读取到内存中,之后进行适当的处理,可以定义一个函数,函数的形参是四个三维数组,或者一个4*3的二维数组,之后的操作相当于矩阵的转矩3 * 4。
2、借助三方包
上面说了这其实就是一个矩阵的转矩,那么可以使用第三方的实现矩阵相关运算的库文件。
⑸ 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读取文件中特定格式的内容
用指针循环移动 判断是不是你对应的数据 16进制的 只能这么处理
⑻ linux/unix C读取文件中的数字
假设该文件是文本方式
1、每次读一行,用fgets
2、假设都是以空格分隔,用sscanf从刚才fgets读到的字符串中取出一段
3、对其中的字符用isdigital判断是否都为数字,如果是,就atoi转换成数字,写入另一个文件
这样做就不需要操作文件指针,基本就是使用解析字符串格式的方法就可以了
⑼ Linux C 缓冲文件和非缓冲文件读写方式用法
1.用write, read, open等系统调用编写分别实现如下功能的程序(要求进行必要的出错检查):
(1)创建一个文件testfile.txt,文件内容从键盘输入;
(2)将testfile.txt的内容显示在屏幕上,并将testfile.txt的内容复制到一个新的文件file2.txt中。
实验代码:
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main()
⑽ Linux系统下 C语言读取文件内容,并将指定内容或某个特殊字符开始的内容,存放到字符串
1、用fgets函数可以读取文件中某行的数据,某列数据就必须一个一个读入每行的第几个字符,再存入到一个字符串当中。2、例程: #include#includevoid main(){ char a[100],b[100],c[100]; int i=3,j=4,k=0; /