473,396 Members | 2,030 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,396 software developers and data experts.

Carriage return

Can anyone please help!!!!!!
I want to develope a program which should count lines of code.
for that I want to know what is carriage return value in C.
So please reply me as soon as possible.If possible send me the code also.
Thank you.
Apr 29 '07 #1
17 2152
AdrianH
1,251 Expert 1GB
Can anyone please help!!!!!!
I want to develope a program which should count lines of code.
for that I want to know what is carriage return value in C.
So please reply me as soon as possible.If possible send me the code also.
Thank you.
The carriage return value is '\r', the line feed value is '\n'. The single quotes are used to get the character value of the character inside it. I.e. 'A' is 65.

We will not be giving you any code here. If you need help, try out the problem yourself and if you are still having difficulty with it, post the relivant code with a description as to what the problem you are having is.

Good luck,


Adrian

P.S. Do not double post. Thank you. See the posting guidelines here
Apr 29 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
\r is carriage return
\n is carriage return + newline feed

Probably you want \n.
Apr 29 '07 #3
AdrianH
1,251 Expert 1GB
\r is carriage return
\n is carriage return + newline feed

Probably you want \n.
If you want to get technical, in reading from/writing to a text file you are correct... under Windoze. It is not portable though.

Different operating systems define it differently. IIRC, Unix defines \n as carriage return only, Mac defines it as line feed + carriage return (reverse of Windows), other OSs I haven't a clue. I no longer work with text files anymore because of this portability issue, unless I am sure that the file generated will never be shifted to another OS or I don't care ;).


Adrian
Apr 29 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Different operating systems define it differently. IIRC, Unix defines \n as carriage return only, Mac defines it as line feed + carriage return (reverse of Windows), other OSs I haven't a clue. I no longer work with text files anymore because of this portability issue, unless I am sure that the file generated will never be shifted to another OS or I don't care.
Good point.
Apr 29 '07 #5
Banfa
9,065 Expert Mod 8TB
\r is carriage return
\n is carriage return + newline feed

Probably you want \n.
\n is never carriage return + newline feed, the C standard states that for a file opened in text mode the end of line marker should be trabnslated to \n. For windows this is carriage return + newline feed.

However if you put \n in a string then that string will contain a newline character with no extra characters.

The clarification I am making is that the translation is part of the file handling and nothing to do with characters and strings.
Apr 29 '07 #6
Banfa
9,065 Expert Mod 8TB
I no longer work with text files anymore because of this portability issue
That's interesting because portability freaks (not that I am one) normally claim that text is the only truly portable way of transferring data between systems, hence(or at least partly hence) XML has come into being.
Apr 29 '07 #7
AdrianH
1,251 Expert 1GB
That's interesting because portability freaks (not that I am one) normally claim that text is the only truly portable way of transferring data between systems, hence(or at least partly hence) XML has come into being.
You are only partly right. If I generate a text file on a Mac and then copy the file over to a PC without the proper translation software, i.e. a binary copy not a text one, this will fail to be read correctly on the PC.

It really starts to get messy when using cygwin using a Unix eol sequence.

