您好我想知道如何打印包含以下内容的文件:
Name : koukos
Surname : aidonis
AM : 6452
Score : 007
Time : Tue Jan 17 14:31:52 2012
-----------------------------------------------
Name : roberto
Surname : carlos
AM : 23456
Score : 2354
Time : Tue Jan 17 14:32:05 2012
-----------------------------------------------
例如,我想要实现的是自定义打印,例如忽略所有带有“名称”或“名称”和“姓氏”的行或仅打印“AM”。我目前正在尝试使用getc,但我不认为这是可能的。 fgets是否可以仅比较每行的前10个字符和缓冲区数组,然后如果它们相等则指向下一行并执行相同的操作?我也考虑过fseek,但是名字不会是恒定的长度所以它不能用于偏移。如果有人能帮助我,我会感激你的帮助。我的程序不完整,因为我不知道在打印功能中写什么。它与getc。问题在于print_exclude。(如果我解决这个问题,我也会得到print_isolated)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
void print(char name_file1[]);
void insert(char name_file[]);
void custom(char name_file[]);
void print_exclude(char name_file[]);
void print_isolated(char name_file[]);
int main()
{
char sel=' ';
while(sel != '0')
{
printf("(1)Insert Data\n(2)Print Content\n(3)Custom Print\n(0)Quit :\n");fflush(stdout);
sel= getchar();fflush(stdin);
if(sel=='1')
{
insert("catalog.txt");
}
else if(sel=='2')
{
print("catalog.txt");
}
else if(sel=='3')
{
custom("catalog.txt");
}
fflush(stdin);
}
return 0;
}
void custom(char name_file[])
{
char sel=' ';
while(sel != '0')
{
printf("Custom Print:\n(1)Print with exclusion\n(2)Isolated Print\n(0)Return\n");fflush(stdout);
sel= getchar();fflush(stdin);
if(sel=='1')
{
print_exclude("catalog.txt");
}
else if(sel=='2')
{
print_isolated("catalog.txt");
}
fflush(stdin);
}
}
void print_exclude(char name_file[]) //estw exclude to name
{
int i,line_init=0;// i=>check ending of lines and begining of line=>line_init.
FILE *src = fopen(name_file, "r");
for(i=getc(src); i!=EOF; i=getc(src)) //copy until eof
{
if (line_init!=0&&i!='N')
{
do
{
for(i=getc(src); i!='\n'; i=getc(src))
printf("%c",i);fflush(stdout);
}
while(i!='\n');
}
if (i!='\n') line_init++;
else if (i=='\n')
{
line_init=0; printf("%c",i);fflush(stdout);
}
}
fclose(src);
}
/*char exclude[8],buff[8];
int i;
FILE * comp;
memset(exclude,'\0',sizeof(char)*8);
memset(buff,'\0',sizeof(char)*8);
exclude[8]="name ";
comp=fopen("catalog.txt","r");
if (comp == NULL) perror("Error opening file");
else
{
for(i=getc(comp); i!=EOF; i=getc(comp)) //copy until eof
{
}
if ( fgets (buff,5,comp) != NULL )
{
puts (buff);
if (strcmp (exclude,buff) != 0)
printf("i found similarty!\n");fflush(stdout);
}
}
}*/
void print_isolated(char name_file[])
{
}
void print(char name_file[])
{
int i;
FILE *src = fopen(name_file, "r");
for(i=getc(src); i!=EOF; i=getc(src)) //copy until eof
{
printf("%c",i);fflush(stdout);
}
fclose(src);
}
void insert(char name_file[])
{
char name[20],sur[20],tel[20],scr[20],sel=' ';
do
{
time_t t;
time(&t);
FILE *stream;
memset(name,'\0',sizeof(char)*20);
memset(sur,'\0',sizeof(char)*20);
memset(tel,'\0',sizeof(char)*20);
memset(scr,'\0',sizeof(char)*20);
stream = fopen("Catalog.txt","a");
printf("+----------------------+\n");fflush(stdout);
printf(" Catalog for H/A \n");fflush(stdout);
printf("+----------------------+\n\n\n");fflush(stdout);
printf("Name : ");fflush(stdout);
scanf("%s",name);
fflush(stdin);
printf("Surname : ");fflush(stdout);
scanf("%s",sur);
fflush(stdin);
printf("AM : ");fflush(stdout);
scanf("%s",tel);
printf("Score : ");fflush(stdout);
scanf("%s",scr);
fflush(stdin);
fprintf(stream, "Name : %s\n",name);
fprintf(stream, "Surname : %s\n",sur);
fprintf(stream, "AM : %s\n",tel);
fprintf(stream, "Score : %s\n",scr);
fprintf(stream, "Time : %s\n",ctime(&t));
fprintf(stream, "____________________________________________\n");
fclose(stream);
printf("\n\n\nContinue ?(y/n) :\n");fflush(stdout);
scanf("%s",&sel);
}
while(sel=='y'||sel=='Y');
}
特定字段的更正打印:
void print_field(char name_file[],char buff[])
{system("cls");
FILE * pFile;
char *pch;
char mystring [50];
pFile = fopen (name_file, "r");
if (pFile == NULL) perror ("Error opening file");
else
{
while( fgets (mystring ,50 , pFile) != NULL )
{
if ((pch = strstr (mystring,buff))!=NULL) //if string is found
printf("%s",mystring);
}
}
fclose (pFile);
}
答案 0 :(得分:4)
我会使用类似fgets()
的内容一次一行地访问文件。
检查每一行并决定是否要打印它是一件简单的事情。您可以使用strstr()
之类的内容来确定该行是否包含某些特定文本。
答案 1 :(得分:0)
是的,您可以使用fgets
逐行读取文件,然后您有多个选项。我想到的两个问题是使用strncmp
仅比较第一个n
字符或使用regex.h
头文件并使用正则表达式来匹配您想要忽略的行。
答案 2 :(得分:0)
你需要使用C吗?在你的情况下,awk似乎更合适。