Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. BinaryWriter writes empty file

BinaryWriter writes empty file

Scheduled Pinned Locked Moved Managed C++/CLI
question
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KlaasVersteeg
    wrote on last edited by
    #1

    Hi, I'm trying to write a binary file, using BinaryWriter. My code does write a file, but it is empty. This is the code I'm using:

    	FileStream^ fs = gcnew FileStream(Convert::ToString(fileName), FileMode::Create);
    	BinaryWriter^ w = gcnew BinaryWriter(fs);
    
    	int rows = fileData->GetLength(0);
    	int cols = fileData->GetLength(1);
    	try
    	{
    		for (int row = 0; row < rows; row++){
    			for (int col = 0; col < cols; col++){
    				w->Write(fileData\[row,col\]);
    			}
    		}
    	}
    	finally
            {
    		w->Close();
    		fs->Close();
            }
    

    Does anyone have a clue why the written files is empty? Thanks!

    K L L 3 Replies Last reply
    0
    • K KlaasVersteeg

      Hi, I'm trying to write a binary file, using BinaryWriter. My code does write a file, but it is empty. This is the code I'm using:

      	FileStream^ fs = gcnew FileStream(Convert::ToString(fileName), FileMode::Create);
      	BinaryWriter^ w = gcnew BinaryWriter(fs);
      
      	int rows = fileData->GetLength(0);
      	int cols = fileData->GetLength(1);
      	try
      	{
      		for (int row = 0; row < rows; row++){
      			for (int col = 0; col < cols; col++){
      				w->Write(fileData\[row,col\]);
      			}
      		}
      	}
      	finally
              {
      		w->Close();
      		fs->Close();
              }
      

      Does anyone have a clue why the written files is empty? Thanks!

      K Offline
      K Offline
      KlaasVersteeg
      wrote on last edited by
      #2

      I forgot to mention that fileData is an array<String^,2>^

      1 Reply Last reply
      0
      • K KlaasVersteeg

        Hi, I'm trying to write a binary file, using BinaryWriter. My code does write a file, but it is empty. This is the code I'm using:

        	FileStream^ fs = gcnew FileStream(Convert::ToString(fileName), FileMode::Create);
        	BinaryWriter^ w = gcnew BinaryWriter(fs);
        
        	int rows = fileData->GetLength(0);
        	int cols = fileData->GetLength(1);
        	try
        	{
        		for (int row = 0; row < rows; row++){
        			for (int col = 0; col < cols; col++){
        				w->Write(fileData\[row,col\]);
        			}
        		}
        	}
        	finally
                {
        		w->Close();
        		fs->Close();
                }
        

        Does anyone have a clue why the written files is empty? Thanks!

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Try running it in the debugger to check that you actually have some data and that it is being written to your file.

        It's time for a new signature.

        K 1 Reply Last reply
        0
        • L Lost User

          Try running it in the debugger to check that you actually have some data and that it is being written to your file.

          It's time for a new signature.

          K Offline
          K Offline
          KlaasVersteeg
          wrote on last edited by
          #4

          Thanks for your answer Richard. I tried running in the debugger. I do have some data to write to the file. How can I check whether it is actually written to the file? When I check the file in Windows Explorer it stays at 0kb.

          L 1 Reply Last reply
          0
          • K KlaasVersteeg

            Thanks for your answer Richard. I tried running in the debugger. I do have some data to write to the file. How can I check whether it is actually written to the file? When I check the file in Windows Explorer it stays at 0kb.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I don't know what is happening with your file, I just tried a similar test and the file was written correctly. All I can suggest is that you step through your program to ensure the values actually get written, and also that no other part of your program destroys the file.

            It's time for a new signature.

            K 1 Reply Last reply
            0
            • L Lost User

              I don't know what is happening with your file, I just tried a similar test and the file was written correctly. All I can suggest is that you step through your program to ensure the values actually get written, and also that no other part of your program destroys the file.

              It's time for a new signature.

              K Offline
              K Offline
              KlaasVersteeg
              wrote on last edited by
              #6

              That's strange. I'll try it on another computer, maybe that helps. Thanks a lot for your help anyway!

              1 Reply Last reply
              0
              • K KlaasVersteeg

                Hi, I'm trying to write a binary file, using BinaryWriter. My code does write a file, but it is empty. This is the code I'm using:

                	FileStream^ fs = gcnew FileStream(Convert::ToString(fileName), FileMode::Create);
                	BinaryWriter^ w = gcnew BinaryWriter(fs);
                
                	int rows = fileData->GetLength(0);
                	int cols = fileData->GetLength(1);
                	try
                	{
                		for (int row = 0; row < rows; row++){
                			for (int col = 0; col < cols; col++){
                				w->Write(fileData\[row,col\]);
                			}
                		}
                	}
                	finally
                        {
                		w->Close();
                		fs->Close();
                        }
                

                Does anyone have a clue why the written files is empty? Thanks!

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Hi, two guesses: 1. you are looking at the wrong folder; is it a relative path? is your current directory what you think it is? add a w->Write(1); early on to make sure there is some data, so the file size and modifiction date must change. 2. not sure what Convert::ToString(fileName) is supposed to do. Have a go with a simple "aha.dat" :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                Prolific encyclopedia fixture proof-reader browser patron addict?
                We all depend on the beast below.


                K 1 Reply Last reply
                0
                • L Luc Pattyn

                  Hi, two guesses: 1. you are looking at the wrong folder; is it a relative path? is your current directory what you think it is? add a w->Write(1); early on to make sure there is some data, so the file size and modifiction date must change. 2. not sure what Convert::ToString(fileName) is supposed to do. Have a go with a simple "aha.dat" :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  Prolific encyclopedia fixture proof-reader browser patron addict?
                  We all depend on the beast below.


                  K Offline
                  K Offline
                  KlaasVersteeg
                  wrote on last edited by
                  #8

                  Hi Luc, I found out that something was going wrong with my fileName indeed. Its working now. Thanks a lot for your help!

                  L 1 Reply Last reply
                  0
                  • K KlaasVersteeg

                    Hi Luc, I found out that something was going wrong with my fileName indeed. Its working now. Thanks a lot for your help!

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    As I suggested yesterday, you should always run through your code with the debugger to ensure that all your variables contain the correct values. When you said the file size was zero, you did not mention that the create time had not been changed. That would have been a good clue that the file was not being written.

                    It's time for a new signature.

                    K 1 Reply Last reply
                    0
                    • L Lost User

                      As I suggested yesterday, you should always run through your code with the debugger to ensure that all your variables contain the correct values. When you said the file size was zero, you did not mention that the create time had not been changed. That would have been a good clue that the file was not being written.

                      It's time for a new signature.

                      K Offline
                      K Offline
                      KlaasVersteeg
                      wrote on last edited by
                      #10

                      Well, the file was actually written, so the create time changed. It was only the referece where to write the data to that as wrong, and that I didn't catch when stepping through the code. But now it works. Thanks again for the help!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups