// 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. S3C2440 之SPI

    概述: S3C2440有两个串行外设SPI接口,SPI具有全双工通信 SPI方框图 SPI操作: 通过使用SPI接口,S3C2440可以与外部器件同时发送.接收8位数据.当SPI接口为主机时,可以通过 ...

  2. Android 教你打造炫酷的ViewPagerIndicator 不仅仅是高仿MIUI

    1.概述 哈,今天给大家带来一个ViewPagerIndicator的制作,相信大家在做tabIndicator的时候,大多数人都用过 TabPageIndicator,并且很多知名APP都使用过这个 ...

  3. Struts2复习(四)防止表单反复提交

    1.採取请求转发的方式完毕表单内容的加入会造成内容的反复插入. 2.採取重定向的方式实现数据的加入不会导致数据的反复插入. 3.防止表单反复提交的两种方式 1)  通过重定向  2)  通过Sessi ...

  4. servlet生成随机验证码

    package com.cgyue; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import jav ...

  5. SQL语言整理归纳

  6. poj 1724 ROADS 最短路

    题目链接 n个节点, m条边, 一开始有K这么多的钱, 每条边有len, cost两个属性, 求1到n的最短距离, 花费要小于k. dis数组开成二维的, dis[u][cost]表示到达u花费为co ...

  7. C语言2

    函数是C语言的基本单位,类是java,c#,c++的基本单位 int abs(int x); double fabs(double x);   按变量的存储方式:静态变量.自动变量.寄存器变量 指针就 ...

  8. Apache配置支持include

    Apache配置支持include 什么是SSI? SSI是英文Server Side Includes的缩写,翻译成中文就是服务器端包含的意思.从技术角度上说,SSI就是HTML文件中,可以通过注释 ...

  9. PCB板常用知识简介——沉金板VS镀金板

    一.PCB板表面处理:  抗氧化,喷锡,无铅喷锡,沉金,沉锡,沉银,镀硬金,全板镀金,金手指,镍钯金 OSP: 成本较底,可焊性好,存储条件苛刻,时间短,环保工艺.焊接好 .平整 . 喷锡:喷锡板一般 ...

  10. ilmerge合并多个组件

    原文 http://www.cnblogs.com/margiex/archive/2008/06/24/302329.html 年初的一篇文章中提到过一下: http://margiex.cnblo ...