// 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. 翻转View

    翻转View by 伍雪颖 CGContextRef context = UIGraphicsGetCurrentContext(); [UIView beginAnimations:nil cont ...

  2. s2sh遇到的问题

    一:ids for this class must be manually assigned before calling save() "类名.hbm.xml"映射文件中< ...

  3. Winform中上传、下载文件选择打开文件的位置

    打开将要上传的文件 var fileName="";OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Mul ...

  4. vs vsvim viemu vax 备忘

    使用gt和gT往返标签 gd:到达光标所在处函数或者变量的定义处. *:读取光标处的字符串,并且移动光标到它再次出现的地方. #:和上面的类似,但是是往反方向寻找. /text:从当前光标处开始搜索字 ...

  5. 有关std::map和std::vector的使用

    先说map吧. 最需要注意的就是:用下标访问map中的元素时,与使用下标访问vector的行为截然不同! 用下标访问不存在的元素时,将导致在map容器中添加一个新的元素,它的键即为该下标! 然而很多时 ...

  6. USACO Section 3.3 Camlot(BFS)

    BFS.先算出棋盘上每个点到各个点knight需要的步数:然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个 ...

  7. leetcode Contains Duplicate python

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  8. android-数据持久化

    动态文件 String FILE_NAME = "tempfile.tmp"; try { //读 FileInputStream fis = openFileInput(FILE ...

  9. C#对象赋值出现的诡异问题,或许你也遇到过,有待你的解决

    前言:今天在代码中,又出现了这个问题,就是对象赋值给一个新的对象时,然后更改新对象中的属性,就会把老对象的值也更改,以前也遇到这个问题,只是没有深究,今天刚好又遇到了此问题,我决定写下来,和大家一起分 ...

  10. JAVA并发,后台线程

    package com.xt.thinks21_2; import java.util.concurrent.TimeUnit; /** * 后台线程测试 * * @author Administra ...