Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #99460

Re: Line read from csv file with fgets has two characters before the terminating one

From Richard Heathfield <rjh@cpax.org.uk>
Newsgroups comp.lang.c
Subject Re: Line read from csv file with fgets has two characters before the terminating one
Date 2016-12-15 08:52 +0000
Organization Fix this later
Message-ID <o2tlio$og0$1@dont-email.me> (permalink)
References <457d2715-2a7a-4131-8083-637dc0919fb3@googlegroups.com> <o2r98t$cm9$1@dont-email.me> <616cc4c3-d560-4146-8f03-93e76bedf1fd@googlegroups.com>

Show all headers | View raw


On 15/12/16 07:50, Alla _ wrote:
> On Wednesday, December 14, 2016 at 2:10:52 PM UTC+3, Richard Heathfield wrote:
>> On 14/12/16 08:04, Alla _ wrote:
>>> Hello!
>>>
>>> This is not truly a C question, but I am working on a C program, and I will be
>>> grateful for your help on this matter.
>>> Please, do me a favor and read my question and explanation to the end, before
>>> answering.
>>>
>>> When reading a line from a csv file with fgets function, and then computing
>>> the length of that line, I get two unknown additional characters at the end of
>>> the line, which are not represented by "vivid" values, and are located right
>>> before the terminating '\0'.
>>>
>>> Here is the line from csv file:
>>> Interest Expense,4,3,3,1,4,
>>
>> If this is the CSV file I sent you, here is a hex dump of that part of
>> the file (offsets removed because of line length considerations):
>>
>> 70 65 72 61 74 69 6e 67  20 50 72 6f 66 69 74 2c  |perating Profit,|
>> 31 32 39 2c 31 33 36 2c  31 37 36 2c 32 33 38 2c  |129,136,176,238,|
>> 33 37 34 0a 49 6e 74 65  72 65 73 74 20 45 78 70  |374.Interest Exp|
>> 65 6e 73 65 2c 34 2c 33  2c 33 2c 31 2c 34 0a 4f  |ense,4,3,3,1,4.O|
>> 74 68 65 72 20 47 61 69  6e 73 20 61 6e 64 20 4c  |ther Gains and L|
>>
>> As you can see, there's nothing weird going on in the file. So there
>> must be something weird going on in the code.
>>
>>> And the length strlen function has returned is 29, while the line has only 28
>>> characters, including '\n', which, as I thought, would be at index 27 (= 28th
>>> position right after the last comma). And then '\0', which is not computed by strlen.
>>> Therefore I expected the length = 28. My assumptions seem to be
>>> incorrect.
>>
>> Let's see the code.
>>
>>> Same is for each line from my file.
>>> When I have converted all required characters into integers, I got two additional
>>> zeros at the end of each line.
>>
>> Let's see the code.
>>
>> Please make sure that the code you post is the code of the program that
>> is exhibiting the problem. Please make sure you don't ask any other
>> questions about that code. Just post the exact code that is showing this
>> exact problem. Only.
> I have hoped to get away without posting the code yet ) I shud posting it
> yet because I still want to reach to some point of completeness before
> posting it.

Fair enough, but if you won't show the code, we can't help you fix it.

The only thing that occurred to me was this:

You *might* get the symptoms you suggest *if* you created the CSV file 
on Windows (giving you \r\n line endings), and then either tried to open 
it as a binary stream rather than a text stream, or perhaps FTPd it to a 
Unix-like system (Linux or MacOS, say) in BINary mode (no line-end 
translation), and then opened it on that system.

