473,383 Members | 1,795 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,383 software developers and data experts.

Reading Same file produces different values

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
Jan 30 '09 #1
17 1564
nukefusion
221 Expert 100+
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?
Jan 31 '09 #2
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.
Feb 2 '09 #3
nukefusion
221 Expert 100+
Okay. Still, without a code sample trying to diagnose the problem is pretty much guesswork.
Feb 2 '09 #4
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.
Feb 2 '09 #5
nukefusion
221 Expert 100+
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.
Feb 2 '09 #6
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:

Expand|Select|Wrap|Line Numbers
  1. static void SaveParams(String ^path, int pan, int report)
  2.  {
  3.  
  4.         array<unsigned char>^ dataArray = gcnew array<unsigned char>(MAX_FILENAME+sizeof(int) + sizeof(int));
  5.         //copy file path
  6.         if(path[path->Length-1] != '\\')
  7.             path += "\\";
  8.         //TilePath = TilePath->Replace("\\", "\\\\");
  9.         Encoding^ charBytes = Encoding::ASCII;
  10.         Buffer::BlockCopy(charBytes->GetBytes(path), 0, dataArray, 0, path->Length);
  11.         Buffer::BlockCopy(BitConverter::GetBytes(pan), 0, dataArray, MAX_FILENAME, sizeof(int));
  12.         Buffer::BlockCopy(BitConverter::GetBytes(report), 0, dataArray, MAX_FILENAME + sizeof(int), sizeof(int));
  13.         try{
  14.             File::WriteAllBytes(".\\wiips.cfg", dataArray);
  15.         }
  16.         catch(Exception ^e){
  17.             e->Message;
  18.         }
  19.     }
  20.  
  21.     static String^ OpenParams(int *pan, int *report)
  22.         {
  23.             String ^path;
  24.             try{
  25.                 array<unsigned char>^ dataArray = File::ReadAllBytes(".\\wiips.cfg");
  26.                 Encoding^ charBytes = Encoding::ASCII;
  27.                 path = charBytes->GetString(dataArray, 0, MAX_FILENAME);
  28.                 //removes null values from string
  29.                 path = path->Remove(path->IndexOf(0));
  30.  
  31.                 *pan = BitConverter::ToInt32(dataArray, MAX_FILENAME);
  32.                 *report = BitConverter::ToInt32(dataArray, MAX_FILENAME + sizeof(int));
  33.             }
  34.             catch(Exception ^e){
  35.                 e->Message;
  36.             }
  37.             return path;
  38.         }
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.
Feb 2 '09 #7
nukefusion
221 Expert 100+
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.
Feb 2 '09 #8
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.
Feb 2 '09 #9
nukefusion
221 Expert 100+
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:

Expand|Select|Wrap|Line Numbers
  1. //removes null values from string
  2. path = path->Remove(path->IndexOf(0));
  3.  
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.
Feb 2 '09 #10
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?
Feb 2 '09 #11
nukefusion
221 Expert 100+
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:

Expand|Select|Wrap|Line Numbers
  1.  static String^ OpenParams(int pan, int report)
  2.  
and the assignment looked like this:
Expand|Select|Wrap|Line Numbers
  1. pan = BitConverter::ToInt32(dataArray, MAX_FILENAME);
  2. report = BitConverter::ToInt32(dataArray, MAX_FILENAME + sizeof(int));
  3.  
If you can confirm that those save and open methods are running okay then the error has got to be occurring elsewhere.
Feb 2 '09 #12
nukefusion
221 Expert 100+
vipergt023, have you got any further forward with this?
Feb 5 '09 #13
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.
Feb 5 '09 #14
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?
Feb 11 '09 #15
nukefusion
221 Expert 100+
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.
Feb 11 '09 #16
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...
Feb 24 '09 #17
nukefusion
221 Expert 100+
I'm glad you've finally found the cause of your problem. Thanks for posting back on your progress.
Feb 25 '09 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

29
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...
8
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...
8
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...
6
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...
1
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"...
11
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...
13
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...
6
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...
6
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...
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...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...

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.