CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2004
    Posts
    204

    Printing a String

    I am trying to print the contents of a buffer LPMIDIHDR lpBuffer

    I am after the lpData member, and since it is declared as LPSTR

    I tried doing it this way:
    Code:
    fprintf(fp,"lpBuffer = <%s>\n", &lpBuffer[i].lpData);
    But I am only getting one character

    lpBuffer = <H>

    What do I have to do to print out the entire string?

    This is the structure from MSDN
    Code:
    typedef struct midihdr_tag {
      LPSTR              lpData;
      DWORD              dwBufferLength;
      DWORD              dwBytesRecorded;
      DWORD_PTR          dwUser;
      DWORD              dwFlags;
      struct midihdr_tag *lpNext;
      DWORD_PTR          reserved;
      DWORD              dwOffset;
      DWORD_PTR          dwReserved[4];
    } MIDIHDR, *LPMIDIHDR;

  2. #2
    Join Date
    Apr 2010
    Location
    Western WA, USA
    Posts
    59

    Re: Printing a String

    Try it without the "&" in front of lpBuffer[i].lpData, as "&lpBuffer[i].lpData" would be the address of the string pointer, not the address of the string.

  3. #3
    Join Date
    Apr 2004
    Posts
    204

    Re: Printing a String

    Thanks for offering some help.

    Still not getting the results I expect.

    fprintf(fp, "[%d] <%s>\n", j, lpBuffer[0].lpData);
    returns
    [0] <>
    [1] <>
    [2] <>
    [3] <>
    [4] <>
    [5] <>
    [6] <>
    [7] <>
    [8] <>
    [9] <>
    [10] <>
    [11] <>
    [12] <>
    .
    .
    .

    fprintf(fp, "[%d] <%s>\n", j, &lpBuffer[0].lpData[j]);
    returns

    [0] <>
    [1] <>
    [2] <>
    [3] <>
    [4] <>
    [5] <>
    [6] <>
    [7] <>
    [8] <ÀÔ>
    [9] <Ô>
    [10] <>
    [11] <>
    [12] <>
    .
    .
    .

    fprintf(fp, "[%d] <%s>\n", j, lpBuffer[0].lpData[j]);
    returns
    Unhandled exception error

  4. #4
    Join Date
    Apr 2004
    Posts
    204

    Re: Printing a String

    Changing the specifier in fprintf works with your suggestion... Thanks!

    Code:
    FILE *fp2;
    fp2 = fopen("output2.txt", "w");
    fprintf(fp2, "dwBytesRecorded = %d\n", lpBuffer[0].dwBytesRecorded);	
    
    for (j = 0; j < (lpBuffer[0].dwBytesRecorded); j++) {
    	fprintf(fp2, "[%d] <%x\n", j, lpBuffer[0].lpData[j]);
    }
    
    fclose(fp2);
    returns
    dwBytesRecorded = 348
    [0] <0>
    [1] <0>
    [2] <0>
    [3] <0>
    [4] <0>
    [5] <0>
    [6] <0>
    [7] <0>
    [8] <ffffffc0>
    [9] <ffffffd4>
    [10] <1>
    [11] <1>
    [12] <0>
    .
    .
    .

  5. #5
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Printing a String

    Quote Originally Posted by mmscg View Post
    Changing the specifier in fprintf works with your suggestion... Thanks!

    Code:
    FILE *fp2;
    fp2 = fopen("output2.txt", "w");
    fprintf(fp2, "dwBytesRecorded = %d\n", lpBuffer[0].dwBytesRecorded);	
    
    for (j = 0; j < (lpBuffer[0].dwBytesRecorded); j++) {
    	fprintf(fp2, "[%d] <%x\n", j, lpBuffer[0].lpData[j]);
    }
    
    fclose(fp2);
    returns
    dwBytesRecorded = 348
    [0] <0>
    [1] <0>
    [2] <0>
    [3] <0>
    [4] <0>
    [5] <0>
    [6] <0>
    [7] <0>
    [8] <ffffffc0>
    [9] <ffffffd4>
    [10] <1>
    [11] <1>
    [12] <0>
    .
    .
    .
    Yep, because lpData is not a string, it's the MIDI data:

    http://msdn.microsoft.com/en-us/libr...=VS.85%29.aspx

    Viggy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured