如何实现java 取出特定字符文件中特定的行内容?

C语言实现修改文本文件中特定行的实现代码
字体:[ ] 类型:转载 时间:
最近由于项目需要实现修改文件的功能,所以,博主认真查阅了一些资料,但是,很遗憾,并没有太多的收获
好的,首先我先叙述下功能要求:其实很简单,就是Shell中sed命令的C语言实现,实现定位到所需要的字段的那一行,之后修改成需要的内容。但是由于C语言是面向过程的语言,需要顺序执行的特点,所以,实现中遇到了很多麻烦,在这里博主将实现的过程描述如下,以便大家参考。
问题描述:
文本内容: 代码如下:wireless.1.authmode=1pression=0wireless.1.current_ap=ssid12wireless.1.current_state=1wireless.1.devname=ath0wireless.1.enable_slave1_status=disabledwireless.1.enable_slave2_status=disabledwireless.1.enable_slave3_status=disabled
我需要做的就是修改第四行的内容,使之变为:
代码如下:wireless.1.current_state=0
问题看似简单,实现过程颇费周折。。。
这里我给出实现的代码,注释已经加在代码中:
代码如下:/*&* Author:DLUTBruceZhang&* Date: &*/#include&stdio.h&#include&stdlib.h&#include&string.h&#include&unistd.h&int main(){&/*& *linebuffer:读取文件中的一行存入的缓冲& *buffer1:一行中第一个字段的存入缓冲& *buffer2:一行中第二个字段的存入缓冲& */&char linebuffer[512] = {0};&char buffer1[512] = {0};&char buffer2[512] = {0};&int line_len = 0;&int len = 0;&&/*& * cc.cfg是文件名,r+代表可以读写文件& */&FILE *fp = fopen("cc.cfg", "r+");&if(fp == NULL)&{&&printf("open error");&&return -1;&}&while(fgets(linebuffer, 512, fp))&{&&line_len = strlen(linebuffer);&&len += line_&&/*&& * buffer1=wireless.1.current_state&& * buffer2=1&& */&&sscanf(linebuffer, "%[^=]=%[^=]", buffer1,buffer2);&&if(!strcmp("wireless.1.current_state", buffer1))&&{&&&/*&&& * 由于已经找到所需要写的位置,所以需要写位置的“头”&&& */&&&len -= strlen(linebuffer);&&&/*&&& * 实现文件位置的偏移,为写文件做准备&&& */&&&res = fseek(fp, len, SEEK_SET);&&&if(res & 0)&&&{&&&&perror("fseek");&&&&return -1;&&&}&&&strcpy(buffer2, "=0");&&&/*strcat(buffer1, "=");*/&&&strcat(buffer1, buffer2);&&&printf("%d",strlen(buffer1));&&&/*&&& * 写文件,存入所需的内容&&&&&& */&&&fprintf(fp, "%s", buffer1);&&&fclose(fp);&&&&&}&}return 0;}
保存文件名为:my_sed.c
运行效果如下:
我们再来看文件中的内容已经变为:
代码如下: wireless.1.authmode=1 pression=0 wireless.1.current_ap=ssid12 wireless.1.current_state=0 wireless.1.enable_slave1_status=disabled wireless.1.enable_slave2_status=disabled wireless.1.enable_slave3_status=disabled
实现原理: 这里实现的效率比较高,因为不是把整个文件的内容装入缓冲区中,而是一行一行的读取,知道匹配为止,之后利用写文件的特性,直接覆盖所写内容即可,由此完成所需功能
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具C语言读取文件中某行内容赋值给整型变量
C语言读取文件中某行内容赋值给整型变量
假设文件内容如下所示:
在C语言中用什么函数实现一次读取一行,并将文件中该行的数值赋给相应的整型变量。
我写个例子让你看看,你就明白了。
假设有文件:1.txt
22.468 1 50
33.411 12 3
77.639 29 3
44.811 20 18
现在读取文件,并用空格分开的数据分别存放到临时变量并输出。
int main()
char filename[] = &1.txt&;
if((fp = fopen(filename,&r&)) == NULL)
printf(&error!&);
return -1;
while (!feof(fp))
float fData = 0;
int iData1 = 0, iData2 = 0;
fscanf(fp, &%f&, &fData);
fscanf(fp, &%d&, &iData1);
fscanf(fp, &%d&, &iData2);
printf(&%f %d %d\n&, fData, iData1, iData2);
//这里可以做其他操作,如把数据保存到数组中以便以后使用
fclose(fp);
请遵守网上公德,勿发布广告信息
相关问答:
用fread 函数该日志已删除> lovedreamland的博客详情
Linux系统下,不像windows图形化文件来操作文件,在命令行下如何查看指定文件的某行到指定行之间的命令如何来写呢?今天我们将利用head命令和tail命令来实现指定行的查看。使用head命令和tail命令显示指定的行:
-n, --lines=[-]K
& & & & & & & print the first K lines instead of the first 10; with &the &leading
& & & & & & & ‘-’, print all but the last K lines of each file
-n, --lines=K
& & & & & & & output &the &last &K lines, instead of the last 10; or use -n +K to
& & & & & & & output lines starting with the Kth
[root etc]# pwd
[root etc]# cat yum.conf
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
installonly_limit=3
//此处是一空行
# &This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
# &It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m
//此处是一空行
# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d
[root etc]# cat yum.conf |wc -l
yum.conf文件共有23行
[root etc]# head yum.conf
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
installonly_limit=3
显示前10行,这个是默认值
[root etc]# head -n 2 yum.conf
cachedir=/var/cache/yum/$basearch/$releasever
[root@localhost etc]# head -n -20 yum.conf
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
显示除最后20行以外的所有行
[root@localhost etc]# tail yum.conf
# download the new metadata and "pay" for it by yum not having correct
# information.
# &It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m
//此处是一空行
# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d
显示最后10行,这是默认值
[root@localhost etc]# tail -n 2 yum.conf
# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d
显示最后2行
[root@localhost etc]# tail -n +21 yum.conf
//此处是一空行
# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d
从第21行开始显示后面的所有行,即第21行到第23行
显示第11行到第20行(总共23行):
从第11行开始显示,但不包括最后3行
[huage@localhost etc]$ head -n -3 yum.conf |tail -n +11
显示前20行,但从第11行开始
[huage@localhost etc]$ head -n 20 yum.conf |tail -n +11
显示除最后3行以外的所有行,但只显示最后10行
[huage@localhost etc]$ head -n -3 yum.conf |tail -n 10
显示前20行中的后10行
[huage@localhost etc]$ head -n 20 yum.conf |tail -n 10
从第11行开始显示,但只显示前10行
[huage@localhost etc]$ tail -n +11 yum.conf |head -n 10
从第11行开始显示,但不包括最后3行
[huage@localhost etc]$ tail -n +11 yum.conf |head -n -3
显示最后13行中的前10行
[huage@localhost etc]$ tail -n 13 yum.conf |head -n 10
显示最后13行中除末尾的3行以外的前10行
[huage@localhost etc]$ tail -n 13 yum.conf |head -n -3
人打赏支持
码字总数 16548
支付宝支付
微信扫码支付
打赏金额: ¥
已支付成功
打赏金额: ¥
& 开源中国(OSChina.NET) |
开源中国社区(OSChina.net)是工信部
指定的官方社区

我要回帖

更多关于 r语言取出特定数据列 的文章

 

随机推荐