使用C语言实现以下功能

zcp0317 2011-07-26 09:21:02
格式化文件的字段提取
输入:竖线分割的文件,格式:

字段1|字段2|流水号1(字段3)|字段4|···|字段26
字段1|字段2|流水号2(字段3)|字段4|···|字段26
·······

输出:冒号分割的文件,文件名随便,内容为输入文件的字段1,字段2,和字段7
格式:
字段1:字段2:字段3(输入文件的字段8)
字段1:字段2:字段3(输入文件的字段8)


我的思路是:
1.用fopen打开文件,
2.用fgets实现逐行读取,具体是,找到每行中字符‘|’的位置,然后两个‘|’之间的字符串写入到新的文件中,不知道这样可不可以?还有具体怎么实现啊???
...全文
242 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
dtmjg 2011-07-27
  • 打赏
  • 举报
回复
你先实现下,遇问题了再问
赵4老师 2011-07-27
  • 打赏
  • 举报
回复
sscanf
Read formatted data from a string.

int sscanf( const char *buffer, const char *format [, argument ] ... );


Routine Required Header Compatibility
sscanf <stdio.h> ANSI, Win 95, Win NT


Return Value

This function returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end of the string is reached before the first conversion.

赵4老师 2011-07-27
  • 打赏
  • 举报
回复
Format Specification Fields: scanf and wscanf Functions
A format specification has the following form:

%
  • [width] [{h | l | I64 | L}]type


    To read strings not delimited by space characters, a set of characters in brackets ([ ]) can be substituted for the s (string) type character. The corresponding input field is read up to the first character that does not appear in the bracketed character set. If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set.

    Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]. This is a common scanf function extension, but note that the ANSI standard does not require it.

    An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.

zcp0317 2011-07-27
  • 打赏
  • 举报
回复
if (4==sscanf(ln,"%[^|]|%[^|]|%[^|]|%*[^|]|%*[^|]|%*[^|]|%*[^|]|%[^|]|",f1,f2,f3,f8)) {
fprintf(fo,"%s:%s:%s(%s)\n",f1,f2,f3,f8);
} else break;


这一段是什么意思啊???
赵4老师 2011-07-27
  • 打赏
  • 举报
回复
#include <stdio.h>
FILE *fi,*fo;
static char ln[1000];
static char f1[100];
static char f2[100];
static char f3[100];
static char f8[100];
void main {
fi=fopen("in.txt","r");
fo=fopen("out.txt","w");
while (1) {
if (NULL==fgets(ln,1000,fi)) break;
if (4==sscanf(ln,"%[^|]|%[^|]|%[^|]|%*[^|]|%*[^|]|%*[^|]|%*[^|]|%[^|]|",f1,f2,f3,f8)) {
fprintf(fo,"%s:%s:%s(%s)\n",f1,f2,f3,f8);
} else break;
}
fclose(fo);
fclose(fi);
}
「已注销」 2011-07-27
  • 打赏
  • 举报
回复
既然格式是固定的,直接如下
int 字段1,字段2,...字段26;
FILE * fpin=fopen(fnin,"rb");
fscanf(fpin,"%d|%d|%d|%d|···|%d",&字段1,&字段2...,&字段26);
按格式按行读入,输出时同理,用fprintf
打开要写入的文件,然后
fprintf(fpin,"%d:%d:%d",字段1,字段2,字段8);
。。结束
zcp0317 2011-07-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 anyidan 的回复:]

那我想提取其中的某一段该怎么办呢?

你必须知道其位置或内容!
[/Quote]
比如我是想提取第2段和第5段的内容,我的想法是判断出1个“|”和第4个“|”的位置,然后再通过这个位置来提取。但现在我的问题是,我不知道知道了位置之后该怎么进行提取操作,不太懂哎,还有那个判断“|”位置用strstr()函数就可以么?
AnYidan 2011-07-27
  • 打赏
  • 举报
回复
那我想提取其中的某一段该怎么办呢?

你必须知道其位置或内容!
zcp0317 2011-07-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 cstdingjw 的回复:]

比如 :
char *pszStr = "I love you" | "ZhuJingmin" | "Saint Valentine's Day" | "1234";
char *pszTemp = NULL;

pszTemp = strtok(pszStr,"|");
while(NULL != pszTemp)
{
printf("%s\n",pszTemp);
……
[/Quote]


那我想提取其中的某一段该怎么办呢?
qianfoyuan 2011-07-26
  • 打赏
  • 举报
回复
比如 :
char *pszStr = "I love you" | "ZhuJingmin" | "Saint Valentine's Day" | "1234";
char *pszTemp = NULL;

pszTemp = strtok(pszStr,"|");
while(NULL != pszTemp)
{
printf("%s\n",pszTemp);
pszTemp = strtok(NULL,"|");
}
即可完成字符串分割.
strtok 函数在 string 类中,所以你要加上 #include <string.h>
lirg8405 2011-07-26
  • 打赏
  • 举报
回复
可以,你先实现下,遇问题了再问

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