Solved

C++ searching a csv file or a text file to search

Posted on 1998-08-03
22
222 Views
Last Modified: 2012-06-21
Hi,
I want to search a text file by  passing a structure  . That is I want to display my search results.
Then based on those results I want to search a csv file and display the results.
 It is like I have to search a txt file with many lines, Where each line is having some entries whuch are seperated by commas. Depending on input I have to search the file for any matching and to display the results i.e
for eg., The lines are like this
12,anil, yes, no,xxx, yyy;
13,anda,no, no,aaa,bbb;
and  so on I will be getting the above things as a struct and I have to search.
similar is the case with the csv file.
If u have any source code accessing a csv file and searching it to Please send me.
Thanks in advance
 
0
Comment
Question by:bsat
  • 11
  • 10
22 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1169243
>> I want to search a text file by  passing a structure
What do you mean???  what is the structure for?  Does it specify what to search for?  Does it specify how to return what you find?  Does it specify the data to search?

Can you explain your question in greater detail?
0
 

Author Comment

by:bsat
ID: 1169244
Edited text of question
0
 

Author Comment

by:bsat
ID: 1169245
want to search a text file by  passing a structure  . That is I want to display my search results.
    Then based on those results I want to search a csv file and display the results.
     It is like I have to search a txt file with many lines, Where each line is having some entries whuch are seperated by commas. Depending on
    input I have to search the file for any matching and to display the results i.e
    for eg., The lines are like this
    12,anil, yes, no,xxx, yyy;
    13,anda,no, no,aaa,bbb;
    and  so on I will be getting the above things as a struct and I have to search.
    similar is the case with the csv file.
    If u have any source code accessing a csv file and searching it to Please send me.
    Thanks in advance
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169246
This makes no sense.  You say you want to pass a strucuture.  But for what reason?  how are you planning on using it.  I can think of at least 3 different purposes for the structure:

It specifes the data to be searched.
It specifes the data to search for (and search options)
It receives the results of the search.

What do you have in mind?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169247
If this is for an assignment we can not provide complete answers, but we can help you.  

If this is for an assignment, it might be a good idea to post the question.
0
 

Author Comment

by:bsat
ID: 1169248
If u please forget the structures and can tell me.
Sorry for troubling u. The structure specifies the datato search for.

0
 

Author Comment

by:bsat
ID: 1169249
If u please forget the structures and can tell me.
Sorry for troubling u. The structure specifies the datato search for.

0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1169250
There's just not much informaiton to go on here.  I'll give you an outline of an approach that will have to be tailored to your needs.  
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169251
If the text file is small enough, you can read the whole thing into memory and search in memory.
to do this you would:

1. open the file using fopen().
2. seek to the end of the file using fseek() with the SEEK_END seeky type and an offset of 0.
3. Get the length of the file using fgetpos()  (this doesn't return the length fo the file, it returns the current position, but since we fseek()ed to the end, the current position is the length.)
4.  Allocate memory for storing the file.  Use new char[Length] where "Length" is the length of the file obtained in 3.
5.  Seek back to the beginning of the file with fseek() and SEEK_SET.
6.  Read the file into the allocated memory using fread().
7.  Search the text in memory for the text you are looking for.
     an easy way is to use a loop that goes through each character in the file text looking for the first character in the search for text.  When a match is found, you compare all the characters in the search for text with the cooresponding characters in the seach in text  (I can elaborate on this if you need.)
8.  Clean up by closing the file fclose() and deleting the memory.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169252
If the file is too long to handle that way, you can load it in "sections"  You would follow the same basic procedure I outlined above, but you don't read the entire file (so you don't have to seek to the end and gets its length) at one time, you read a portion of it, like maybe 256 bytes.  Then you search in that portion.   If you don't find a match you go on to the next portion.  

There is one pitfall in this approach.  The text to search for might  be positioned so it spans two sections.  That is it starts at the end of one section and ends at the start of another section.  The search (as i described it) won't find it then.  There is a way (several) around this.  I can elaborate if you want to go that route.
0
 

Author Comment

by:bsat
ID: 1169253
Hi,
The thing is that I have to find an exact match for the line by passing all the variables which are distinguished by commas in the text file and i have to send all these 6 present in the line to output. Can u please eloborate on how to search?

 
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:bsat
ID: 1169254
The thing is that I have to find matches for the line by passing all the variables which are distinguished by commas in the
  text file and i have to send all these 6 present in the line to output. Can u please eloborate on how to search? I will be passing many lines as output.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169255
