I want to create a comma seperated file of columns with specific length. How can i specify the length of each column and may be also define the type of the data.
This is just an example of how u could do it. This example is using a pure text writer, so all data (like numbers) can be readed when u open the datafile in notepad or something. Best practice is using the binary writer i think, but then u need to convert the data into bytes first
As u will see this example opens the file, writes the number of record, writes the colum name, numeric value of the column type (in this case 1 = 32bit integer), writes the second column name, writes the column type, writes the string size (only needed cause its a fixed with string) then starts writing all records.
System.IO.
StreamWriter file = new System.IO.StreamWriter("db.dat");
// Write the number of records file.Write(m_records.Count);
// Write the record layout file.Write("ID"); // column name file.Write(1); // int value file.Write("Name"); // column name file.Write(2); // string value file.Write(20); // sting length
foreach (Record record in m_records) { file.Write(record.ID); file.Write(record.Name.PadLeft(20, ' ')); } file.Flush(); file.Close();
There might be a better way to do this, but i'm used to do stuff like this in c++ and c# is totally different for this.
Yes, but as i readed the post he wants to write data using fixed length, so that aint csv no more. When using fixed length data a breakline aint required.
a CSV file is not supposed to be fixed sized, thats why the sperator is in it .csv is a standard file type used by many programs, so its not adviced to change the content structure of the file. Storing datatype / size of your fields is also not part of the csv file. What u need / want is a custom file format.
What u can do is store a header containing the layout of the file, store the column headers (if needed) then store the data. If u do this, dont store the file as .csv or other programs trying to open the file will give errors!
For a custom file format like this, try ... Make a enumeration of the field types u have like String = 0, Integer = 1, ....
At the beginning of your custom fileformat its recommed to store the version of the file (in case u make changes the reader/writer must be same version to read/write the file)
So lets say u start with file version 1.0
1. Store the file version 2. Store the number of columns 3. Store the numeric value of the column type and the size of the column for each colum (integers can have size 0, cause they are allready fixed size) 4. Store your column headers 5. Store each record using the layout u specified.
When u read the file back into the program ... 1. Read the file version and compare it to your reader 2. Get the number of colums 3. Setup your dataset or something using the layout information from the file 4. Setup your column header readed from the file 5. Fill your data from the file, using the layout u specified.
To have fixed with text fields, try this:
string value = "test"; value = value.PadRight(20, ' ');
This will fill the string with spaces to be 20 character long
A small issue with Nightmare_BE's sample is that this doesn't append a new line at the end of each record. The following sample would do this:
using (System.IO.StreamWriter writer = new System.IO.StreamWriter("data.csv")) { foreach (Record record in Records) { string line = string.Join(",", record.Values); writer.WriteLine(line); } }
CSV Data
Luo Cao
regards
swingme
Ninerh
This is just an example of how u could do it.
This example is using a pure text writer, so all data (like numbers) can be readed when u open the datafile in notepad or something. Best practice is using the binary writer i think, but then u need to convert the data into bytes first
As u will see this example opens the file, writes the number of record, writes the colum name, numeric value of the column type (in this case 1 = 32bit integer), writes the second column name, writes the column type, writes the string size (only needed cause its a fixed with string) then starts writing all records.
System.IO.
StreamWriter file = new System.IO.StreamWriter("db.dat");// Write the number of records
file.Write(m_records.Count); // Write the record layout
file.Write("ID"); // column name
file.Write(1); // int value
file.Write("Name"); // column name
file.Write(2); // string value
file.Write(20); // sting length
foreach (Record record in m_records)
{
file.Write(record.ID);
file.Write(record.Name.PadLeft(20, ' '));
}
file.Flush();
file.Close();
There might be a better way to do this, but i'm used to do stuff like this in c++ and c# is totally different for this.
Anarchy
Yes, but as i readed the post he wants to write data using fixed length, so that aint csv no more. When using fixed length data a breakline aint required.
Tryin2Bgood
a CSV file is not supposed to be fixed sized, thats why the sperator is in it
.csv is a standard file type used by many programs, so its not adviced to change the content structure of the file. Storing datatype / size of your fields is also not part of the csv file. What u need / want is a custom file format.
What u can do is store a header containing the layout of the file, store the column headers (if needed) then store the data. If u do this, dont store the file as .csv or other programs trying to open the file will give errors!
For a custom file format like this, try ...
Make a enumeration of the field types u have like String = 0, Integer = 1, ....
At the beginning of your custom fileformat its recommed to store the version of the file (in case u make changes the reader/writer must be same version to read/write the file)
So lets say u start with file version 1.0
1. Store the file version
2. Store the number of columns
3. Store the numeric value of the column type and the size of the column for each colum (integers can have size 0, cause they are allready fixed size)
4. Store your column headers
5. Store each record using the layout u specified.
When u read the file back into the program ...
1. Read the file version and compare it to your reader
2. Get the number of colums
3. Setup your dataset or something using the layout information from the file
4. Setup your column header readed from the file
5. Fill your data from the file, using the layout u specified.
To have fixed with text fields, try this:
string value = "test";
value = value.PadRight(20, ' ');
This will fill the string with spaces to be 20 character long
Dylan Smith
A small issue with Nightmare_BE's sample is that this doesn't append a new line at the end of each record. The following sample would do this:
using (System.IO.StreamWriter writer = new System.IO.StreamWriter("data.csv")) {
foreach (Record record in Records) {
string line = string.Join(",", record.Values);
writer.WriteLine(line);
}
}