That might do it. I can't think of anything else that would, except 
screwy code.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Line read from csv file with fgets has two characters before the terminating one Alla _ <modelling.data@gmail.com> - 2016-12-14 00:04 -0800
  Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-14 11:10 +0000
    Re: Line read from csv file with fgets has two characters before the terminating one Alla _ <modelling.data@gmail.com> - 2016-12-14 23:50 -0800
      Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 08:52 +0000
        Re: Line read from csv file with fgets has two characters before the terminating one Alla _ <modelling.data@gmail.com> - 2016-12-15 00:58 -0800
          Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 09:09 +0000
            Re: Line read from csv file with fgets has two characters before the terminating one Alla _ <modelling.data@gmail.com> - 2016-12-15 04:23 -0800
              Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 12:50 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-15 14:14 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 14:42 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Keith Thompson <kst-u@mib.org> - 2016-12-15 09:49 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one gazelle@shell.xmission.com (Kenny McCormack) - 2016-12-15 17:58 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-15 18:22 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Keith Thompson <kst-u@mib.org> - 2016-12-15 11:51 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-15 20:35 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Keith Thompson <kst-u@mib.org> - 2016-12-15 12:58 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-12-15 13:11 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-16 02:14 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-16 11:25 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-16 16:41 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-16 22:08 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-17 02:48 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-17 11:54 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-17 15:33 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one supercat@casperkitty.com - 2016-12-17 08:27 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-17 17:25 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-12-17 09:53 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-17 18:21 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one supercat@casperkitty.com - 2016-12-17 10:30 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-17 22:39 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-12-17 14:51 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one BartC <bc@freeuk.com> - 2016-12-17 23:12 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one supercat@casperkitty.com - 2016-12-18 08:37 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one Keith Thompson <kst-u@mib.org> - 2016-12-18 13:14 -0800
            Re: Line read from csv file with fgets has two characters before the terminating one Geoff <geoff@invalid.invalid> - 2016-12-15 15:03 -0800
              Re: Line read from csv file with fgets has two characters before the terminating one Keith Thompson <kst-u@mib.org> - 2016-12-15 15:29 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one Geoff <geoff@invalid.invalid> - 2016-12-15 20:48 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one Keith Thompson <kst-u@mib.org> - 2016-12-15 22:58 -0800
                OSX line endings (Was: Line read from csv file with fgets has two characters before the terminating one) gazelle@shell.xmission.com (Kenny McCormack) - 2016-12-16 10:22 +0000
                Re: OSX line endings (Was: Line read from csv file with fgets has two characters before the terminating one) Geoff <geoff@invalid.invalid> - 2016-12-16 07:29 -0800
                Making OSX's boot drive case-sensitve (Was: OSX line endings (Was: Line read from csv file with fgets has two characters before the terminating one)) gazelle@shell.xmission.com (Kenny McCormack) - 2016-12-17 10:06 +0000
                Re: Making OSX's boot drive case-sensitve (Was: OSX line endings (Was: Line read from csv file with fgets has two characters before the terminating one)) Geoff <geoff@invalid.invalid> - 2016-12-17 12:39 -0800
                Re: Making OSX's boot drive case-sensitve (Was: OSX line endings (Was: Line read from csv file with fgets has two characters before the terminating one)) gazelle@shell.xmission.com (Kenny McCormack) - 2016-12-18 13:54 +0000
                Re: Making OSX's boot drive case-sensitve (Was: OSX line endings (Was: Line read from csv file with fgets has two characters before the terminating one)) Geoff <geoff@invalid.invalid> - 2016-12-18 17:57 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one Geoff <geoff@invalid.invalid> - 2016-12-16 07:12 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one Keith Thompson <kst-u@mib.org> - 2016-12-16 12:02 -0800
              Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 23:29 +0000
              Re: Line read from csv file with fgets has two characters before the terminating one Alla _ <modelling.data@gmail.com> - 2016-12-16 02:37 -0800
                Re: Line read from csv file with fgets has two characters before the terminating one Geoff <geoff@invalid.invalid> - 2016-12-16 07:51 -0800
          Re: Line read from csv file with fgets has two characters before the terminating one Manfred <noname@invalid.add> - 2016-12-15 12:22 +0100
            Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 11:29 +0000
              Re: Line read from csv file with fgets has two characters before the terminating one Melzzzzz <Melzzzzz@zzzzz.com> - 2016-12-15 12:40 +0100
                Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 12:00 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one Melzzzzz <mel@zzzzz.com> - 2016-12-15 13:09 +0100
                Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 12:43 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-15 09:18 -0500
                Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 14:49 +0000
                Re: Line read from csv file with fgets has two characters before the terminating one "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-15 10:34 -0500
              Re: Line read from csv file with fgets has two characters before the terminating one Manfred <noname@invalid.add> - 2016-12-15 16:34 +0100
    Re: Line read from csv file with fgets has two characters before the terminating one Alla _ <modelling.data@gmail.com> - 2016-12-15 00:22 -0800
      Re: Line read from csv file with fgets has two characters before the terminating one Richard Heathfield <rjh@cpax.org.uk> - 2016-12-15 09:04 +0000
  Re: Line read from csv file with fgets has two characters before the terminating one mark.bluemel@gmail.com - 2016-12-14 03:38 -0800

csiph-web