Writing a new column to an existing CSV file - c#

I am programmatically creating a CSV file, and I am writing 5 columns to it. Later I want to write 1 more column to the CSV file. How do I do that?
Regards
Sanchaita

The easiest (and from what I know, the only) way of doing this is reading the contents of the CSV file, add the column programmatically, and rewrite the file.
When you try to append new content somewhere in the middle of the file (as opposed to replacing it), the file has to be rewritten to disk anyway, so you shouldn't worry about performance when employing that approach yourself. And as far as I know, this isn't supported by any API calls anyway.
On an unrelated note, I'd suggest you create a temporary file first which has all your modifications, and only replace the original file if all goes well. But that's just good programming practice.

Related

Editing specific line of text file in asp.net?

I have to change the specific line of the text file in asp.net.
Can I change/Replace the text in a particular line only??
I have used the replace function in text file but it is replacing text in entire file.
I want to replace only one line specified by me.
Waiting for the reply..
Thanks in advance..
File systems don't generally allow you to edit within a file other than directly overwriting byte-by-byte. If your text file uses the same number of bytes for every line, then you can very efficiently replace a line of text - but that's a relatively rare case these days.
It's more likely that you'll need to take one of these options:
Load the whole file into memory using File.ReadAllLines, change the relevant line, and then write it out again using File.WriteAllLines. This is inefficient in terms of memory, but really simple to code. If your file is small, it's a good option.
Open the input file and a new output file. Read a line of text at a time from the input, and either copying it to the output or writing a different line instead. Then close both files, delete the input file and rename the output file. This only requires a single line of text in memory at a time, but it's considerably more fiddly.
The second option has another benefit - you can shuffle the files around (using lots of rename steps) so that at no point do you ever have the possibility of losing the input file unless the output file is known to be complete and in the right place. That's even more complicated though.

c# Parsing a flat file and editing which conist of hex code as seperator

Hi Team,
I have a flat file with data init seperated by hexcode as Rows/columns I need to parse the file and inject an additional column with data.
e.g. EID1000ENAJohnJOBSalesMan>EID1001ENASmithJOBAnalyst> and soon.............
Assuming that in above scenario I need to inject Deptono as DEP10> what would be the best way to do this i.e. File IO has methods in c# or writing core code to achive the same, any sample\link\suggestion on this would be of gr8 help.
Well there are surely many ways to do it but I would do something like this.
Open the file for reading, and another file for writing.
Read the file line by line, compare the data to see if the record is the one you want, if it is change it and add that line to the new temp file, otherwise just copy the line to the temp file. In the end replace the old file with the new one. You will have to do this if the file is quite big, otherwise switch to a proper database like SqlLite.

What's the best structure to conserve file related information?

