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

Can a functions return more than one value at a time?
With the use of pointers a function can return multiple values.
Lamaan
0 0
7
Is C programming the best for beginners?
Yes, it is good to start with C so that other programming would be easy to understand
Amit
0 0
7
Can we have if inside else-if or else in C programming language?
Yes it is possible to do so. The if... else if..... else statements support one other inside themselves... so...
Kirthi
How do I start learning C programming and finish it in one month?
Please follow the book "C Programming" by Detal & Detal. Its an applied book easy to understand compare to others. U may also learn through online free tutorial.
Pallavi
0 0
8
What is the benefit of using enum to declare a constant?
ENUMS.. Can give numerical values for Sunday, Monday,... Easy to use in coding... Makes the program more readable/ understandable, than using 1,2,3.. Etc.
Sita
0 0
7

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

Ask a Question

Related Lessons

How to write shared library program using C Programming?
Library Name : hellofilename : hello.c#include void Hello(){ printf("Hello World .. How are you 1\n");}======================================Header File:hello.h#ifndef __HELLO_H#define __HELLO_Hvoid Hello(void);#endif===============================Application...

Datatypes in C Language
Data types in C Language Data types specify how we enter data into our programs and what type of data we enter. C language has some predefined set of data types to handle various kinds of data that...

What Are IT Industries Performance Metrics?
1. Outstanding Expectation: Eligible to get Promotion easily and good salary hike. Always preferrable to go abroad. 2. Exceed Expectation: Can get Promotion as per schedule of company with good salary...

Working with C/C++ applications
Inorder to learn C and C++ programming languages one can work with various editors available.To name a few are the most popular one is turbo c++, DEV C++, Eclipse, NetBeans. Here are the screen shots...

Facts about C language
C programming language was developed in 1972 by Dennis Ritchie at AT&T Bell Labs. It was developed to overcome the problems of languages such as B, BPCL. It was developed to write the Unix operating...

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 >

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 >

Applications engineering is a hot trend in the current IT market.  An applications engineer is responsible for designing and application of technology products relating to various aspects of computing. To accomplish this, he/she has to work collaboratively with the company’s manufacturing, marketing, sales, and customer...

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