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

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
 
bsatAsked:
Who is Participating?
 
nietodCommented:
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
 
nietodCommented:
>> 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
 
bsatAuthor Commented:
Edited text of question
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
bsatAuthor Commented:
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
 
nietodCommented:
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
 
nietodCommented:
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
 
bsatAuthor Commented:
If u please forget the structures and can tell me.
Sorry for troubling u. The structure specifies the datato search for.

0
 
bsatAuthor Commented:
If u please forget the structures and can tell me.
Sorry for troubling u. The structure specifies the datato search for.

0
 
nietodCommented:
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
 
nietodCommented:
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
 
bsatAuthor Commented:
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
 
bsatAuthor Commented:
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
 
nietodCommented:
>>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
 
nietodCommented:
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
 
bsatAuthor Commented:
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
 
nietodCommented:
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
 
bsatAuthor Commented:
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
 
ozoCommented:
what is the format of date?
0
 
bsatAuthor Commented:
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
 
bsatAuthor Commented:
The format of the date is yyyy/mm/date.
0
 
nietodCommented:
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
 
nietodCommented:
If you can ask specific questions or start on this and then post what you get done, we can provide more help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.