Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie: How to read/parse lines from a file

Status
Not open for further replies.

cadbilbao

Programmer
Apr 9, 2001
233
ES
Hello.

I'm trying to detect a line with this text: "OPINION"

I'm working with:
char c,c2;
while(c!=EOF)
{
c=fgetc(myfile);
if (strcmp (c,"O") = 0 )
{
.......

But I consider that there's another easier option, isn't?

I would be extremely obliged of anybody could help me...
 
// how about reading the whole file in a buffer...
char file|10000|;

char temp;
int i=0;

while(file>>temp){
file|i++| = temp;
}

file|i| = '\0';

// and then use the strstr function to retrieve the position of your text...

char* ptr = strstr(file, "WHATYOUARESEEKING");

// ptr now points on "WHATYOUARESEEKING" or NULL if it wasn't found

ptr += strlen("WHATYOUARESEEKING");
//now points on the next value...

I didn't compile this code but that praticaly should be it... hope this help...
 
ifstream myfile.open("name");

while(myfile>>temp){
file|i++| = temp;
}

myfile.close();

I forgot this... sorry
 
here's a function I have done...


// you call the function like this
ifstream in;

if (seekFile(in, "MYWORD")){
... in is positionned at the caracter folowing "MYWORD"
}
else{
... wasn't found
}

// function definition
bool seekFile(ifstream& in, const char* seekString){
char tmp;
int pos=0;
int seekStringLength = strlen(seekString);
bool returnValue = false;

while(pos!=seekStringLength && in >> tmp){
if (seekString[pos] == tmp){
++pos;
}
else{
pos=0;
}
}
if (pos==seekStringLength) returnValue = true;
return returnValue;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top