文件操作代码练习
2015-05-17 21:23
344 查看
main.cpp
// oj_file_operation.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
extern int number_operate(char *inFile, char *outFile);
int main(int argc, char* argv[])
{
printf("Hello World!\n");
// number_operate("e:\tmp\in_data.txt", "e:\tmp\out_data.txt");
number_operate("e://tmp/in_data.txt", "e://tmp/out_data.txt");
return 0;
}
file_read_write.cpp
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *get_digit_start(char *p_in)
{
char *p_start = p_in;
int i = 0;
//while 循环写法
//入参检查
for (i = 0; i < strlen(p_in); i++) //"\n"
{
if (p_in[i] == ' ')
{
p_start++;
continue;
}
if (p_in[i] < '0' || p_in[i] > '9')
{
return NULL;
}
if (p_in[i] >= '0' && p_in[i] <= '9')
{
break;
}
}
return p_start;
}
char *get_digit_end(char *p_in)
{
char *p_end = p_in;
int len = 0;
int i = 0;
len = strlen(p_in);
p_end = &p_in[len - 2]; //"\n"
//相等条件
while (p_end != p_in)
{
if (*p_end < '0' || *p_end > '9')
{
return NULL;
}
if (*p_end == ' ')
{
p_end--;
continue;
}
if (*p_end >= '0' && *p_end <= '9')
{
break;
}
}
return p_end;
}
/*
int is_digit_chars(char *pin)
{
}
*/
int number_operate(char *inFile, char *outFile)
{
FILE *fp = NULL;
FILE *fp_out = NULL;
char line_chars[65];
char out_chars[65];
char *p_digit_start = NULL;
char *p_digit_end = NULL;
char *p_tmp = NULL;
int i = 0;
int is_digit_chars = 1;
if (NULL == (fp = fopen(inFile, "r+")))
{
printf(" inFile file opened error");
perror("perror");
return -1;
}
if (NULL == (fp_out = fopen(outFile, "w+")))
{
printf("outFile file opened error");
perror("perror");
return -1;
}
while (! feof(fp))
{
fgets(line_chars, sizeof(line_chars), fp); //末尾有换行符 \n
printf("%s\n", line_chars);
p_digit_start = line_chars;
if(NULL == ((p_digit_start = get_digit_start(line_chars))))
{
continue;
}
if(NULL == ((p_digit_end = get_digit_end(line_chars))))
{
continue;
}
p_tmp = p_digit_start;
while (p_tmp != p_digit_end)
{
if (*p_tmp < '0' || *p_tmp > '9')
{
is_digit_chars = 0;
break;
}
p_tmp++;
}
if (1 != is_digit_chars)
{
is_digit_chars = 1;
continue;
}
//should add /n
strncpy(out_chars, p_digit_start, p_digit_end - p_digit_start + 1);
out_chars[p_digit_end - p_digit_start + 1] = '\0';
//out_chars[p_digit_end - p_digit_start + 2] = '\r';
//out_chars[p_digit_end - p_digit_start + 3] = '\n';
printf("out file %s \n", out_chars);
fputs(out_chars, fp_out);
fputs("\n", fp_out); //换行
}
return 1;
}
// oj_file_operation.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
extern int number_operate(char *inFile, char *outFile);
int main(int argc, char* argv[])
{
printf("Hello World!\n");
// number_operate("e:\tmp\in_data.txt", "e:\tmp\out_data.txt");
number_operate("e://tmp/in_data.txt", "e://tmp/out_data.txt");
return 0;
}
file_read_write.cpp
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *get_digit_start(char *p_in)
{
char *p_start = p_in;
int i = 0;
//while 循环写法
//入参检查
for (i = 0; i < strlen(p_in); i++) //"\n"
{
if (p_in[i] == ' ')
{
p_start++;
continue;
}
if (p_in[i] < '0' || p_in[i] > '9')
{
return NULL;
}
if (p_in[i] >= '0' && p_in[i] <= '9')
{
break;
}
}
return p_start;
}
char *get_digit_end(char *p_in)
{
char *p_end = p_in;
int len = 0;
int i = 0;
len = strlen(p_in);
p_end = &p_in[len - 2]; //"\n"
//相等条件
while (p_end != p_in)
{
if (*p_end < '0' || *p_end > '9')
{
return NULL;
}
if (*p_end == ' ')
{
p_end--;
continue;
}
if (*p_end >= '0' && *p_end <= '9')
{
break;
}
}
return p_end;
}
/*
int is_digit_chars(char *pin)
{
}
*/
int number_operate(char *inFile, char *outFile)
{
FILE *fp = NULL;
FILE *fp_out = NULL;
char line_chars[65];
char out_chars[65];
char *p_digit_start = NULL;
char *p_digit_end = NULL;
char *p_tmp = NULL;
int i = 0;
int is_digit_chars = 1;
if (NULL == (fp = fopen(inFile, "r+")))
{
printf(" inFile file opened error");
perror("perror");
return -1;
}
if (NULL == (fp_out = fopen(outFile, "w+")))
{
printf("outFile file opened error");
perror("perror");
return -1;
}
while (! feof(fp))
{
fgets(line_chars, sizeof(line_chars), fp); //末尾有换行符 \n
printf("%s\n", line_chars);
p_digit_start = line_chars;
if(NULL == ((p_digit_start = get_digit_start(line_chars))))
{
continue;
}
if(NULL == ((p_digit_end = get_digit_end(line_chars))))
{
continue;
}
p_tmp = p_digit_start;
while (p_tmp != p_digit_end)
{
if (*p_tmp < '0' || *p_tmp > '9')
{
is_digit_chars = 0;
break;
}
p_tmp++;
}
if (1 != is_digit_chars)
{
is_digit_chars = 1;
continue;
}
//should add /n
strncpy(out_chars, p_digit_start, p_digit_end - p_digit_start + 1);
out_chars[p_digit_end - p_digit_start + 1] = '\0';
//out_chars[p_digit_end - p_digit_start + 2] = '\r';
//out_chars[p_digit_end - p_digit_start + 3] = '\n';
printf("out file %s \n", out_chars);
fputs(out_chars, fp_out);
fputs("\n", fp_out); //换行
}
return 1;
}
相关文章推荐
- Python练习代码 -- 字符串和正则表达式, 文件文件夹操作
- java文件操作练习代码 读取某个盘符下的文件
- Ubuntu下通过CMake文件编译CUDA+OpenCV代码操作步骤
- Linux下配置文件读取操作流程及其C代码实现
- Linux 下几个文件操作命令的代码实现
- Hadoop—HDFS读写文件操作---练习4
- VM虚拟机上安装Eclipse和Hadoop上文件命令操作的java代码实现
- python读取读取配置文件信息操作代码
- Java 代码完成删除文件、文件夹操作
- 一个WinForm记事本程序(包含主/下拉/弹出菜单/打开文件/保存文件/打印/页面设置/字体/颜色对话框/剪切版操作等等控件用法以及记事本菜单事件/按键事件的具体代码)
- 利用python脚本抓取AC的代码[爬虫+HTMLParser+handle_entityref+正则表达式+模拟登陆+文件操作]
- 黑马程序员_File类代码练习(列出文件目录)
- 文件操作简易代码[C语言]
- 统一Windows Azure和一般web应用之间的文件操作代码(转+译)
- verilog文件操作代码(转)
- Hadoop HDFS文件操作的Java代码
- Linux 下几个文件操作命令的代码实现,myfind find 查找命令 实现
- .Net 最详细的文件操作移动复制删除代码
- java代码操作zip文件--写入zip文件
- 代码审计之文件操作