I really hate the non-standerdised end of line character sequences. :(


Adrian
Apr 30 '07 #8
AdrianH
1,251 Expert 1GB
\n is never carriage return + newline feed, the C standard states that for a file opened in text mode the end of line marker should be trabnslated to \n. For windows this is carriage return + newline feed.

However if you put \n in a string then that string will contain a newline character with no extra characters.

The clarification I am making is that the translation is part of the file handling and nothing to do with characters and strings.
True, but if you are processing the input stream, you have to be aware of it.


Adrian
Apr 30 '07 #9
Banfa
9,065 Expert Mod 8TB
True, but if you are processing the input stream, you have to be aware of it.
True, I have to say I almost never open a file for input in text mode, I prefer to just handle the possibility of varying line end characters which you have to handle anyway if want the possibility of reading files that may have originated from any platform.
Apr 30 '07 #10
JosAH
11,448 Expert 8TB
Java implements a nice utility class: System. It has a member which basically
is a map<string, string>. It stores all sorts of handy little system dependent
values; one of them is "line.separator" and its value is one of \n, \r\n, \n\r or
whatever. I stole the idea and I carry such a map around, one per platform.
The map is read in memory at startup of my code and I never have to worry
about those dependencies anymore in my C++ code.

kind regards,

Jos
Apr 30 '07 #11
AdrianH
1,251 Expert 1GB
Java implements a nice utility class: System. It has a member which basically
is a map<string, string>. It stores all sorts of handy little system dependent
values; one of them is "line.separator" and its value is one of \n, \r\n, \n\r or
whatever. I stole the idea and I carry such a map around, one per platform.
The map is read in memory at startup of my code and I never have to worry
about those dependencies anymore in my C++ code.

kind regards,

Jos
Does it work in the case that I describe above? I.e. You are on a PC platform running your app that reads in a text file. That text file was generated on a Mac platform and was binary copied to the PC platform. Will your app recognise the Mac eol anyway?

Personally, I use scanf a lot using a format string something like: "%[^\r\n]" (with a buffer limit stated between the % and the [ of course) to read in a line and then "%*[\r\n]" to flush out the end of line characters.

When I output the chars, I either open it up as a text file and use '\n' or open it up as a binary and use a macro to output the correct EOL symbol(s) for the platform. In the cases that I don't care if it is compatiable for the local platform, I just use '\n' in a binary format. In the cases that I do care, and don't care about it's compatiblility on another platform, I use the appropriate local representation.


Adrian
Apr 30 '07 #12
JosAH
11,448 Expert 8TB
Does it work in the case that I describe above? I.e. You are on a PC platform running your app that reads in a text file. That text file was generated on a Mac platform and was binary copied to the PC platform. Will your app recognise the Mac eol anyway?

Personally, I use scanf a lot using a format string something like: "%[^\r\n]" (with a buffer limit stated between the % and the [ of course) to read in a line and then "%*[\r\n]" to flush out the end of line characters.

When I output the chars, I either open it up as a text file and use '\n' or open it up as a binary and use a macro to output the correct EOL symbol(s) for the platform. In the cases that I don't care if it is compatiable for the local platform, I just use '\n' in a binary format. In the cases that I do care, and don't care about it's compatiblility on another platform, I use the appropriate local representation.


Adrian
I usually don't do that either, i.e. I open a file, fgets() lines and use sscanf()
for disecting the line. Both \r and \n are treated as white space if they're left
in that line so they don't bother me.

I use that System thingie for writing formats alien to the current platform.

kind regards,

Jos
Apr 30 '07 #13
AdrianH
1,251 Expert 1GB
I usually don't do that either, i.e. I open a file, fgets() lines and use sscanf()
for disecting the line. Both \r and \n are treated as white space if they're left
in that line so they don't bother me.

I use that System thingie for writing formats alien to the current platform.

kind regards,

Jos
So you are saying even though fgets() fails to get the line correctly, your use of sscanf() will basicly ignore that fact?

That will work in a lot of cases, but I do not see it working in cases where you need to parse a line at a time for a file that was written on a different platform. Unix EOLs will actually work for all OSs (well at least between the three I mentioned) in the manner you are suggesting, but between Mac and Windoze, it is a fight to the death. ;)


Adrian
Apr 30 '07 #14
JosAH
11,448 Expert 8TB
So you are saying even though fgets() fails to get the line correctly, your use of sscanf() will basicly ignore that fact?

That will work in a lot of cases, but I do not see it working in cases where you need to parse a line at a time for a file that was written on a different platform. Unix EOLs will actually work for all OSs (well at least between the three I mentioned) in the manner you are suggesting, but between Mac and Windoze, it is a fight to the death. ;)


Adrian
I didn't know that. The MAC does \n\r doesn't it? I thought that a simple fgets()
goes as far as the \n on Windows machines. The \r would end up on the next
line just counting for a white space. Isn't that true? Or is fgets() stupid enough
to chop off the last char in its buffer unconditionally ?

kind regards,

Jos
Apr 30 '07 #15
AdrianH
1,251 Expert 1GB
I didn't know that. The MAC does \n\r doesn't it? I thought that a simple fgets()
goes as far as the \n on Windows machines. The \r would end up on the next
line just counting for a white space. Isn't that true? Or is fgets() stupid enough
to chop off the last char in its buffer unconditionally ?

kind regards,

Jos
I don't know for sure, but I do know that Windows does not recognise the Unix EOL. A perfect example of that is opening a Unix text file in notepad. It thinks it is all on one line.

Because of that, since the Mac reverses the way Window's does it, it would probably do the same thing. And if the Mac file had two consecutive EOLs, it would probably recognise it as one (the first char of the fist EOL and the last char of the last EOL would be ignored) in the Windows world.

This wasn't a big issue before. But now with all these different platforms are networked together, it can become an issue if the file is mistakenly transfered another via a binary protocol.


Adrian
Apr 30 '07 #16
weaknessforcats
9,208 Expert Mod 8TB
All of this file handling is interesting but the original question was:
I want to know what is carriage return value in C.
and that is \r.

The counting of lines of code is a separate problem.
Apr 30 '07 #17
AdrianH
1,251 Expert 1GB
All of this file handling is interesting but the original question was:


and that is \r.

The counting of lines of code is a separate problem.
Actually, even though that was the original question, that was not the intent. Hararnkhor needed a way of counting lines of code, so he/she should be aware of the problems.


Adrian
Apr 30 '07 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Canes_Rock | last post by:
The information posted at: ...
4
by: Les Juby | last post by:
Can someone please help with a suggestion as to how I can keep the formatting (carriage returns) that the user enters into a memo field and then display that later. I figured I might be able to...
4
by: Josh | last post by:
Hi, I'm using System.Data.DataSet.ReadXml to convert some xml from a webservice to a DataSet. The xml looks like: <?xml version="1.0"...
2
by: Andrew Chanter | last post by:
I have a VBA function that returns a string including "vbcr" (VB Carriage Return) to seperate a list into multiple rows, eg Item1 & vbcr & Item2 & vbcr & Item3 This works as planned in the...
2
by: eagleofjade | last post by:
I am trying to import data from a Word document into an Access table with VBA. The Word document is a form which has various fields. One of the fields is a field for notes. In some cases, this...
2
by: Matt Mercer | last post by:
Hi all, I am having a frustration problem, and I have read about 25 newsgroup postings that do not have a satisfying answer :) The problem appears to be common where carriage returns are lost...
3
by: Dinsdale | last post by:
I have an xml file that is read into an object using serialization. One of the objects has a string field called delimeter that I want to contain a carriage return. Instead of trying to include the...
6
by: shajias | last post by:
Hi, I am having a xml code like this <name> I am having a carriage return here. Second line with carriage return. This is the last line. </name>
11
by: evenlater | last post by:
My db allows the user to send email via CDO. The body of the email is determined in code. I have built an email form with To, CC and Subject lines and a large text box for the body of the message...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.