473,321 Members | 1,622 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,321 software developers and data experts.

strcmp problem

I done a program to read text file, where content is

#Header#
name: Juicy
age: 25
@Header@
#Text#
user text here
@Text@
#End#
tel
fax
@End@
eof

And I read the text file

$filename = "/usr/local/file/rpt.txt";
$fp = fopen($filename, 'r');

$content = fgets($fp);
while(strcmp($content,"@End@")<>0) {

$content = fgets($fp);

if(strcmp($content, "#Text#")==0){ //not match
$strPretext .= $content;
}//end if
//end while
I should get the data from #Text# until one line before @End@, but I get
nothing, I found that the strcmp for #Text# always not match when it read
through whole file.
I cant find out what goes wrong. Please advise.

Thanks.
Mar 27 '06 #1
1 1802
Rex
Some observations: When reading a file you can use the funciton
feof() to check if a file pointer has reached the end of file marker.
This is useful when looping thru a file's contents.
I dont believe the inital fgets is needed -- just grab the first
line in the loop.
Also, instead of using the binary string comparision function use
the == operator and trim $contents to remove any uneeded whitespace
chars.
I'm not sure if the following listing is what you're looking for
as $strPretext only contains "#Text#" once the code is executed but i
think it's a step in the right direction.
If you're looking for the line after the "#Text#" line just do
another $content = fgets($fp); (line read) inside the if stmnt.

$filename = "/usr/local/file/rpt.txt";
$fp = fopen($filename, 'r');
$strPretext = "";

while( !feof($fp) ) {
$content = fgets($fp);
if( trim($content) == "#Text#") {
$strPretext .= $content;
}
}

echo $strPretext;

Mar 27 '06 #2

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

Similar topics

6
by: Shane Peck | last post by:
I am having a problem using the strcmp function. If I debug my program from within Visual C++, strcmp returns 0 as I want, but if I build the project and then try and execute it, it seems to think...
6
by: muser | last post by:
The following error appears: 'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'. I've already tried using single quotations. the header file only contains the struct contents....
6
by: Robert Mens | last post by:
Hi, I've got this problem with this project i am working on, i am a bit new to c so i don't know why this happens. string is read from a telnet socket. Here's the piece of code that...
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
10
by: lchian | last post by:
Hi, For two stl strings s1 and s2, I got different results from strcmp(s1.c_str(), s2.c_str()) and s1.compare(s2) can someone explain what these functions do? It seems that strcmp gives...
36
by: Chuck Faranda | last post by:
I'm trying to debug my first C program (firmware for PIC MCU). The problem is getting serial data back from my device. My get commands have to be sent twice for the PIC to respond properly with...
0
by: noobcprogrammer | last post by:
#include "IndexADT.h" int IndexInit(IndexADT* word) { word->head = NULL; word->wordCount = 0; return 1; } int IndexCreate(IndexADT* wordList,char* argv)
6
by: kevin | last post by:
Hi All here is a problem I just can't understand! I don't really want a work around I just want this to plainly work or atleast explained.... Thanks in advance.... Problem: strcmp(st, s) it runs...
47
by: fishpond | last post by:
One way I've seen strcmp(char *s1, char *s2) implemented is: return immediately if s1==s2 (equality of pointers); otherwise do the usual thing of searching through the memory at s1 and s2. Of...
4
by: Marcus | last post by:
you guys are funny. i thought this was comp.lang.c++... not comp.we.can.only.comment.on things.that.can.be.compiled.c++ back in the day in this group, the jibe replies would've looked like:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.