# Strange behaviour of read work file

**URL:** <https://techcommunity.softwareag.com/t/strange-behaviour-of-read-work-file/202142>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-on-Linux\
**Created:** [December 17, 2018, 11:20am UTC](https://techcommunity.softwareag.com/t/strange-behaviour-of-read-work-file/202142 "2018-12-17T11:20:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [December 17, 2018, 11:20am UTC](https://techcommunity.softwareag.com/t/strange-behaviour-of-read-work-file/202142/1 "2018-12-17T11:20:25Z")

</div>

Hello all,

I got a csv-file from a foreign system and natural seems to read wrong values out of it. OK, I got a workaround for it. But I didn’t expect that behaviour.

Reproduceable sample :

```auto
define data local
1 #tab (A1/1:7)
end-define
define work file 1 '/tmp/test.csv' /* type 'csv'
write work file 1 variable 'A' 'B' 'C' 'D' 'E' 'F'
write work file 1 variable '1' '2' '3'
write work file 1 variable 'z' 'y' 'x' 'w'
write work file 1 variable 'a' 'b' 'c'
close work file 1
read work file 1 #tab(*)
write #tab(*)
end-work
end
```

Output:

```auto

A B C D E F
1 2 3 D E F
z y x w E F
a b c w E F
```

… a trap for the unwary. Since there is no “read work … variable” my workaround is:

```auto

read work file 1 #TAB(*) giving length #csvfields
if #csvfields < *UBOUND(#TAB)
add 1 to #csvfields
reset #tab(#csvfields:7)
end-if
```

Regards

matthias

---

<div class="post-metadata">

**Author:** ![James\_Gilpin1](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/james_gilpin1/32/152_2.png) [@James\_Gilpin1](https://techcommunity.softwareag.com/u/James_Gilpin1)\
**Post date:** [December 17, 2018, 1:50pm UTC](https://techcommunity.softwareag.com/t/strange-behaviour-of-read-work-file/202142/2 "2018-12-17T13:50:51Z")

</div>

This may not be the same issue… but you need to reset the array, as you are not reading values into all the fields each time

define data local  
1 #tab (A1/1:7)  
end-define  
define work file 1 ‘/tmp/test.csv’ /\* type ‘csv’  
write work file 1 variable ‘A’ ‘B’ ‘C’ ‘D’ ‘E’ ‘F’  
write work file 1 variable ‘1’ ‘2’ ‘3’  
write work file 1 variable ‘z’ ‘y’ ‘x’ ‘w’  
write work file 1 variable ‘a’ ‘b’ ‘c’  
close work file 1  
reset #tab(_) /_ reset array… do not wish to have OLD data  
read work file 1 #tab(_)  
write #tab(_)  
reset #tab(_) /_ reset array… do not wish to have OLD data  
end-work  
end

---

<div class="post-metadata">

**Author:** ![Ralph\_Zbrog](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/ralph_zbrog/32/78_2.png) [@Ralph\_Zbrog](https://techcommunity.softwareag.com/u/Ralph_Zbrog)\
**Post date:** [December 17, 2018, 5:26pm UTC](https://techcommunity.softwareag.com/t/strange-behaviour-of-read-work-file/202142/3 "2018-12-17T17:26:12Z")

</div>

The behavior is documented, but I’ve never experienced it because I always using a single WRITE WORK for my detail records, so all detail records have the same number of fields.  
The SELECT option is the default for a READ WORK.

> [@](#):
>
> If a record does not fill all fields specified in the SELECT option, the following applies:  
> ?For a field which is only partially filled, the section which has not been filled is reset to blanks or zeros.  
> ?Fields which are not filled at all still have the contents they had before.

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [December 18, 2018, 12:48pm UTC](https://techcommunity.softwareag.com/t/strange-behaviour-of-read-work-file/202142/4 "2018-12-18T12:48:06Z")

</div>

Thanks for answering.

@James Gilpin: Yes, it is the same issue and your RESET is a valid Workaround, too. I prefer to do a RESET at the beginning of the loop - just in case there are some ESCAPE TOPs in the following code.

@Ralph Zbrog: In my case, a non-Natural-Application writes the workfile… So I wasn’t aware of the individual length of the records.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 3:41pm UTC](https://techcommunity.softwareag.com/t/strange-behaviour-of-read-work-file/202142/5 "2021-04-02T15:41:59Z")

</div>


