// TestCFile.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include <stdio.h>
#include <string.h> char* substr(const char*str,unsigned start, unsigned end);
char * getFirst(char str[]);
char* getValue(char str[]);
void writeAfter(char buffer[]);
void del(char id[]);
char* searchByID(char id[]); char *path = "/home/magc/software/test1.txt";
char *dest = "D:/123.txt";
char buffer[]; /*截取字符串*/
char* substr(const char*str,unsigned start, unsigned end)
{
unsigned n = end - start;
static char stbuf[];
strncpy(stbuf, str + start, n);
stbuf[n] = ;
return stbuf;
} char * getFirst(char str[])
{
int firstComma=strstr(str,",")-str;
if(firstComma!=-)//当str中存在,号的时候
{
return substr(str,,firstComma); }else
{
return "nothing was found";
} } /*得到命令中的值 如 add,xxx,x,xx,xx结果是 xxx,x,xx,xx*/
char* getValue(char str[])
{
int firstComma=strstr(str,",")-str;
if(firstComma!=-)//当str中存在,号的时候
{
return substr(str,firstComma+,strlen(str)-); }else
{
return "values not found";
}
} void writeAfter(char buffer[])
{
printf("writeAfter");
FILE *df = fopen(dest,"at");
if(df!=NULL)
{
printf("df is not null");
//gets(buffer);//--------buffer来自于控制台的输入
strcat(buffer,"\n");
fputs(buffer,df);
}
//fclose(df);
} /*根据ID来删除*/
/*实际上就是逐行遍历 将不是以此ID开头的都保存起来然后覆盖原文件*/
void del(char id[])
{
char firstWord[];
FILE *df = fopen(dest,"r");
//存放所有读取的信息的二维字符串数组
char lines[][];
int lineNo=; char line[];//一行信息
while(fgets(line,,df)!=NULL)
{
printf(line);//原本的文件信息中已经带有了换行符号 这里的line也是带有换行符号的//故这里不使用puts
strcpy(firstWord,getFirst(line));//得到第一个值 例如 本来是001,xxx,xxx,xxx 应该得到001 if(strcmp(firstWord,id)!=) //如果该行的id不是我们要找的id 那么 我们应该记住此行
{
strcpy(lines[lineNo++],line);
}else
{
//什么也不做
}
} //重新写入文件 覆盖写
FILE *wf=fopen(dest,"w");
for(int i=;i<lineNo;i++)
{
fputs(lines[i],wf); } fclose(df);
fclose(wf);
} /*根据ID来查找*/
/*输入 001 找到的结果是 001,xxx,xx,\n 找到的每条信息本身末尾带有\n*/
char* searchByID(char id[])
{
char firstWord[];
FILE *df = fopen(dest,"r"); char line[];//一行信息
while(fgets(line,,df)!=NULL)
{
//printf(line);//原本的文件信息中已经带有了换行符号 这里的line也是带有换行符号的//故这里不使用puts
strcpy(firstWord,getFirst(line));//得到第一个值 例如 本来是001,xxx,xxx,xxx 应该得到001 if(strcmp(firstWord,id)==) //如果该行的id是我们要找的id 找到了!
{
break;
}else
{
//什么也不做
}
}
fclose(df);
return line; } //int _tmain(int argc, _TCHAR* argv[])
//{
// //成功运行!
// //char id[]="003";
// //del(id);
//
// /*测试del*/
// //char str[]="add,xxxx,xxx,xxx";
// //printf(substr(str,0,3));//正确输出add
// //strcpy(str,"search,xxxx");
// //printf(getFirst(str));//正确输出search
// //strcpy(str,getFirst("001,xxx,xxx,xx"));
// //printf(str);//正确得到001
//
//
// /*测试查找*/
// char str[255];
// strcpy(str,searchByID("001"));
// printf(str);
//
// char c[255];
// gets(c);
//}

