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
-----------------------------------------------
私は、たとえば達成したいことは、カスタムを作ることです"名前"または "名前"と "姓"または "午前"のみを表示するすべての行を無視するように印刷します。私は現在getcで試していますが、これは可能だとは思いません。 fgetsでは、各行の最初の10文字だけをバッファ配列と比較し、次の行と等しい点を比較して同じことをすることは可能ですか?私もfseekについて考えましたが、名前は一定の長さではないので、オフセットで使用するような作業はできませんでした。 Plz誰かが私を助けることができるなら...あなたの助けに感謝します。私のプログラムは、私は印刷機能で何を書くべきか分からないので、完全ではありません。そのgetcと。問題はprint_excludeである(私はpropablyこの問題を解決する場合、私は同様print_isolated取得LL)
#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);
}
だから私は配列に各行を格納し、最初の8要素を比較して、それらが世界と一致するかどうかを確認する必要があります。それが一致すれば私はそれを印刷しません。右? – BugShotGG
私がfgets()を使って遭遇する問題は、\ n使用しないことを暗示している配列を出力したくない場合に行を変更する方法です。どうすれば次の行に切り替えることができますか? help plz :) – BugShotGG
前の行に戻る必要がない限り、行を配列に格納する必要はありません。各行を1つずつ処理するだけです。あなたは行を変更しません。あなたはそれを印刷するか、印刷しません。また、 'fgets()'をもう一度呼び出すと、次の行に切り替わります。 –