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. C / C++ / MFC
  4. Cfile write problem [modified]

Cfile write problem [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelp
16 Posts 5 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.
  • A Offline
    A Offline
    aei_totten
    wrote on last edited by
    #1

    I am trying to write out an array and all I am getting is junk in the file. Here's the code:

    short frameData\[3648\];
    short \*p;
    file.Open( "C:\\\\test.txt",  CFile::modeCreate|CFile:: modeReadWrite);
    p = (short \*)Frameptr;
    for ( int i=0; i<= 3647; i++) 
    {
    	frameData\[i\] = \*p; 				
    	p++;
    }	
    //write out data		
    file.Write(frameData,sizeof(short)\*3647);	
    file.Close();
    

    modified on Monday, July 14, 2008 2:34 PM

    D A 2 Replies Last reply
    0
    • A aei_totten

      I am trying to write out an array and all I am getting is junk in the file. Here's the code:

      short frameData\[3648\];
      short \*p;
      file.Open( "C:\\\\test.txt",  CFile::modeCreate|CFile:: modeReadWrite);
      p = (short \*)Frameptr;
      for ( int i=0; i<= 3647; i++) 
      {
      	frameData\[i\] = \*p; 				
      	p++;
      }	
      //write out data		
      file.Write(frameData,sizeof(short)\*3647);	
      file.Close();
      

      modified on Monday, July 14, 2008 2:34 PM

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      aei_totten wrote:

      ...all I am getting is junk in the file.

      How are you verifying this? What should be in the file?

      "Love people and use things, not love things and use people." - Unknown

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      A 1 Reply Last reply
      0
      • D David Crow

        aei_totten wrote:

        ...all I am getting is junk in the file.

        How are you verifying this? What should be in the file?

        "Love people and use things, not love things and use people." - Unknown

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        A Offline
        A Offline
        aei_totten
        wrote on last edited by
        #3

        I open the file and there are a bunch of weird chars, boxes, greek letters, etc. and the data is an array of numbers in the 646-780 range. I step through with the debugger and the array is populated fine.

        C A 2 Replies Last reply
        0
        • A aei_totten

          I open the file and there are a bunch of weird chars, boxes, greek letters, etc. and the data is an array of numbers in the 646-780 range. I step through with the debugger and the array is populated fine.

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Well, you asked to write the array as a binary format into the file, so each short from your array will be writte as two bytes in the file (thus resulting in garbage characters). You first have to convert each short into a string and write that string into the file.

          Cédric Moonen Software developer
          Charting control [v1.4] OpenGL game tutorial in C++

          A 1 Reply Last reply
          0
          • A aei_totten

            I open the file and there are a bunch of weird chars, boxes, greek letters, etc. and the data is an array of numbers in the 646-780 range. I step through with the debugger and the array is populated fine.

            A Offline
            A Offline
            aei_totten
            wrote on last edited by
            #5

            and for some reason it isn't posting a less than or equal to in my loop, I even tried to go back and edit it with no luck.

            1 Reply Last reply
            0
            • C Cedric Moonen

              Well, you asked to write the array as a binary format into the file, so each short from your array will be writte as two bytes in the file (thus resulting in garbage characters). You first have to convert each short into a string and write that string into the file.

              Cédric Moonen Software developer
              Charting control [v1.4] OpenGL game tutorial in C++

              A Offline
              A Offline
              aei_totten
              wrote on last edited by
              #6

              Thanks that was it, duh.

              L 1 Reply Last reply
              0
              • A aei_totten

                Thanks that was it, duh.

                L Offline
                L Offline
                led mike
                wrote on last edited by
                #7

                aei_totten wrote:

                Thanks that was it, duh.

                Yeah, imagine that! The file had in it what you told the computer to put in it! Wow, what will they think of next?

                led mike

                M 1 Reply Last reply
                0
                • A aei_totten

                  I am trying to write out an array and all I am getting is junk in the file. Here's the code:

                  short frameData\[3648\];
                  short \*p;
                  file.Open( "C:\\\\test.txt",  CFile::modeCreate|CFile:: modeReadWrite);
                  p = (short \*)Frameptr;
                  for ( int i=0; i<= 3647; i++) 
                  {
                  	frameData\[i\] = \*p; 				
                  	p++;
                  }	
                  //write out data		
                  file.Write(frameData,sizeof(short)\*3647);	
                  file.Close();
                  

                  modified on Monday, July 14, 2008 2:34 PM

                  A Offline
                  A Offline
                  aei_totten
                  wrote on last edited by
                  #8

                  Okay new problem. I decided to write the file each element at a time. Here is the new bit of code...

                  		for ( int i=0; i<= 3647; i++) 
                  		{
                  			frameData\[i\] = \*p; 			
                  			temp.Format("%d ",frameData\[i\]);
                  			file.Write(temp,sizeof(temp));
                  			p++;
                  		}	
                  

                  but instead of getting each number followed by a blank which is what I had intended. I get some of the numbers on top of each other (like four in a row) and some of them correct.

                  A M 2 Replies Last reply
                  0
                  • L led mike

                    aei_totten wrote:

                    Thanks that was it, duh.

                    Yeah, imagine that! The file had in it what you told the computer to put in it! Wow, what will they think of next?

                    led mike

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #9

                    led mike wrote:

                    Wow, what will they think of next?

                    This![^]

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    L 1 Reply Last reply
                    0
                    • A aei_totten

                      Okay new problem. I decided to write the file each element at a time. Here is the new bit of code...

                      		for ( int i=0; i<= 3647; i++) 
                      		{
                      			frameData\[i\] = \*p; 			
                      			temp.Format("%d ",frameData\[i\]);
                      			file.Write(temp,sizeof(temp));
                      			p++;
                      		}	
                      

                      but instead of getting each number followed by a blank which is what I had intended. I get some of the numbers on top of each other (like four in a row) and some of them correct.

                      A Offline
                      A Offline
                      aei_totten
                      wrote on last edited by
                      #10

                      oh yeah temp is a CString

                      1 Reply Last reply
                      0
                      • A aei_totten

                        Okay new problem. I decided to write the file each element at a time. Here is the new bit of code...

                        		for ( int i=0; i<= 3647; i++) 
                        		{
                        			frameData\[i\] = \*p; 			
                        			temp.Format("%d ",frameData\[i\]);
                        			file.Write(temp,sizeof(temp));
                        			p++;
                        		}	
                        

                        but instead of getting each number followed by a blank which is what I had intended. I get some of the numbers on top of each other (like four in a row) and some of them correct.

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #11

                        for ( int i=0; i <= 3647; i++ )
                        {
                        frameData[i] = *p;
                        // temp.Format("%d ",frameData[i]); <-- Wrong format specifier for short int
                        temp.Format("%hd ",frameData[i]);
                        // file.Write(temp,sizeof(temp)); <-- Wrong string length
                        file.Write(temp, temp.GetLength() * sizeof(TCHAR));
                        p++;
                        }

                        If you're writing all text, maybe CStdioFile is a better class to use... Mark

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        A 1 Reply Last reply
                        0
                        • M Mark Salsbery

                          led mike wrote:

                          Wow, what will they think of next?

                          This![^]

                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                          L Offline
                          L Offline
                          led mike
                          wrote on last edited by
                          #12

                          Sure but I like to pronounce that Shamoo ;) Have you tried one? Does it really work? Or is it a misinfomercial? :laugh:

                          led mike

                          M 1 Reply Last reply
                          0
                          • L led mike

                            Sure but I like to pronounce that Shamoo ;) Have you tried one? Does it really work? Or is it a misinfomercial? :laugh:

                            led mike

                            M Offline
                            M Offline
                            Mark Salsbery
                            wrote on last edited by
                            #13

                            led mike wrote:

                            Does it really work?

                            Works great on the commercial! I haven't tried one though. German technology - it HAS to be good!

                            Mark Salsbery Microsoft MVP - Visual C++ :java:

                            L 1 Reply Last reply
                            0
                            • M Mark Salsbery

                              for ( int i=0; i <= 3647; i++ )
                              {
                              frameData[i] = *p;
                              // temp.Format("%d ",frameData[i]); <-- Wrong format specifier for short int
                              temp.Format("%hd ",frameData[i]);
                              // file.Write(temp,sizeof(temp)); <-- Wrong string length
                              file.Write(temp, temp.GetLength() * sizeof(TCHAR));
                              p++;
                              }

                              If you're writing all text, maybe CStdioFile is a better class to use... Mark

                              Mark Salsbery Microsoft MVP - Visual C++ :java:

                              A Offline
                              A Offline
                              aei_totten
                              wrote on last edited by
                              #14

                              Thanks so much. I can't believe how much c++/mfc I had forgotten since switching to C sharp and Java. But somethings you just can't beat in c++. :-D

                              L 1 Reply Last reply
                              0
                              • A aei_totten

                                Thanks so much. I can't believe how much c++/mfc I had forgotten since switching to C sharp and Java. But somethings you just can't beat in c++. :-D

                                L Offline
                                L Offline
                                led mike
                                wrote on last edited by
                                #15

                                aei_totten wrote:

                                But somethings you just can't beat in c++.

                                You can beat a fish.

                                led mike

                                1 Reply Last reply
                                0
                                • M Mark Salsbery

                                  led mike wrote:

                                  Does it really work?

                                  Works great on the commercial! I haven't tried one though. German technology - it HAS to be good!

                                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                                  L Offline
                                  L Offline
                                  led mike
                                  wrote on last edited by
                                  #16

                                  Mark Salsbery wrote:

                                  German technology - it HAS to be good!

                                  Yep[^]

                                  Two and a half years ago, the bursting of the dot-com bubble erased most of the market value of Germany's technology sector. Now, the market itself is being scrapped.

                                  Deutsche Börse, which runs the Frankfurt stock exchange, announced today that it would close the Neuer Markt -- the technology-related market that is Germany's answer to the Nasdaq -- by the end of 2003.

                                  led mike

                                  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