>>I want to search a text file by  passing a structure  . That is I want to display my
>> search results.  Then based on those results I want to search a csv file and
>> display the results.

Are their two searches to be done here or one?

The search I desribed before was text oriented.  It can be used to find an exact match for a section of text in the file.  That text might even span lines.  The following search is a similar but a little more suited for a CSV file.  It will not necessarily look for exact matches.  For example if there are extra spaces on a line they will be ignored, only the data on the line will be used in the search.

continues.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169256
For this I would use an fstream to read from the file.  

1. Declare an fstream object and specify the file name and the "ios::in" mode.
2. Read a single line from the file using the "getline()" member function.  Use the version
    of getline that allows you to specify the maximum line length and the terminator.  Like
    fil.getline(LinBuf,80,'\n').
3. Parse the line and read it into a structure or class that represents the data specified on that line.  How you do this will depend on the data to some extent.   I would need more details to help you on that.
4.  Compare a structure containing the data to search for with the structure you just filled from the file.  If they are the same, you are done searching, otherwise continue searching by reading the next line.
5.  Close the file stream with the close() member.

0
 

Author Comment

by:bsat
ID: 1169257
There are two searches.
1. I have to search in a csv file.
2. I have to search in a text file. I am facing problem with the second search at present. I will be getting a number, and some statusses which alltogether with the number form a line in the text file and are seperated by commas. I have to search the file
for all the lines with specified number or any of the statuss.
The search should yield results by number or status 1 or status2
or status3 or status4 or status5.And all the lines which has anyof the above are outputted.
If u have any source code...please do send it to me. It is very urgent.  
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169258
You are going to have to write the code.  It is going to depend on what is on each line.  It depends on the format of the file.  If the line contains numbers parsing it is different that when it contains strings.  If it contains a mixture of the two it is different again.  If some ofthe numbers are integers and some are floating point it is different again.  

You say the lines look like this

   12,anil, yes, no,xxx, yyy;

what are the restructions of each "field"?  what should they be read into.  Like is the first always an integer number?  Could it be a decimal number?  Could it be a word, like "the" and just in the example happens to be a word that is a number?  Could it be a sentince that has spaces in it?  How should it be stored? as an int?  as a double? as a char * array?

Same thing for the other fields.   I see the "yes" and "no"s in there?  will those fields always contain either a yes or no?  will they always be lowercase?
0
 

Author Comment

by:bsat
ID: 1169259
Hi,
My First Search is like this:
Sorry for the late reply. I was not here. The first field is always an integer.The remaining fields are srings and one field is date. The user enters the range of the numbers like 1 to 20 in the userinterface and checks the radio buttons which correspond to different fileds and says search.
My class should search the .lst file and submit all the results corresponding to sprcified number range.

 There should be another search like:
There will be certain fields in each line of the  file like
1.Started
2.Ended
3.Date.
I have to search the file based on date.
If the user enters a from date  and a to date  and says search....
 I have to search the file and display all the dates and and count of all Started on that date and count of all Ended on that date for all dates.
Hope u can help me ASAP. It is very urgent here.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1169260
what is the format of date?
0
 

Author Comment

by:bsat
ID: 1169261
Hi,
My First Search is like this:
Sorry for the late reply. I was not here. The first field is always an integer.The remaining fields are srings and one field is date. The user enters the range of the numbers like 1 to 20 in the userinterface and checks the radio buttons which correspond to different fileds and says search.
My class should search the .lst file and submit all the results corresponding to sprcified number range.

 There should be another search like:
There will be certain fields in each line of the  file like
1.Started
2.Ended
3.Date.
I have to search the file based on date.
If the user enters a from date  and a to date  and says search....
 I have to search the file and display all the dates and and count of all Started on that date and count of all Ended on that date for all dates.
Hope u can help me ASAP. It is very urgent here.
0
 

Author Comment

by:bsat
ID: 1169262
The format of the date is yyyy/mm/date.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169263
Hopefully this will get you started.


fstream F("FILENAME.TXT",ios::in);

while (!F.eof())
{
   const int MAXLINLEN = 100;
   char LinBuf[MAXLINLEN];
   F.getline(LinBuf,MAXLINLEN);
   int Num;
  char Name[MAXLINLEN];
  char YesNo[MAXLINLEN];

   sscanf(LinBuf," %i , %s , %s",Num,Name,YesNo);
}


You will have to customize this sort of approach for your needs.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169264
If you can ask specific questions or start on this and then post what you get done, we can provide more help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now