Hi,
I create a byte array and then copy a string (256 bytes) and two integers (using BitConverter::GetBytes()) into it. I use File::WriteAllBytes() to save it to disk. Then I read it with File::ReadAllBytes(). If I write and read immediately following each other, everything works properly. However, if I write, then do some other stuff, then read, the string comes out ok, but the integers are not correct. Any ideas? Thanks.
Vinoj
17 1564
That would tend to suggest that the problem lies with whatever "stuff" you are doing in between the write and read. What does this other stuff involve?
The interesting thing is that the other stuff only involves reading the file (it's a configuration file) and running operations on what it read. There are no write operations. If I kill the program and read the config file again, it reads correctly (indicating that the file hasn't changed). It's only within a single instance of the program that it acts up. Thanks.
Okay. Still, without a code sample trying to diagnose the problem is pretty much guesswork.
Do you know if there's some Endian conversion that I should be concerned with with the GetBytes() or ReadAllBytes() methods? I'm on a Intel platform.
No, not as far as I know. The encoding used when reading and writing is important but I don't think that's your problem. You've already stated that if you read the bytes straight after you've written them then the result is what you'd expect. It's only when you do this 'other stuff' that problems occur. Unfortunately you don't say what the other stuff you are doing is, so I can't really help you.
Sorry, I'm not trying to vague, I was trying not to innundate the thread with a bunch of code. The other "stuff" is setting the parameters of a form with the info from the config file, then opening the form, displaying information, then closing the form. The setup of the program is a form (Form1) with a config button and the "stuff" button. If I click the config button right after startup, it works (reads the config file properly). When I click "stuff", it opens the file (properly) and displays the info needed. When I exit the form created by "stuff", it goes back to Form1. Then if I click the config button or "stuff", the read is incorrect. It changes both integers to 0x2 consistently.
Here are my save and open routines: - static void SaveParams(String ^path, int pan, int report)
-
{
-
-
array<unsigned char>^ dataArray = gcnew array<unsigned char>(MAX_FILENAME+sizeof(int) + sizeof(int));
-
//copy file path
-
if(path[path->Length-1] != '\\')
-
path += "\\";
-
//TilePath = TilePath->Replace("\\", "\\\\");
-
Encoding^ charBytes = Encoding::ASCII;
-
Buffer::BlockCopy(charBytes->GetBytes(path), 0, dataArray, 0, path->Length);
-
Buffer::BlockCopy(BitConverter::GetBytes(pan), 0, dataArray, MAX_FILENAME, sizeof(int));
-
Buffer::BlockCopy(BitConverter::GetBytes(report), 0, dataArray, MAX_FILENAME + sizeof(int), sizeof(int));
-
try{
-
File::WriteAllBytes(".\\wiips.cfg", dataArray);
-
}
-
catch(Exception ^e){
-
e->Message;
-
}
-
}
-
-
static String^ OpenParams(int *pan, int *report)
-
{
-
String ^path;
-
try{
-
array<unsigned char>^ dataArray = File::ReadAllBytes(".\\wiips.cfg");
-
Encoding^ charBytes = Encoding::ASCII;
-
path = charBytes->GetString(dataArray, 0, MAX_FILENAME);
-
//removes null values from string
-
path = path->Remove(path->IndexOf(0));
-
-
*pan = BitConverter::ToInt32(dataArray, MAX_FILENAME);
-
*report = BitConverter::ToInt32(dataArray, MAX_FILENAME + sizeof(int));
-
}
-
catch(Exception ^e){
-
e->Message;
-
}
-
return path;
-
}
I've checked the code and I only call them and access the file when I intend to. Hope this helps. Thanks for your time.
Well, I can't really see anything drastically wrong with the code you've posted, but then you did say you've already tested the save and open methods by running one after the other, with expected results.
The only thing I can say is that the second time you click the button the load can't be working properly and it's probably got something to do with the elusive 'stuff' method, based on the fact that if you restart program the read will work fine once again.
I'd add some breakpoints and step through the code the second time you click the button, checking the variable contents within the watch window of VS to try and track down exactly where this problem is occurring.
Do you know if there's a way to flush any buffers or stacks in VS? I'm using static methods to open/save the file so all the variables are local. On the open, the variables are initialized to zero. Also on the open, the data read from the disk is incorrect [string -ok][int - wrong][int-wrong]. Real bizarre.
Okay, I would forget about flushing buffers or anything like that because I think that's a red herring.
First off, I would do something with the exceptions that might be thrown during those Open and Save methods. I don't use C++ all the time myself, but it looks like you're doing nothing with the exceptions that are caught. So if exceptions are being thrown during those blocks, you're not going to be aware of it - instead you'll just end up with bizarre behaviour like you're getting at the moment.
I think this line from the Open method looks suspect: -
//removes null values from string
-
path = path->Remove(path->IndexOf(0));
-
If IndexOf(0) can't be found then this will throw an exception which means the next two lines that read the numbers from the file, won't execute. That could lead to the behaviour you're describing because the numbers are not being read back from the file.
Modify your exception handlers and run your code again, see what (if any) errors you catch.
It's not hitting any exceptions in my test case. I'm debugging and stepping through it and that's how I'm finding the incorrect reads. Good catch on the IndexOf() line, that would've been an error in certain situations. Any other ideas?
Well I ran a test case here in C++ using the code you provided for the save and open methods and it worked fine for me. The only thing I changed in my sample was getting rid of the dereferencing that takes place in the open method, so that the signature looked like this: -
static String^ OpenParams(int pan, int report)
-
and the assignment looked like this: -
pan = BitConverter::ToInt32(dataArray, MAX_FILENAME);
-
report = BitConverter::ToInt32(dataArray, MAX_FILENAME + sizeof(int));
-
If you can confirm that those save and open methods are running okay then the error has got to be occurring elsewhere.
vipergt023, have you got any further forward with this?
Hi nukefusion,
I've been out of the office for a few days. I haven't gotten any further. I know for sure that those two places are the only places where I'm accessing the file. I step through my form calls and I don't see any place where any problems could be caused. I'm almost out of options unless I try saving with another method, maybe using the filestream class.
Now I get something really weird. I rearranged the order of the file to have the integers first, then the string (<int><int><string>). I use the FileStream class to read the bytes in and the order of the array is <string><int><int> - and the ints are wrong. I check the file outside the program using a binary viewer and the data is correct (not swapped). How could this happen?
Again, it's hard to say without seeing your code. Did you follow my example in my previous post. I tested it again and it works, so you must be doing something extra to break it that you haven't mentioned here.
Maybe you would be better off creating a serializable class or something.
I figured out what it was. During my form load, I had an open file dialog that ended up changing the working directory in memory. I would then open a file with a relative path and it would look somewhere else. Now I have to figure out how to keep the local directory...
I'm glad you've finally found the cause of your problem. Thanks for posting back on your progress.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: VooDoo |
last post by:
Hi,
I have a Temperature sensor with internal web server that provide the data
in a text format.
When quering the ip(ttp://192.168.10.220/temp) of the sensor i get that kind
of reply:
Probe...
|
by: Nick |
last post by:
Hi !
I want to load an old Pascal-Dos-File where
records stand in. When i view the file
in a HEX-Editor it's clear how to acces these
Strings and chars in that file. Since these
are old 8BIT...
|
by: Darsant |
last post by:
I'm currently reading 1-n number of binary files, each with 3 different
arrays of floats containing about 10,000 values a piece for a total of
about 30,000 values per file.
I'm looking for a way...
|
by: bas |
last post by:
hey,
I am having a lot of trouble trying to get this program to work
properly. It is supposed to read integers from a file and place them
into the categories for inventory. The problem seems to...
|
by: laredotornado |
last post by:
Hi,
I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed
using Apache's apxs and the php library was installed to
/usr/local/php. However, when I set my "error_reporting"...
|
by: Freddy Coal |
last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all
the file in a String.
I make this function for make that:
'--------------------------
Public Shared Function...
|
by: swetha |
last post by:
HI Every1,
I have a problem in reading a binary file.
Actually i want a C program which reads in the data from a file which
is in binary format and i want to update values in it.
The file...
|
by: efrenba |
last post by:
Hi,
I came from delphi world and now I'm doing my first steps in C++. I'm
using C++builder because its ide is like delphi although I'm trying to
avoid the vcl.
I need to insert new features...
|
by: ankitks.mital |
last post by:
Folks,
Is it possible to read hash values from txt file.
I have script which sets options. Hash table has key set to option,
and values are option values.
Way we have it, we set options in a...
|
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...
|
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: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |