Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 109,546 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,176 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Search for keywords and return line of code

 
Reply to this topicStart new topic

Search for keywords and return line of code, Read the input file and search for keywords

yuyuivy
post 7 Aug, 2008 - 07:07 AM
Post #1


New D.I.C Head

*
Joined: 7 Aug, 2008
Posts: 1

Hello, i'm writing a program which can read a text file (input) and it can find keywords such as "Module" and "Form" from the input text file. The keywords found are then write into an output file. My program should search for those keywords and return the line of code where the keywords are found.

The following is my program but there are errors >> no conversion from 'int' to 'char *'. Can anyone help me to check on my coding?
Besides, can someone teach me how to return the line of code when the keyword is found?
Thank you very much for your help. I have attached the input file.


cpp


#include "stdafx.h"
#include <iostream>
#include <fstream>

const int COL_LEN=500;
const int ROW_LEN=500;
typedef char Content[COL_LEN];

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
Content word[ROW_LEN];
int n=0;

ifstream infile("Project1.txt");
ofstream outfile("output_Project1.txt");

while(infile.getline(word[n++], COL_LEN) && n<ROW_LEN);
--n;
for (int i=0;i<n;i++){

if(word[i]=='M'&&word[i+1]=='o'&&word[i+2]=='d'&&word[i+3]=='u'&&word[i+4]=='l'&&word[i+5]=='e')
outfile <<word[i] <<endl;
}
return 0;
}

Attached File  Project1.txt ( 2.03k ) Number of downloads: 3


Mod edit: You only need to post the download link once!
User is offlineProfile CardPM

Go to the top of the page


pertheusual
post 9 Aug, 2008 - 05:27 PM
Post #2


D.I.C Head

**
Joined: 26 Jan, 2008
Posts: 222



Thanked 1 times
My Contributions


You should use the C++ string class instead of your char array. It has methods for searching strings.

You can read each line into a string, and keep track of the line number. Then if the line contains the keyword(using the find method of the string class), print the info you need into the output file.

Per
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 9/7/08 10:05PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month