Text File : how to READ, WRITE or REWRITE a text file

Franco_FestiFranco_Festi Member Posts: 19
Hi guys,

i've looked up and down this forum many times, but i still haven't found what i was looking for.

I've got a text file, like this.

aaaaaaaaa
bbbbbbbbb
ccccccccc
ddddddddd
eeeeeeeee

I need to rewrite the 3rd line. Something like this :

aaaaaaaaa
bbbbbbbbb
ppp
ddddddddd
eeeeeeeee

Well. I know how to place the pointer in the right position (TEXTMODE = FALSE, file.SEEK(Position) ) but i've got problems when it comes to write my string.

My code is (example) :

linelength : integer;
index : integer;
string : text[100];

string := 'ppp';

FOR index := 1 TO LineLength
File.WRITE(string[index]);

The point is : i wanted to avoid inserting CRLF at the end of my line. It worked, but the result wasn't so good.

aaaaaaaaa
bbbbbbbbb
p p p ccccccccc (as you can see, the old line is still there)
ddddddddd
eeeeeeeee

Furthermore, openin my text file with WordPad i see that after every single char appears a strange "square symbol" (i think you know what i mean)... using NotePad it's just a space.

So... i need your help guys !
Thank you !!

Franco

Comments

  • kinekine Member Posts: 12,562
    I think there is no way how to do that. You can only copy the file under another name, read it until the point you want to change, write the new line, and continue with copying the rest of the file (skipping the replaced line).
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Franco_FestiFranco_Festi Member Posts: 19
    kine wrote:
    I think there is no way how to do that. You can only copy the file under another name, read it until the point you want to change, write the new line, and continue with copying the rest of the file (skipping the replaced line).

    Thanks for replying. The fact is i've already thought about it, and i can't do it. The text file i'm using is a shared file. A second software is using it so i can't delete/replace it.

    (by the way, i asked them..the answer wasn't what i wanted. Maybe it's just lazyness..but my hands are tied...)

    Thank you anyway.

    Franco
  • kinekine Member Posts: 12,562
    Sared file and you are changing it when another app is accessing it? I can only be sorry for you.. ](*,)

    In these days using file as place for exchanging the information is not way how to do that.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Franco_FestiFranco_Festi Member Posts: 19
    kine wrote:
    Sared file and you are changing it when another app is accessing it? I can only be sorry for you.. ](*,)

    In these days using file as place for exchanging the information is not way how to do that.

    I know! That's exactly what i told them! I feel like i'm still living in the middle age of IT! Bleah!
    :?
  • ajhvdbajhvdb Member Posts: 672
    Use the streams example from reynolds. Looks really easy:
    viewtopic.php?f=23&t=29313&p=146950#p146950
  • Franco_FestiFranco_Festi Member Posts: 19
    ajhvdb wrote:
    Use the streams example from reynolds. Looks really easy:
    viewtopic.php?f=23&t=29313&p=146950#p146950

    That example doesn't do what i need - it replaces a string you're already supposed to know -anyway, it gives me an idea.

    1. I can read the line in the position i need to replace.
    2. Save the value of this line inside a global (bigtext).
    3. Search for this line inside the file (maybe using the example in the link).
    4. Replace the target line with a new one.

    I'll try, then i'll let you know.

    Thank you :)
  • Franco_FestiFranco_Festi Member Posts: 19
    ajhvdb wrote:
    Use the streams example from reynolds. Looks really easy:
    viewtopic.php?f=23&t=29313&p=146950#p146950

    That example doesn't do what i need - it replaces a string you're already supposed to know -anyway, it gives me an idea.

    1. I can read the line in the position i need to replace.
    2. Save the value of this line inside a global (bigtext).
    3. Search for this line inside the file (maybe using the example in the link).
    4. Replace the target line with a new one.

    I'll try, then i'll let you know.

    Thank you :)

    :(
    still having the same old problem. Using two different text files the result is ok, but not good for my purpose : i must use the same file without copy, delete or rename.
    ](*,)
  • SogSog Member Posts: 1,023
    go to your position, write the del char until you meet a crlf and then write your string?
    (just went to see the ascii table, perhaps go to the end of the line and write the backspace char (8))
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • kinekine Member Posts: 12,562
    The tools you have in NAV are not able to "remove" part inside the file. You can do only something like:

    Using two File variables. One for reading (FileR), one for reading/writing (FileRW). Open same file with both. Set the FileR after the text you want to change. Set the FileRW to the position of the change, write the new part. After that read everything from FileR and write it into FileRW. After all is read from FileR, use FileRW.TRUNC to truncate rest of the file.

    You need to think about the problem in his way: you can only "overwrite" the data in the file, you cannot remove part of them. The additional part you want to "remove" must be overwritten with the rest of the file, you can only truncate the end of the file.

    But I do not know, if it will works when the replaced part is shorten than the inserted one (whn the FileR will read on modified par by FileRW).

    I hope that you catch what I tried to describe... 8)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • jordi79jordi79 Member Posts: 273
    Hi,

    Just wanted to say that this is an excellent post. I never used BigText before. But after seeing this code example, I will use it more now.
Sign In or Register to comment.