C 根据行来读取文件 字符串的截取的更多相关文章

  1. (转)Java按指定行数读取文件

    package test import java.io.File; import java.io.FileReader; import java.io.IOException; import java ...

  2. [Python] 跳过前几行快速读取文件内容:islice

    from itertools import islice start = 1 # 跳过第一行idx=0,从idx=1开始读取文件 with codecs.open('data.json', encod ...

  3. c++ 读取文件字符串 并且解析

    /* "/Users/macname/Desktop/aa-1.log" 链接:https://pan.baidu.com/s/1fKB5vXDe6bYOhoslc-kr7w  密 ...

  4. scala2.11读取文件

    1.读取行 要读取文件的所有行,可以调用scala.io.Source对象的getLines方法: import scala.io.Source val source = Source.fromFil ...

  5. shell总结:读取文件、参数、if、分割字符串、数组长度、空文件、变量赋值、多进程、按行切割文件、查看线程

    Reference: http://saiyaren.iteye.com/blog/1943207 1.     Shell  读取文件和写文件 for line in $(<top30000. ...

  6. C++/Php/Python/Shell 程序按行读取文件或者控制台

    写程序经常需要用到从文件或者标准输入中按行读取信息,这里汇总一下.方便使用 1. C++ 读取文件 #include<stdio.h> #include<string.h> i ...

  7. Java 读取文件到字符串

    Java的io操作比较复杂 package cn.outofmemory.util; import java.io.BufferedReader; import java.io.FileInputSt ...

  8. 文件操作ofstream,open,close,ifstream,fin,依照行来读取数据, fstream,iosin iosout,fio.seekg(),文件写入和文件读写,文件拷贝和文件

     1.ofstream,open,close 写入文件 #include<iostream> #include<fstream> using namespace std; ...

  9. head 命令 读取文件的前n行,默认查看文件的前十行

    head 命令 读取文件的前n行 默认查看文件的前十行 head /etc/passwd # 默认查看文件的前十行 /etc/passwd # 查看文件的前两行

随机推荐

  1. CSS:margin负数的使用

    给所有div加上边框=10px之后,再给所有div设置margin-left与margin-top;以及浮动(float:left) 因此时需要鼠标悬停效果:所以设置给div设置伪类:hover,然因 ...

  2. Android 创建目录

    //android 内部存储自定义目录写入文件 try{ File testDir = new File(this.getFilesDir().getAbsolutePath() + File.sep ...

  3. 创建oracle数据库的表空间、用户、目录、导入\导出文件等信息

    1.创建表空间 create tablespace ts_aw logging datafile 'd:\app\Administrator\product\tablespace\ts_aw.dbf' ...

  4. Excel Sheet Row Numbers

    Given the sequence S1 = {a,b,c,d,…,x,y,z,aa,ab,ac…. } and given that this sequence corresponds (term ...

  5. BZOJ 2762: [JLOI2011]不等式组( 平衡树 )

    对不等式变形..然后就是维护一些数, 随便找个数据结构都能写吧....用double感觉会有精度误差, 分类讨论把<改成<=了很久后弃疗了, 自己写了个分数体....然后速度就被完爆了.. ...

  6. Linux各个发行版本的介绍, 以及VirtualBox+CentOS的安装步骤

    Linux和Unix系统有哪些主要的发行版本 Unix: (非开源传统商业操作系统) IBM AIX, HP HP-UX, Sun Solaris,等 各家硬件厂商的发行版本, 往往是和自家的硬件设备 ...

  7. clip原理

    1.clip的概述: clip是修剪之意 clip有4个属性值:inherit auto rect(20px,40px,60px,0px) !important 其中有作用的仅rect这个属性值,着重 ...

  8. 虚拟机NAT模式主机ping不通虚拟机解决方案

    本篇没有抓包,只是简单一个实施.需要的童鞋可以拿走这个方法. 虚拟机与真机通信三种模式, 桥接模式,NAT 模式 ,HOST 模式. 桥接就是在真机的网络上模拟一个网卡,给虚拟机申请一个和真机在同一个 ...

  9. DAL – RDBMS 的分区

    编辑人员注释:本文章由AzureCAT 云与企业工程组的高级项目经理Shaun Tinline-Jones 和Chris Clayton 共同撰写. "云服务基础"应用程序也称作& ...

  10. [每日一题] 11gOCP 1z0-052 :2013-09-10 ABOUT ALERTS...............................................A50

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/11546561 正确答案:AD 使用服务器生成的警报系统,从Oracle10g版本开始,Or ...