Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7062807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:38:18+00:00 2026-05-28T04:38:18+00:00

Hello i am trying to find out how can i print a file which

  • 0

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:38:18+00:00Added an answer on May 28, 2026 at 4:38 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find out how to anchor all of the content in a
I'm tearing my hair out trying to find how to just write a Hello
Hello I been trying to figure out how to set up ASP.Net forms authentication
I'm trying to do a find and replace in an OpenXML word document which
I've been googling around and trying to find out what is the behaviour of
I'm trying to write a regexp which will help to find non-translated texts in
I'm trying to build a hello world using GTK, which includes the line: #include
I'm trying to start off using FBJS, and I can't figure this out. The
I'm having trouble describing this issue, which is probably why I can't find the
I'm trying to learn wxWidgets, but I'm stuck on a point which I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.