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

problems with CR (carriage return) and LF (line feed )

I have created a program that downloads a web page and then performs
some text processing on it . The problem is in the text processing ,
every line (in the downloaded txt file ) ends with a strange symbol
which is the carriage return and the line feed . ( Hex values 0D and
0A ). How are these values represented in C ??? . For istance for
every character I read from the file i want the function to ignore it
.. for example :

.................................................. ..

while((c=fgetc(fp) ) != EOF )
{

switch(c)
{
case '<' :
{
tagFlag=true;
cont=true;
i=0;
if(getvalue==1)
{
getvalue=0;
string_found=false ;
}
break;
}
case '>' :
{
tagFlag=false;
break;
}
case <<<<<< What should i put here ??????
{
break;
}
default :
{
if( (string_found == true) )
{
if(tagFlag == false )
{

getvalue=1;
printf("%c \n",c);
}
}
else if( (string_found==false))
{
if( (tagFlag==false) &&
(cont==true))
{
if(c==target[i])
{

if(i==
(target.GetLen()-1) )
{
times_found++;

string_found=true;
}
else
{
i++;

cont=true;
}
}
}
}
break;

}
}
}
..................................................

The file is stored like this :
......................................

if(ret == SOCKET_ERROR)
{

exit(EXIT_FAILURE);
}

_setmode(_fileno(fp), _O_TEXT);
/* fp is the file pointer */
do
{
bytesRead = recv(itsSocket, Buffer,
sizeof(Buffer), 0);

fwrite(Buffer,sizeof(char),bytesRead,fp);
} while(bytesRead!=0)

(Ok I know socket programming is offtopic but my question isn't ....
)
Nov 13 '05 #1
6 20388
Andrew wrote:
I have created a program that downloads a web page and then performs
some text processing on it . The problem is in the text processing ,
every line (in the downloaded txt file ) ends with a strange symbol
which is the carriage return and the line feed . ( Hex values 0D and
0A ). How are these values represented in C ??? .


0x0D and 0x0A. Alternatively, '\x0D' and '\x0A'.

It may be that these values happen to correspond to characters in the
execution character set, and can be represented some other way (such as
'\r' or '\n', for example), but this is implementation-dependent.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Nov 13 '05 #2
in comp.lang.c i read:
I have created a program that downloads a web page and then performs
some text processing on it . The problem is in the text processing ,
every line (in the downloaded txt file ) ends with a strange symbol
which is the carriage return and the line feed .
naturally they do, that's what the http specification requires -- i.e.,
http `headers' must all end with crlf. generally files are transported
verbatim, so those bytes are likely present in the file, on the server.
( Hex values 0D and 0A ). How are these values represented in C ??? .


umm, 0x0d and 0x0a.

--
a signature
Nov 13 '05 #3
Andrew wrote:

I have created a program that downloads a web page and then performs
some text processing on it . The problem is in the text processing ,
every line (in the downloaded txt file ) ends with a strange symbol
which is the carriage return and the line feed . ( Hex values 0D and
0A ). How are these values represented in C ??? . For istance for
every character I read from the file i want the function to ignore it
. for example :
I have taken the liberty of reformating your code so I can clearly
indicate suggested changes (which are no longer quoted lines).
.................................................. .

while ((c = fgetc(fp) ) != EOF ) {
switch(c) {
case '<' : tagFlag = true;
cont = true;
i = 0;
if (getvalue == 1) {
getvalue = 0;
string_found = false ;
}
break;

case '>' : tagFlag=false;
break;

/* case <<<<<< What should i put here ?????? */ case '\n':
case '\r': break;
default : if ( (string_found == true) ) {
if (tagFlag == false ) {
getvalue = 1;
printf("%c \n",c);
}
}
else if ( (string_found == false)) {
if ( (tagFlag == false) && (cont == true)) {
if (c == target[i]) {
if (i == (target.GetLen()-1) ) {
times_found++;
string_found = true;
}
else {
i++;
cont = true;
}
}
}
}
break; } /* switch */
} /* while */


Excessive vertical spacing is just as harmful to comprehensibility
as the lack of breaks. Note that braces around the individual
cases are useless and confusing, as code normally simply executes
in order in the absence of a break.

I believe that the standards for HTML specify that those lines end
in \r\n, so the solution should be portable. However I am not
sure of this. You may want to inject a blank, which you can
probably do by replacing the "break" with "c = ' '" and falling
through. Other than this I am making no allegations about the
accuracy of the code.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 13 '05 #4
in comp.lang.c i read:
I believe that the standards for HTML specify that those lines end
in \r\n, so the solution should be portable.


they specify 0x0d 0x0a. whether those correspond to \r and \n depends on
the implementation. most likely they will, but the key to writing portable
code is in not making assumptions you can avoid.

--
a signature
Nov 13 '05 #5
those who know me have no need of my name wrote:
I believe that the standards for HTML specify that those lines
end in \r\n, so the solution should be portable.


they specify 0x0d 0x0a. whether those correspond to \r and \n
depends on the implementation. most likely they will, but the
key to writing portable code is in not making assumptions you
can avoid.


Of course. But the i/o system would presumably make those
translations if the internal system is not ascii based. At any
rate, the point is that it is a vulnerability to be watched when
porting.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #6
Thank you very-very much people it worked fine !!!!
Nov 13 '05 #7

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

Similar topics

3
by: Canes_Rock | last post by:
The information posted at: ...
1
by: wschaub | last post by:
Is there any way of forcing an ASMX web service not to translate a carriage return – line feed (\r\n) with a (\n\n), in other words the carriage return \r is replaced with a \n if contained as...
3
by: David N. | last post by:
Hi All, I spent too much time on trying to get the CrLf into a string, which contains embedded SQL statements that can be executed by the SQLClient.SqlCommand. Note that these SQL statements...
1
by: Jim Heavey | last post by:
In the Regular Expression, I can use \n for Line Feed, \t for a tab and \r for carriage return. Am I able to use thos same expressions in a "Replace method" of the string object? such as ...
2
by: sys | last post by:
I have a web method that returns a string. When I return a string that has a carriage return and line feed in it ("\r\n") on the client side I only receive a string including the line feed ("\n")....
11
by: TheRain | last post by:
Hi, I am trying to append a carriage return to my string using the string builder class, but when I do this the string ends up containing "13". I tried this multiple ways like so ...
3
by: Gillian Steele | last post by:
I'm trying to import data into a memo field by running an append query. The data is to be imported as if it was entered with separate lines in the memo field. I have tried inserting chr(13)s...
4
by: whitej77777 | last post by:
I am trying to write a user defined function that will allow me to strip off the last carriage return and line feed from a text field. We have address fields stored in a text field for our ERP...
4
by: coolguyraj | last post by:
Hi. I have form that has a text box. I want insert unformatted text into the database. Even if the user gives an carriage return or New line feed in the database it should be stored as on single...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.