CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2009
    Posts
    2

    Angry C# File Operations

    Hi,
    I am working on a method which adds a new line in a text file.
    The line should be of this format.
    Line name (length 15, start position 0)
    Project name (length 20, start position 0 + 15)
    date (length 8, start position 0+15+20 )

    For example i just took 3 fields, in real i have to deal with more than 10 fields
    Length is the maximum length. If the length is less then it adds spaces. if the length is greater than than required length then it should erase that entire line.

    Ex:
    TestLineName TestProjectName 06172009 --- This is correct line

    TestLineName TestProjectNameAgain --- this is incorrect as TestProjectNameAgain exceeds max limit. Now how can i remove only the second line?



    I am able to create a new line when all conditions are fine but when there is any exception then I am not sure how to delete that line. Can any one help me please?

    code:
    int startPosition = 0
    using (FileStream fs = new FileStream(saveToPath, FileMode.Append, FileAccess.Write))
    {

    if (Line_Name.Length > 15)
    throw new Exception();
    //and delete the current line... how to implement?
    else
    bytesToWrite = System.Text.ASCIIEncoding().GetBytes(Line_Name);

    fs.Seek(startPosition , SeekOrigin.Begin);
    fs.Write(bytesToWrite, 0, bytesToWrite.Length);


    if (Project_name.Length > 20)
    throw new Exception();
    //and delete the current line... how to implement?
    else
    bytesToWrite = System.Text.ASCIIEncoding().GetBytes(Project_name);
    fs.Seek(startPosition + 15, SeekOrigin.Begin);
    fs.Write(bytesToWrite, 0, bytesToWrite.Length);

    if (createDate.Length > 8)
    throw new Exception();
    //and delete the current line... how to implement?
    else
    bytesToWrite = System.Text.ASCIIEncoding().GetBytes(createDate);
    fs.Seek(startPosition + 35, SeekOrigin.Begin);
    fs.Write(bytesToWrite, 0, bytesToWrite.Length);

    }


    I am stuck at two things.
    1. Not able to delete the current line
    2. Is there any better way to do entire funtionality?

    Please help me.
    TIA

  2. #2
    Join Date
    Jun 2002
    Posts
    936

    Re: C# File Operations

    I think you approach the problem the wrong way. You have to use the stream class to write the file line by line then read it line by line then delete specific line that you want. The way you have it now is not the way to approach it. Try to use the following

    Code:
    using System.IO;
    
    //then use the following classes to read and write the file line by line
    StreamWriter outputFile;
    StreamReader inputFile;
    You may need to declare them different than the way I declared them above

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: C# File Operations

    Quote Originally Posted by vcstarter View Post
    I think you approach the problem the wrong way. You have to use the stream class to write the file line by line then read it line by line then delete specific line that you want. The way you have it now is not the way to approach it. Try to use the following

    Code:
    using System.IO;
    
    //then use the following classes to read and write the file line by line
    StreamWriter outputFile;
    StreamReader inputFile;
    You may need to declare them different than the way I declared them above
    As far as I am aware, you cannot modify a file in arbitrary locations. You can append to a file, or you can read the file into memory, change it, and then replace the original completely.

  4. #4
    Join Date
    Jun 2009
    Posts
    2

    Re: C# File Operations

    Quote Originally Posted by vcstarter View Post
    I think you approach the problem the wrong way. You have to use the stream class to write the file line by line then read it line by line then delete specific line that you want. The way you have it now is not the way to approach it. Try to use the following

    Code:
    using System.IO;
    
    //then use the following classes to read and write the file line by line
    StreamWriter outputFile;
    StreamReader inputFile;
    You may need to declare them different than the way I declared them above
    I cannot use the StreamWriter i guess. Its not like writing entire line at a time. I have to write the text at specified location.

    Line name (length 15, start position 0) --- I have start writing it at position 0
    Project name (length 20, start position 0 + 15) -- for this i have to start writing it at position 15
    date (length 8, start position 0+15+20 ) -- for this at position 35

    For that im trying this way and iam able to write it correctly.

    Code:
    bytesToWrite = System.Text.ASCIIEncoding().GetBytes(createDate);
    fs.Seek(startPosition + 35, SeekOrigin.Begin);
    fs.Write(bytesToWrite, 0, bytesToWrite.Length);

  5. #5
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: C# File Operations

    try this approach:

    1. read the entire file into memory. This can be done either with the StreamReader or the File.ReadAllLines method.
    2. Modify the string(s) you need to
    3. Write the entire file to the filesystem using either a StreamWriter or File.WriteAllLines...

    (vcstarter has this one figured out, I'm just saying this to strengthen his/her point)

    Some sample code might look like this:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ModifyFile
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] file = File.ReadAllLines(@"C:\MyFile.txt");
                string lineName = "line name here";
                for(int i = 0; i < file.Length; i++)
                {
                    file[i] = file[i].Insert(0, lineName.PadRight(15, ' '));
                }
                File.WriteAllLines(@"C:\MyFile.txt", file);
            }
        }
    }
    Edit: Oh, and BigEd781 said the same thing too
    It's not a bug, it's a feature!

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: C# File Operations

    Just to reiterate what was already said, but in a slightly different way. You can't easily do it the way you're trying. You have to either:

    A) Read the entire file into memory, zero the existing file by setting its length to '0', then validate each line before you write it back out.

    or B) Read the file into memory line by line and after each line is validated to be correct, write it to a temporary file. When all lines have been processed, delete the original file and rename the temporary file to the correct name.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured