// 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. loading android

    drawal/loading.xml <?xml version="1.0" encoding="utf-8"?><animated-rota ...

  2. JavaScript之面向对象的概念,对象属性和对象属性的特性简介

    一.大家都知道,面向对象语言有一个标志,那就是他们都有类的概念,通过类我们可以创建任意多个具有相同属性和方法的对象.但ECMAScript(指定JavaScript标准的机构,也就是说JavaScri ...

  3. IdeasToComeTrue

    灵感这玩意,果真是有的吧.不考虑什么架构和盈利模式,就只是想到的有趣,随便写写,以飨流年. 我的头脑风暴:爱玩儿aiWaner 2015/08/22 换书: 每个人可能有很多闲置图书,自己看完了觉得好 ...

  4. 小鱼提问2 属性访问器中get,set再用public修饰行吗,private呢?

    /// <summary> /// 是否有一个用户正在连接服务器中 /// </summary> public bool IsConnectting { get { retur ...

  5. (总结)Nginx/LVS/HAProxy负载均衡软件的优缺点详解

    PS:Nginx/LVS/HAProxy是目前使用最广泛的三种负载均衡软件,本人都在多个项目中实施过,参考了一些资料,结合自己的一些使用经验,总结一下. 一般对负载均衡的使用是随着网站规模的提升根据不 ...

  6. 初识C(1)----与C基本无关的开篇

    1.啥叫编程 编程乃编写程序的简称,所以要想知道啥叫编程,首先要清楚什么是程序(Program). 普及一点计算机小知识:从根本上说,计算机是由数字电路组成的运算机器,处理的数字也仅限于0和1组成的数 ...

  7. Angular ng-repeat 对象和数组遍历

    直接上代码 <!DOCTYPE html> <html> <head> <meta name="description" content= ...

  8. liunx使用技巧

    1.挂载与卸载U盘 新建一个目录:mkdir /mnt/usb; Fdisk –l |less  查看添加之后的设备名,设备文件系统格式 加载U盘设备: mount –t vfat /mnt/usb ...

  9. Mysql 忘密码 + Phpadmin 修改密码无法登陆

    mysql有时候忘记密码了怎么办?我给出案例和说明!一下就解决了! Windows下的实际操作如下 1.关闭正在运行的MySQL. 2.打开DOS窗口,转到mysql\bin目录. 3.输入mysql ...

  10. Html 小插件3

    搜狗搜索框代码 <script>function verifyquery(form){if(form.sogou_drop.value==2){form.insite.value='';} ...