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. 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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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,
|
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;
|
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";
|
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)
| |
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
|
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.
|
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));
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |