473,326 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

what is wrong the following code?

I have following code, if you cut and paste it in VS and compile it
you see only an answer of 0. I was expecting values from o through
2999

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using System.IO;
using System.Collections;

namespace ResourceTester
{
class Program
{
static void Main(string[] args)
{
//File.Delete("my.resources");
//FileStream fs = new FileStream("my.resources",
FileMode.CreateNew, FileAccess.Write, FileShare.Write, 1,
FileOptions.None);
//ResourceWriter rw = new ResourceWriter(fs);
//for (int i = 1; i < int.MaxValue; i++)
//{
// Console.WriteLine(i.ToString());
// rw.AddResource(i.ToString(),
"XXXXXXXXXXXXXXXXXXXXXXXXXXX" + i.ToString());
//}
//rw.Generate();
//rw.Close();
//fs.Dispose();
//fs.Close();

FileStream fs = new FileStream("my.resources",
FileMode.Create, FileAccess.Write);
IResourceWriter rw = new ResourceWriter(fs);

for(int i=0;i<3000;i++)
{
rw.AddResource(i.ToString(),
"VVVVVVVVVVVVVVVVVVVVVVVVVVV " + i.ToString());

if (i % 1000 == 0)
{
//generate and free resource writer<br>
rw.Generate();
rw.Close();

//free stream<br>
fs.Dispose();
fs.Close();

//reload file<br>
fs = new FileStream("my.resources",
FileMode.Append, FileAccess.Write);
rw = new ResourceWriter(fs);
}
}
//generate and free resource writer<br>
rw.Generate();
rw.Close();

//free stream<br>
fs.Dispose();
fs.Close();

FileStream fsa = new FileStream("my.resources",
FileMode.Open, FileAccess.Read);
IResourceReader rd = new ResourceReader(fsa);
IDictionaryEnumerator dict = rd.GetEnumerator();
while (dict.MoveNext())
{
Console.WriteLine(dict.Key);
}
}
}
}

Any help would be appreciated.

What I am trying to achive is to create a resource file of 3000
numbers, well I could very well directly write the data to the
resource, in reality I want to write all the way up to int.MaxValue.
But when I write, I run into 'Out of Memory' exception. So based on
the suggestion I was told to close and append the file stream, when I
do that at the end I can see the file size is increasing but when you
read the data, only the first file flush data is available.

Thanks.
Jun 28 '08 #1
6 1938
CSharper wrote:
I have following code, if you cut and paste it in VS and compile it
you see only an answer of 0. I was expecting values from o through
2999
<snip>
What I am trying to achive is to create a resource file of 3000
numbers, well I could very well directly write the data to the
resource, in reality I want to write all the way up to int.MaxValue.
But when I write, I run into 'Out of Memory' exception. So based on
the suggestion I was told to close and append the file stream, when I
do that at the end I can see the file size is increasing but when you
read the data, only the first file flush data is available.
I'm speculating here, but it's probably because the resource file has its
own "end of resources" marker embedded, so any data you append is simply
ignored. The only way to achieve this is to write the resources file in one
go, and you apparently can't do that because of the memory restriction.

Conceivably you could try to reverse-engineer or look up the format to
figure out a way to append the data without hitting this issue, but a far
more productive course of action would be to ask why on earth you want to
write 2,147,483,647 resource entries to a file. There's no problem with
having individual resources that are big, but how would you expect to make
effective use of that many different entries?

There is no requirement to use resource files to store data. If you don't
need it to be localizable, you're better off using your own files for very
particular data needs.

--
J.
Jun 28 '08 #2
On Jun 28, 11:40*am, Jeroen Mostert <jmost...@xs4all.nlwrote:
CSharper wrote:
I have following code, if you cut and paste it in VS and compile it
you see only an answer of 0. I was expecting values from o through
2999

<snip>
What I am trying to achive is to create a resource file of 3000
numbers, well I could very well directly write the data to the
resource, in reality I want to write all the way up to int.MaxValue.
But when I write, I run into 'Out of Memory' exception. So based on
the suggestion I was told to close and append the file stream, when I
do that at the end I can see the file size is increasing but when you
read the data, only the first file flush data is available.

I'm speculating here, but it's probably because the resource file has its
own "end of resources" marker embedded, so any data you append is simply
ignored. The only way to achieve this is to write the resources file in one
go, and you apparently can't do that because of the memory restriction.

Conceivably you could try to reverse-engineer or look up the format to
figure out a way to append the data without hitting this issue, but a far
more productive course of action would be to ask why on earth you want to
write 2,147,483,647 resource entries to a file. There's no problem with
having individual resources that are big, but how would you expect to make
effective use of that many different entries?

There is no requirement to use resource files to store data. If you don't
need it to be localizable, you're better off using your own files for very
particular data needs.

--
J.
thank you one reason I want to use the resource to use ILMERGE to
combine all the program, dll and resource files (required informaation
for the program inside the resource file) into one exe file. so I have
two questions
what can I use alternative to resource file to use as a resource in
ilmerge
or does anyone know what is the format or resource file so that I can
manually add?

