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

Why is C programming language called C?
It was first known as New B as it was an improved version of the B programming language. As C comes next to B in the English alphabet, it was renamed C later before its release. C is the only programming...
Shiva
0 0
5
Can stdout be forced to print somewhere other than the screen?
Although the stdout standard stream defaults to the screen, you can force it to print to another device using something called redirection
Vijay
0 0
5
Difference between "C structure" and "C++ structure".
C is process oriented programming.. so c follows procedure in execution where as C++ is object oriented and messages are been passes between the objects..
Khushboo

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

Ask a Question

Related Lessons

C PROGRAM FOR GENERATING SOUND
#include<stdio.h> #include<conio.h> #include<dos.h> void main() { sound(3000); // sound function have single parameter , this parameter we put integer value its generating...

V. Muthu Ganeshan

0 0
0

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

Compiler vs Interpreter
Compiler Interpreter Convert all the code into binary format and execute. Convert one statement at a time and execute, then Convert next statement and execute. After conversion, source...

INTRODUCTION TO PROGRAMMING LANGUAGES
Language is a medium for communication. If we want to perform anything with another person, we can know the human language as a human being. Similarly, if we want to perform anything with a computer, we...

Can we store different data types in a stack?
Yesterday, one of my Facebook friend asked me this question. My answer is "yes", and in this post I will discuss how could we do this.I am a great supporter of working with unions and I will be using union...

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