473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

need help with search code

I'm fairly new to C, but have most of the basics covered. I have a file
that my program writes to, but I need a way to search for information within
that file and then edit that information or print it to the screen. I would
like to be able to input a string (for example) and have it scan that file
for the exact string. I'm just not too sure how to approach this. Any help
would be great. Thanks in advance!
Nov 13 '05 #1
1 1292
"Edwin Parker" <ep*****@mobilemate.com> wrote:
I'm fairly new to C, but have most of the basics covered. I have a file
that my program writes to, but I need a way to search for information
within that file and then edit that information or print it to the
screen. I would like to be able to input a string (for example) and have
it scan that file for the exact string. I'm just not too sure how to
approach this. Any help would be great. Thanks in advance!


If the file is a text file in lines of a limited length, and the search
term must be within one line, you could use fgets to read each line of the
file into a buffer and use strstr function to look for the search term:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
char linebuf[200], *newl, *found;
FILE *fp;
if(argc != 3)
{
fprintf(stderr, "Usage requires two arguments: search term then filename\n");
exit(EXIT_FAILURE);
}
fp = fopen(argv[2], "r");
if(fp == NULL)
{
fprintf(stderr, "Error opening file %s for read as text\n", argv[2]);
exit(EXIT_FAILURE);
}
while(fgets(linebuf, sizeof linebuf, fp))
{
newl = strchr(linebuf, '\n');
if(!newl) fprintf(stderr, "Warning: long line split\n");

found = strstr(linebuf, argv[1]);
if(found) fputs(linebuf, stdout);
}
return 0;
}

--
Simon.
Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: nortelsale | last post by:
I know nothing about programming but I am building a website (written with php) that allow people post some info. Is there a site I can get some templates or php scripts for an advanced search...
3
by: Liz Malcolm | last post by:
Hello and TIA for guidance. I am building a reusable search procedure (thanks go to Graham Thorpe for his example that set me on my way). Everything works up until the 2nd match is found, the...
7
by: satan | last post by:
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList. These are my classes: ...
0
by: Chuck36963 | last post by:
Hi all, I've been working on a listing problem and I can't figure out how to work it out. I have looked far and wide on the web to find answers, but I'd like other peoples input on my project in...
9
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result...
1
by: ebernedo | last post by:
Hey guys, I have two main questions First off (pictures are kind of blurry) I have this table http://i197.photobucket.com/albums/aa109/ebernedo/DiscTable.jpg And thats my database I use my...
3
by: suek | last post by:
I have a table with over 4000 records to search upon, and the users don't like a combo box. So what I have been trying to do for the last twelve hours is do some code to get a text box to search. ...
4
by: Freedolen | last post by:
Hi I found a website "http://www.gahooyoogle.com" which is a search engine and fetches result from both yahoo and Google. tried to develop a similar one but with more simpler and i...
3
by: teephish | last post by:
Hello, I'm currently in the process of creating a small access database and I'm having some problems with creating a customized search. I would like the user to be able to search a record by last...
1
by: vikjohn | last post by:
I have a new perl script sent to me which is a revision of the one I am currently running. The permissions are the same on each, the paths are correct but I am getting the infamous : The specified...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.