UrbanPro

Learn C Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

How do I search a string in a file in C programming?

Asked by Last Modified  

Follow 1
Answer

Please enter your answer

Learning Education

#include stdio.h #include string.h #include stdlib.h #ifdef DEBUG #define INITIAL_ALLOC 2 #else #define INITIAL_ALLOC 512 #endif char * read_line(FILE *fin) { char *buffer; char *tmp; int read_chars = 0; int bufsize = INITIAL_ALLOC; char *line = malloc(bufsize); ...
read more
#include stdio.h #include string.h #include stdlib.h #ifdef DEBUG #define INITIAL_ALLOC 2 #else #define INITIAL_ALLOC 512 #endif char * read_line(FILE *fin) { char *buffer; char *tmp; int read_chars = 0; int bufsize = INITIAL_ALLOC; char *line = malloc(bufsize); if ( !line ) { return NULL; } buffer = line; while ( fgets(buffer, bufsize - read_chars, fin) ) { read_chars = strlen(line); if ( line[read_chars - 1] == '\n' ) { line[read_chars - 1] = '\0'; return line; } else { bufsize = 2 * bufsize; tmp = realloc(line, bufsize); if ( tmp ) { line = tmp; buffer = line + read_chars; } else { free(line); return NULL; } } } return NULL; } int main(int argc, char *argv[]) { FILE *fin; char *line; if ( argc != 3 ) { return EXIT_FAILURE; } fin = fopen(argv[1], "r"); if ( fin ) { while ( line = read_line(fin) ) { if ( strstr(line, argv[2]) ){ fprintf(stdout, "%s\n", line); } free(line); } } fclose(fin); return 0; } read less
Comments

Engineer

you have to declare the syntax and then type char filename=name.txt,line =2000,search string =number
Comments

Advance Excel And VBA Training

Please see the attached Images
Comments

View 1 more Answers

Related Questions

How can you check to see whether a symbol is defined?
You can use the #ifdef and #ifndef preprocessor directives to check whether a symbol has been defined (#ifdef) or whether it has not been defined (#ifndef).
Name
How do I get C help in Ubuntu?
Use command line tool 'man'. for example 'man printf' will give you details regarding printf function C
Ashmita
0 0
5
What is an lvalue?
L-Value stands for left value L-Value of Expressions refer to a memory locations In any assignment statement L-Value of Expression must be a container(i.e. must have ability to hold the data) Variable...
Nagasanthoshi
How do I learn C programming easily?
Just follow kanitkar book of c programming for beginners
Harsh
0 0
5
Basics of 'C'language
C is middle level language, using C you can write program which directly interact with the hardware like device driver programming or you can write GUI application. C is very powerful language compare...
Raghavendra

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Basic Concepts of Web Designing
An introduction to domain names, web servers, and website hosting 1)What is the web? In a nutshell, the web is a whole bunch of interconnected computers talking to one another. The computers (on the...

C-Program Swapping Contents Of Variables Using Function [Call By Reference Method]
//Header Files #include#include // User defined functions swap with 2 pointer variables passed as an argument list void swap(int*i,int*j){ // Local variable temp int temp; // swapping contents using...
S

Dynamic Memory Allocation in C using malloc()
#include <stdio.h>#include <conio.h>//#include <malloc.h> OR#include <stdlib.h>void main(){ int *ptr, i, n, sum = 0; printf("how many elements ? "); scanf("%d", &n);...

C Program-Error Handling
//Header files #include<stdio.h>#include<conio.h>#include<stdlib.h> //Main function void main(){ int dividend=10; int divisor=0; int quotient; //Function for clearing screen clrscr(); ...
S

Array vs Linked List
Array Linked List Accessing element is easy. Accessing element is difficult compare to Array. Easy to use. Difficult to use. Memory is Fixed size. Memory is variable size. If...

Recommended Articles

Brilliant Academy is one of the reputed institutes for B.Tech tuition classes. This institute is specialised in delivering quality tuition classes for B.E, Engineering - all streams and Engineering diploma courses. Incorporated in 2012, Brillant Academy is a brainchild of Mr Jagadeesh. The main motto of the academy is to...

Read full article >

Lasya Infotech is a Hyderabad based IT training institute founded in 2016 by O Venkat. Believing in his innovation, passion and persistence and with a diverse blend of experience, he started his brainchild to deliver exemplary professional courses to aspiring candidates by honing their skills. Ever since the institute envisions...

Read full article >

Hadoop is a framework which has been developed for organizing and analysing big chunks of data for a business. Suppose you have a file larger than your system’s storage capacity and you can’t store it. Hadoop helps in storing bigger files than what could be stored on one particular server. You can therefore store very,...

Read full article >

Software Development has been one of the most popular career trends since years. The reason behind this is the fact that software are being used almost everywhere today.  In all of our lives, from the morning’s alarm clock to the coffee maker, car, mobile phone, computer, ATM and in almost everything we use in our daily...

Read full article >

Looking for C Language Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for C Language Classes?

The best tutors for C Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C Language with the Best Tutors

The best Tutors for C Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more