Thanks.
Jun 28 '08 #3
CSharper wrote:
one reason I want to use the resource to use ILMERGE to
combine all the program, dll and resource files (required informaation
for the program inside the resource file) into one exe file.
I'm not familiar with ILMerge so I couldn't tell you how to use it in this
particular scenario, but I will say that you can compile resources into
separate satellite assemblies, which you could then conceivably merge with
ILMerge. See http://msdn.microsoft.com/library/21a15yht for more information
on that. Whether the program would still be able to find the resources that
are merged this way is another matter.
so I have two questions what can I use alternative to resource file to
use as a resource in ilmerge or does anyone know what is the format or
resource file so that I can manually add?
I wouldn't try manipulating .resources files manually; there's a reason the
framework offers a separate class for reading and writing them.

If you need more control over resource generation, you should use the .resx
format instead, since that's XML. You can compile .resx files into
..resources for embedding into assemblies with resgen.

--
J.
Jun 28 '08 #4
On Jun 28, 6:00*pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
CSharper wrote:
one reason I want to use the resource to use ILMERGE to
combine all the program, dll and resource files (required informaation
for the program inside the resource file) into one exe file.

I'm not familiar with ILMerge so I couldn't tell you how to use it in this
particular scenario, but I will say that you can compile resources into
separate satellite assemblies, which you could then conceivably merge with
ILMerge. Seehttp://msdn.microsoft.com/library/21a15yhtfor more information
on that. Whether the program would still be able to find the resources that
are merged this way is another matter.
so I have two questions what can I use alternative to resource file to
use as a resource in ilmerge or does anyone know what is the format or
resource file so that I can manually add?

I wouldn't try manipulating .resources files manually; there's a reason the
framework offers a separate class for reading and writing them.

If you need more control over resource generation, you should use the .resx
format instead, since that's XML. You can compile .resx files into
.resources for embedding into assemblies with resgen.

--
J.
resx format, do you think, I will able to append data to it? I can't
find the constructor for it in C# 3.0, I thought it was
ResXResourceWriter???
Jun 29 '08 #5
DBC User wrote:
resx format, do you think, I will able to append data to it?
It's XML, so you can't just append at the end -- but since XML is an open
format, you can rewrite these files in any number of ways that don't run
into memory problems. You can't do the same for .resources files because you
don't know the format.
I can't find the constructor for it in C# 3.0, I thought it was
ResXResourceWriter???
It's in System.Windows.Forms, but the class will probably not be of much use
to you, since it cannot be used to append data any more than ResourceWriter
can. What you can do is stream the XML data in and write it out to a new
file until you hit the end, then write out your new resources. This requires
no additional memory.

Please note that if all you want to do is merge resource files and you don't
actually have a memory problem (only the pathological one you created
yourself by going for MaxInt entries) then the problem is much more cleanly
solved by using ResourceWriter or ResXResourceWriter. Read the resources
from one file, read the resources from the second file, add the resources
from the second file to the resources of the first file, write out the new
resource file. It seems unlikely to me that you'd run into memory problems
unless the resource files were already very large to begin with, but if
that's the case then the merged resource file would probably not be loadable.

--
J.
Jun 29 '08 #6
On Jun 29, 3:16*am, Jeroen Mostert <jmost...@xs4all.nlwrote:
DBC User wrote:
resx format, do you think, I will able to append data to it?

It's XML, so you can't just append at the end -- but since XML is an open
format, you can rewrite these files in any number of ways that don't run
into memory problems. You can't do the same for .resources files because you
don't know the format.
I can't find the constructor for it in C# 3.0, I thought it was
ResXResourceWriter???

It's in System.Windows.Forms, but the class will probably not be of much use
to you, since it cannot be used to append data any more than ResourceWriter
can. What you can do is stream the XML data in and write it out to a new
file until you hit the end, then write out your new resources. This requires
no additional memory.

Please note that if all you want to do is merge resource files and you don't
actually have a memory problem (only the pathological one you created
yourself by going for MaxInt entries) then the problem is much more cleanly
solved by using ResourceWriter or ResXResourceWriter. Read the resources
from one file, read the resources from the second file, add the resources
from the second file to the resources of the first file, write out the new
resource file. It seems unlikely to me that you'd run into memory problems
unless the resource files were already very large to begin with, but if
that's the case then the merged resource file would probably not be loadable.

--
J.
Thanks, I went back to resources and instead of creating single
resource files, I have created multiple resource files and during the
lookup, if I don't find it in one resource I switch to another
resource and it works with one caviat. I ran csc to build the project
with all these resources and it compiled and created the exe file. At
the end, I ran ILMerge to build one exe file with other dls I use in
the project, ILMerge fails with Out Of Memory. Now I need to find out,
if there is an alternate way to include the depened dlls in a single
exe using csc/al.exe
Jun 30 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
10
by: Greener | last post by:
Hi, I need help badly. Can you do client-side programming instead of server-side to capture the Browser type info? If this is the case, what's wrong with the following? <script...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
17
by: Paul | last post by:
HI! I get an error with this code. <SCRIPT language="JavaScript"> If (ifp==""){ ifp="default.htm"} //--></SCRIPT> Basicly I want my iframe to have a default page if the user enters in...
56
by: Cherrish Vaidiyan | last post by:
Frinds, Hope everyone is doing fine.i feel pointers to be the most toughest part in C. i have just completed learning pointers & arrays related portions. I need to attend technical interview on...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
3
by: Terry Olsen | last post by:
I've got 2 different web pages, both updating the same SQL database. One is for the Technician and one is for the Manager. The technician's update page works fine but the Manager's update page...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
7
by: mosfet | last post by:
HI, when trying to compile an embedded version of STL called ustl on win32 platform I get the following error : /// Returns the minimum of \p a and \p b template <typename T1, typename T2>...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.