Reputation: 5190
Hello i am trying to find out how can i print a file which has content like:
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
-----------------------------------------------
what i want to achieve for example is to make custom print like ignoring all lines with "Name" or "Name" AND "Surname" or print only "AM". I am currently trying it with getc but i dont think that this is possible. Is it possible with fgets to compare only the, say, first 10 chars of each line with a buffer array and then if they are equal point to the next line and do the same thing? I also thought about fseek but the names wont be of constant length so it wouldnt work to use it with offset. Plz if anyone can help me... I appreciate your help. My program is not complete since i dont know what to write in the print functions. Its with the getc. The problem is in print_exclude.(if i solve this propably i ll get the print_isolated as well)
#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');
}
Corrected Print for specific field:
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);
}
Upvotes: 2
Views: 7326
Reputation: 17085
Yes you can use fgets
to read your file line by line and then you have several options. Two that come to my mind is to use strncmp
to compare only the first n
characters or use the regex.h
header file and use regular expressions to match lines you want to ignore.
Upvotes: 0
Reputation: 67203
I would use something like fgets()
to reach the file one line at a time.
It is then a simple matter to examine each line, and decide whether or not it's one you want to print. You could use something like strstr()
to determine whether or not that line contains some particular text.
Upvotes: 4