473,806 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fgets()/strlen unix/DOS portability question

I'm doing some stuff at home that I can't justify doing on work time,
mostly to learn C programming. But it might possibly be useful at work.
I run linux at home at home and Windows at work. One portability
question arises.

Given fgets() on a file...

linux file with a line with 22 columns of data plus CHR(10), strlen()
of the resulting string = 23

DOS file with a line with 22 columns of data plus CHR(13) + CHR(10),
strlen() of the resulting string = 23 ??? (I'm not certain.) Is this
platform-specific or does ANSI C address this situation ?

If absolutely necessary, I suppose my program could...
- open a scratch file
- fprintf(scratch _file, "Hello world.\n")
- close the scratch file
- re-open the scratch file
- gets and take strlen() of the result.
- close the scratch file

And finally use the count of bytes in the string to adjust some
variables (bleagh).

--
Walter Dnes; my email address is *ALMOST* like wz*******@waltd nes.org
Delete the "z" to get my real address. If that gets blocked, follow
the instructions at the end of the 550 message.
Nov 14 '05 #1
3 2777
"Walter Dnes (delete the 'z' to get my real address)"
<wz*******@walt dnes.org> wrote:
Given fgets() on a file...

linux file with a line with 22 columns of data plus CHR(10), strlen()
of the resulting string = 23

DOS file with a line with 22 columns of data plus CHR(13) + CHR(10),
strlen() of the resulting string = 23 ??? (I'm not certain.) Is this
platform-specific or does ANSI C address this situation ?


If you open a file using a text stream (which is the default), C
translates all line endings to a single \n, regardless how it is
represented by the OS. It doesn't matter whether the file on disk
contains \n, \n\r, \r\n, \r, or even byte counts _before_ each line,
when you read a line through a text stream it appears to your program as
a sequence of characters followed by a single \n.
This is, of course, very useful for writing portable programs, since all
unportable, system-specific line-ending and file-padding bits are
handled by the implementation, not by you. Sometimes it is useful to
read raw bytes even for a text file; if you want that, use a binary
stream.

Richard
Nov 14 '05 #2
in comp.lang.c i read:
Sometimes it is useful to read raw bytes even for a text file; if you want
that, use a binary stream.


though doing so is not required to work.

--
a signature
Nov 14 '05 #3
On Tue, 01 Jun 2004 11:16:06 GMT, Richard Bos, <rl*@hoekstra-uitgeverij.nl> wrote:
If you open a file using a text stream (which is the default), C
translates all line endings to a single \n, regardless how it is
represented by the OS.


Thanks for the answer. Also, thanks for the answer I wanted to hear.

--
Walter Dnes; my email address is *ALMOST* like wz*******@waltd nes.org
Delete the "z" to get my real address. If that gets blocked, follow
the instructions at the end of the 550 message.
Nov 14 '05 #4

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

Similar topics

5
7252
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files, two aspects which I want to incorporate into my program eventually. That aside, my most pressing problem right now is how to get rid of the newline in the input when I use fgets(). Now I have looked around on the net, not so much in this group...
2
2348
by: Diego | last post by:
Hi, Using gcc 2.96 This message was suggested by a thread started by Knak on 21/03/04 The question is: When I run the following code, if I want to introduce a second pile of data, the fgets is ignored. I've been even tempted to use gets. Please,
32
3905
by: FireHead | last post by:
Hello C World & Fanatics I am trying replace fgets and provide a equavivalant function of BufferedInputReader::readLine. I am calling this readLine function as get_Stream. In the line 4 where default_buffer_length is changed from 4 --24 code works fine. But on the same line if I change the value of default_buffer_length from 4 --10 and I get a memory error. And if the change the value of the same variable from 4 --1024 bytes;
20
11026
by: Xavoux | last post by:
Hello all... I can't remind which function to use for safe inputs... gets, fgets, scanf leads to buffer overflow... i compiled that code with gcc version 2.95.2, on windows 2000 char tmp0 = "ABCDEFGHI\0"; char buff; /* Input buffer. */ char tmp1 = "ABCDEFGHI\0";
285
8996
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/ specs gcc version 2.95.3 20010315 (release)
21
3503
by: J de Boyne Pollard | last post by:
HSfgets() is standard in C file I/O. HS> HSThe only issue you need to pay attention too, is RAW HS(binary) vs COOK mode. It will relate the EOL (end HSof line) definitions of MS-DOS (<CRL><LR>) vs HSUnix (<LF>). Depending on your application that HSmay or may no pertain. TRThis is a handy definition, but it is NOT CORRECT. TRThe raw vs cooked distinction in Unix is VERY different
3
1647
by: FireHead | last post by:
Hello comp.lang.c Couple of years ago I created a topic as to how I can use the function fgets so that I can read a line of text from a source of data. After a long time I have finally got all of my initial questions answered accordingly. The code below is hereby allowed to be used anybody and modified without any legal strings attached... Simple.
26
2283
by: Bill Cunningham | last post by:
I was talking with someone about fgets and he said that fgets puts the \n in a string but not \0. I decided to test this assumption because my documentation didn't say if fgets put \0 after a string literal. Strlen was what I used to decide the \0 was not added. How can it be added for a string? My code. int main (void) { char input; fgets (input, sizeof(input), stdin); printf ("%i\n", strlen(input));
12
1870
by: Krumble Bunk | last post by:
Hi all, Having some trouble with a seemingly-simple task. Need to have a basic CLI functionality as per: prompt <- \n prompt <- \n promptquit <- should quit as per my snippet, but doesn't. I've thrown a few random fflush()'s in, but no joy. I have never
0
9719
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9598
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10623
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10111
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9192
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3852
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.