Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

C++ - Perfect on Linux, saves wrong on Windows?


  • Please log in to reply
11 replies to this topic

#1 Wolfy87

Wolfy87

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:12:38 PM

Posted 16 February 2010 - 03:52 PM

I have a program I wrote in C++ and SDL, it compiles with no errors for Linux and Windows.

On Linux (Ubuntu) all is well, however on windows I have a problem.

When I click new it adds some more data to the end of the file, on Windows it erases everything then adds the line in.

As you can see, this is a problem.

The main problem seems to be with reading files.

Just in case it makes a difference for Linux it is compiled with the basic compiler that comes with build-essentials, for Windows it is compiled with MinGW32 (Cross-Compiler).

If anyone has any idea please let me know because I haven't a clue, same code, no errors, works on one OS, sounds like it should be fine to me.

Thanks, Wolf87.

BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:05:38 AM

Posted 16 February 2010 - 04:06 PM

1. What is SDL?
2. Can you post an example of what you're referring to?
3. How is the file being opened?

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:12:38 PM

Posted 16 February 2010 - 06:07 PM

SDL, or simple direct media layer, is a graphics library for C/C++, it is primarily used for 2D games.

I am not sure what you had in mind for an example, the project is 1300+ lines but I will try to pick out the parts which involve making a new file tomurro, I dont have access to my PC at the moment.

And I am using ifstream and ofstream from fstream.h.

Edited by Wolfy87, 16 February 2010 - 06:09 PM.


#4 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:05:38 AM

Posted 16 February 2010 - 07:05 PM

Hmmm.. if you're using fstream, the only thing I could think of would be that you have to open the file with the std::ios::bin flag, otherwise things like line endings will get translated.

Oh, one thing: <fstream.h> does not exist, according to the standard. <fstream> is the standards-compliant and portable way to write that.

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#5 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:12:38 PM

Posted 17 February 2010 - 01:11 PM

I replaced all of my opening lines with

fread.open("./data/data.dat", std::ios::binary);

It did not make a difference, it still wipes the file then puts my extra line in but only on windows.

I just don't see how it would make a difference of what OS it is running on.

I read the whole file into my data array, I remove the old part from my data array, I add my new edited part onto the end and then rewrite the file. Should be a simple process.

Thanks for your help Billy.

Edited by Wolfy87, 17 February 2010 - 01:12 PM.


#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:38 AM

Posted 17 February 2010 - 01:53 PM

I just don't see how it would make a difference of what OS it is running on.


Umm, becasue is all operating systems were the same, then we could run any software on any computer...... :thumbsup:

#7 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:12:38 PM

Posted 17 February 2010 - 05:31 PM

That did not help at all, obviously you have misunderstood me, both programs have exactly the same code but when run on different operating systems they behave differently. I am quite sure that it is not meant to happen, otherwise you may aswell have Linux and Windows versions of C++ syntax as well as its compilers.

#8 Romeo29

Romeo29

    Learning To Bleep


  • BC Advisor
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:07:38 AM

Posted 17 February 2010 - 09:42 PM

fread.open("./data/data.dat", ios::binary | ios::in); should take care of your problem. You dont have to specify std:: if you are using std namespace else specify it too.

#9 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:12:38 PM

Posted 18 February 2010 - 08:30 AM

Nope, no difference :/ still fine on Linux and the Windows version is still broken.

Is anyone else starting to think this is a bit odd?

Even when compiled on a different PC with a real compiler rather than a cross-compiler, it still makes no difference other than the file size.

I am guessing I've got you all stumped now :/

#10 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:12:38 PM

Posted 21 February 2010 - 02:40 PM

I have decided to rewrite it using the windows api so don't worry about it anymore, hopefully I wont encounter the same off problem.

Thanks for your suggestions, Wolfy87.

#11 Romeo29

Romeo29

    Learning To Bleep


  • BC Advisor
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:07:38 AM

Posted 21 February 2010 - 07:32 PM

You did not post the full code, so nobody cannot say anything. It depends on how you open a file and how you write to it. Some open modes just wipe the file. Also it depends on the file pointer.

#12 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:12:38 PM

Posted 22 February 2010 - 06:32 PM

Right, I will post a link to the project within a couple of days just incase somone wants to take a look. The way I am rewriting it dose not involve the same idea of removing the old data and adding the new part in, instead it just rewrites the whole file from the current values so it should not do it this time.

But still I am very curious as to what is causing it to only work on Linux.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users