Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Text files

FosforFosfor Posts: 27Member
Hello all,

I wrote an application which uses text files. I know how to read and write into these files. But when I already have put some text in these files, I want to add a new line of text between the text that is already in these files. So I want to jump to a line and put the new line of text at this line between the other lines of text. The same counts for reading. How do I read the text from a line somewere between the other lines of text and put this line of text in a string variable?

Does anyone know how to do this?

Thanks,
Martijn

Comments

  • HackmanCHackmanC Posts: 441Member
    For reading ... Read all the file until you get the line you need. You know "Input Line".

    For writting ... Read all the file and save in a buffer until you get the line where you will put a new line, save the new line in the buffer, continue reading and buffering until EOF. Write the buffer to a new file. (You can replace the buffer with a disk file, so you dont need to write the buffer at end).

    There is an object that handle part of the work... A FileSystemObject and others but is not part of VB, is part of the OS or Office. I dont use it often.

    -------
    : Hello all,
    :
    : I wrote an application which uses text files. I know how to read and write into these files. But when I already have put some text in these files, I want to add a new line of text between the text that is already in these files. So I want to jump to a line and put the new line of text at this line between the other lines of text. The same counts for reading. How do I read the text from a line somewere between the other lines of text and put this line of text in a string variable?
    :
    : Does anyone know how to do this?
    :
    : Thanks,
    : Martijn
    :

    [red]Good luck![/red]
    [blue]Hackman[/blue]

  • FosforFosfor Posts: 27Member
    Thanks for the reply!

    But isn't there another way to do this? Because now I have to read a whole lot of lines before I get to the line witch I want to read or where I want to write (with the buffer).

    I know the FileSystemObject, I have used it several times. Why don't you use it? Do you think there's another (better, maybe faster) way to resolve this problem?

    Regards,
    Martijn


    : For reading ... Read all the file until you get the line you need. You know "Input Line".
    :
    : For writting ... Read all the file and save in a buffer until you get the line where you will put a new line, save the new line in the buffer, continue reading and buffering until EOF. Write the buffer to a new file. (You can replace the buffer with a disk file, so you dont need to write the buffer at end).
    :
    : There is an object that handle part of the work... A FileSystemObject and others but is not part of VB, is part of the OS or Office. I dont use it often.
    :
    : -------
    : : Hello all,
    : :
    : : I wrote an application which uses text files. I know how to read and write into these files. But when I already have put some text in these files, I want to add a new line of text between the text that is already in these files. So I want to jump to a line and put the new line of text at this line between the other lines of text. The same counts for reading. How do I read the text from a line somewere between the other lines of text and put this line of text in a string variable?
    : :
    : : Does anyone know how to do this?
    : :
    : : Thanks,
    : : Martijn
    : :
    :
    : [red]Good luck![/red]
    : [blue]Hackman[/blue]
    :
    :

  • infidelinfidel Posts: 2,900Member
    : But isn't there another way to do this? Because now I have to read a whole lot of lines before I get to the line witch I want to read or where I want to write (with the buffer).

    Not really. If you know ahead of time where in the file you want to go you can do some kind of Seek command to jump to that point. More than likely you're going to need to programmatically seek out a particular line, meaning you'll have to read the file one line at a time one way or another. You could technically just start writing data at any point in the file, but you'll write over whatever was at that location. You can't just insert a line into the middle of the file, you have to move everything from that point to the end forward to "make room" for the new data. It's really a pretty simple algorithm, just read lines from one file and write them to another, perhaps writing additional lines to the output file where you deem them necessary.

    : I know the FileSystemObject, I have used it several times. Why don't you use it? Do you think there's another (better, maybe faster) way to resolve this problem?

    I use it for convenience. I find its object model easier to remember than the corresponding VB commands like Open and Put/Get etc. When it comes down to it, though, you're still dealing with flat files no matter how you open them.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • FosforFosfor Posts: 27Member
    : : But isn't there another way to do this? Because now I have to read a whole lot of lines before I get to the line witch I want to read or where I want to write (with the buffer).
    :
    : Not really. If you know ahead of time where in the file you want to go you can do some kind of Seek command to jump to that point. More than likely you're going to need to programmatically seek out a particular line, meaning you'll have to read the file one line at a time one way or another. You could technically just start writing data at any point in the file, but you'll write over whatever was at that location. You can't just insert a line into the middle of the file, you have to move everything from that point to the end forward to "make room" for the new data. It's really a pretty simple algorithm, just read lines from one file and write them to another, perhaps writing additional lines to the output file where you deem them necessary.
    :
    : : I know the FileSystemObject, I have used it several times. Why don't you use it? Do you think there's another (better, maybe faster) way to resolve this problem?
    :
    : I use it for convenience. I find its object model easier to remember than the corresponding VB commands like Open and Put/Get etc. When it comes down to it, though, you're still dealing with flat files no matter how you open them.
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    : [code]
    : $ select * from users where clue > 0
    : no rows returned
    : [/code]
    :
    :

    Ok, thanks for the info. I'll try in the way you sugested.
    Regards,
    Martijn
  • DrMartenDrMarten Posts: 748Member
    If this was the PASCAL room I could have helped more.

    Anyway one way would be ReadInputFile in two loops.

    In 1st loop WriteOutputFile1 up to where you want to insert text to a new file.

    In 2nd loop WriteOutputFile2 from the end of where you want to insert text to a new file.

    Join the files WriteOutputFile1+InsertedTextFile+WriteOuputFile2 as a new file.

    You might find a DOS "JOIN" command on the net if you can call a MS-DOS action from within VB, I don't know about this.

    CMD brings up a console window if you type it in the "RUN" area from the windows START button.

    Hope this might give you an idea to write a VB join program maybe as well?

    Hope this helps. :-)

  • FosforFosfor Posts: 27Member
    : If this was the PASCAL room I could have helped more.
    :
    : Anyway one way would be ReadInputFile in two loops.
    :
    : In 1st loop WriteOutputFile1 up to where you want to insert text to a new file.
    :
    : In 2nd loop WriteOutputFile2 from the end of where you want to insert text to a new file.
    :
    : Join the files WriteOutputFile1+InsertedTextFile+WriteOuputFile2 as a new file.
    :
    : You might find a DOS "JOIN" command on the net if you can call a MS-DOS action from within VB, I don't know about this.
    :
    : CMD brings up a console window if you type it in the "RUN" area from the windows START button.
    :
    : Hope this might give you an idea to write a VB join program maybe as well?
    :
    : Hope this helps. :-)
    :
    :
    Thanks for the reply. I'll try it at a later time.
    Regards,
    Martijn
Sign In or Register to comment.