I am building an interface whose primary function would be to act as a file renaming tool (the underlying task here is to manually classify each file within a folder according to rules that describe their content). So far, I have implemented a customized file explorer and a preview window for the files.
I now have to find a way to inform a user if a file has already been renamed (this will show up in the file explorer's listView). The program should be able to read as well as modify that state as the files are renamed. I simply do not know what method is optimal to save this kind of information, as I am not fully used to C#'s potential yet. My initial solution involved text files, but again, I do not know if there should be only one text file for all files and folders or simply a text file per folder indicating the state of its contained items.
A colleague suggested that I use an Excel spreadsheet and then simply import the row or columns corresponding to my query. I tried to find more direct data structures, but again I would feel a lot more comfortable with some outside opinion.
So, what do you think would be the best way to store this kind of data?
PS: There are many thousands of files, all of them TIFF images, located on a remote server to which I have complete access.
I'm not sure what you're asking for, but if you simply want to keep some file's information such as name, date, size etc. you could use the FileInfo class. It is marked as serializable, so that you could easily write an array of them in an xml file by invoking the serialize method of an XmlSerializer.
I am not sure I understand you question. But what I gather you want to basically store the meta-data regarding each file. If this is the case I could make two suggestions.
Store the meta-data in a simple XML file. One XML file per folder if you have multiple folders, the XML file could be a hidden file. Then your custom application can load the file if it exists when you navigate to the folder and present the data to the user.
If you are using NTFS and you know this will always be the case, you can store the meta-data for the file in a file stream. This is not a .NET stream, but a extra stream of data that can be store and moved around with each file without impacting the actual files content. The nice thin about this is that no matter where you move the file, the meta-data will move with the file, as long as it is still on NTFS
Here is more info on the file streams
http://msdn.microsoft.com/en-us/library/aa364404(VS.85).aspx
You could create an object oriented structure and then serialize the root object to a binary file or to an XML file. You could represent just about any structure this way, so you wouldn't have to struggle with the
I do not know if there should be only one text file for all files and folders or simply a text file per folder indicating the state of its contained items.
design issues. You would just have one file containing all of the metadata that you need to store. If you want speedier opening/saving and smaller size, go with binary, and if you want something that other people could open and view and potentially write their own software against, you can use XML.
There's lots of variations on how to do this, but to get you started here is one article from a quick Google:
http://www.codeproject.com/KB/cs/objserial.aspx

How can I append text to a start and end of a file as header and footer?

I'm filling an XML document manually using C# and I need to have <data> as header and </data> as footer for the whole XML file. Is there an easy way to do that ? I know It can be done but I couldn't find a way to do it. Keep in mind that I'm updating the entries so I need to make sure that they always come between the header and the footer.
Thanks
Example
<data>
Entry
New Entry 1
New Entry 2
</data>
As for appending text to a file, this is easy just open the file in append mode and write the text.
For inserting, there is no POSIX or Windows way available to insert text. So you need to write a new file with the header, and then write the rest of the file.
I'm not sure what you are trying to do makes sense - and I agree with Brian on a lack of way to pre-pend a file without writing a new file. A better approach would be to store the Entry items (in a separate file or other storage medium) and then generate the final version as Brian stipulates.
If you are manipulating XML content generally, you might consider using Linq to XML or the native .Net XML libraries.
Well, simplest is to use these three commands in the following order. Of course, this doesn't take care of checking if you already put the tags or not. For that, you should use XML related libs.
Assume, file is ab.txt. Run these dos commands -
echo ^<data^> > new_ab.txt
type ab.txt >> new_ab.txt
echo ^<^/data^> >> new_ab.txt
:)
There are many ways to do it. Do you want to do it via C# code? It will be bit lengthy to put it here :) I'm sure you know how to read/write files via C#.
Cheers!
Assumption: you are working on Windows. For unix, commands are much easier. :)

Does XDocument.Save(string filename) resave the whole file or just changes?

Basically if I do Xdoc.Load(filename), do some changes then do Xdoc.Save(filename) does it only save things that changed such as inserted or removed elements, etc, or does it resave everything?
Depending on the answer I'm thinking of determining whether my app is going to save per-change or save on explicit save and on exit. Also considering whether to write to multiple xml files or just keep everything in one big file. I have no idea how big the one big file would be but I suspect it could potentially be 10's of MBs, so if it's resaving the entire file then I definitely can't be saving every change while keeping one big file.
If it does save the entire file, does anyone have opinions of having a separate xml file for each entity (potentially hundreds) and whether or not it's a good idea?
It saves the whole file. That is the nature of text based formats. A text file cant overwrite itself without rewriting the unchanged parts.
Yes, saving a document saves the whole document.
What's the use case for the "per change" save? Is it just in case the application crashes? If so, I suggest you save these incremental changes in a temporary directory as small files, but when the user explicitly says to save the file, save it in one big file. (That's easier to copy around etc.) Delete the temporary directory on exit.
I do wonder whether you really need the temporary directory at all though. It sounds like quite a lot of work for little benefit.

Categories