I think this is for an expert!!.. coz` no one knowes it..

I think this is for an expert!!.. coz` no one knowes it..

Can anyone tell how to insert a new line in a file

Example:      test.txt

line 1
line 2
line 3
line 4
line 6
line 7
line 8

after the insertion of the line...

line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8

i tryed to use the seek() method.. it works but he overides the other lines..

Anyone know how to do that





Answer this question

I think this is for an expert!!.. coz` no one knowes it..

  • Dehim

    You will have to rewrite the file

    Read lines from input file
    Output to temporary file
    Hit insert place, write insert line to temp file
    Continue to read from input file and write to temp file
    Finish read/write
    Delete original file
    Rename/move temp file to original file



  • Dave Wickert

    Someone mentioned SQLExpress ... another item to see is SQLite whcih is an embedded database.

    You can download SQLite http://www.sqlite.org/ there are also managed providers available http://sourceforge.net/projects/adodotnetsqlite/

    Here is a quick article on it :) http://www.eggheadcafe.com/articles/20040714.asp

    Cheers,

    Greg


  • caiman

    Have you considered SQL Server 2005 Express Edition It's free, and allows you to create databases of up to 4GB.

    -Tom Meschter
    Software Dev, Visual C# IDE



  • virayanna

    The filesystem does not know you want to insert a random number of bytes into the file. How do you expect that any part of the file/operating system will understand that you want to insert data into the file



  • S.Mark

    ya.. and how about the sizE


  • lingga

    first i wannet to use too database.. but the i found out that.. the user must have the sql server..

    and that`s not an option..


  • holzert

    hi,

    files that hold text are two types

    the file that have fixed entery point like .txt which you have to read the file from the beginning to end so if you want to edit something you have to read from the start like my code .

    there is other type have random entry point like the .doc files you can seek a particular part and edit it without reading the entire file, you can enter it from any point

    i don't know much about the second type because i didn't use it yet

    hope this helps



  • William Gates

    ya but what is the file is 300 Mb
    it will take lonk lonk ti me antil i delete and i create the file..

    isnt a possible way to change that file



  • Ben Nakagawa

    Use ADO.Net to connect to the text file and fill a dataset with it. You can make changes to the dataset (such as inserting lines at a particular index) and then save it back out as a text file.

    Here's how to connect to the file using a DSN:
    http://www.c-sharpcorner.com/database/Connect/ConnectODBCText.asp

    If you don't want to use the DSN, you can get the connection string and set up the connection like a normal database connection string (using the ODBC Text driver).


  • Shiggity

    Textfiles are not really good on that part, as you have noticed.

    I would probably try to change it to be in a SQL database instead. Much easier to work with than textfiles. If a textfile is required it can be exported from the database.



  • blettner

    i can add them.. but what if i wanna modify something int the file


  • delly_jm

    It will work, but you would use memory more efficient if you rewrite to a new file as you read it and insert the line when at the appropiate position.

    Why cant you add the new lines at the end zapacila



  • ImagineNation

    dont know thats why i`m asking..


  • Jeremy613

    loooool

    once i was trying to do something like that , here its my code but i don't think its the best thing to do in 300 mb files, path is the path of the file that you want to use, data is the data that you want to insert, linenumber is which line you to insert this data in


    public static void InsertIntoFile(string Path, string Data, int LineNumber)
    {
    StringBuilder result = new StringBuilder();
    FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.ReadWrite);
    int lines = 0;
    using (StreamReader sr = new StreamReader(fs))
    {
    string line;
    while ((line = sr.ReadLine()) != null)
    {
    if (lines == (LineNumber - 1))
    {
    result.Append(Data);
    result.Append(
    " | ");
    }
    result.Append(line);
    result.Append(
    " | ");
    lines++;
    }
    }
    string fullstr = result.ToString();
    string[] text = fullstr.Split('|');
    using (StreamWriter sw = new StreamWriter(Path))
    {
    for (int i = 0; i < text.Length; i++)
    {
    string item = textIdea.Trim();
    sw.WriteLine(item);
    Console.WriteLine(item);
    }
    }
    }


    hope this helps



  • I think this is for an expert!!.. coz` no one knowes it..