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. simple string problem - Noob

simple string problem - Noob

Scheduled Pinned Locked Moved C / C++ / MFC
questioniosdata-structureshelp
5 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.
  • P Offline
    P Offline
    Paul Hasler
    wrote on last edited by
    #1

    Writing a simple Win32 Console App to read the header of a WAV file. Aim was to test my understanding of File IO, but seem to have uncovered a problem with my understanding of strings instead when I got some unexpected output. :-O The code below results in output such as: RIFF RIFF╠╠╠╠¿ ↕ If char ch[4] defines ch as a character array of size 4, why does cout << ch appear to output additional bytes beyond the end of ch?

    int main(int argc, char *argv[])
    {
    char ch[4];
    register int i;
    if(argc!=2)
    {
    cout << "Usage: WavInfo \n";
    return 1;
    }
    ifstream in(argv[1], ios::in | ios::binary);
    if(!in)
    {
    cout << "Cannot open file.\n";
    return 1;
    }
    in.read(ch,4);
    for(i=0; i<4; i++){cout << ch[i];}
    cout << "\n";
    cout << ch << "\n";

    }

    L G 2 Replies Last reply
    0
    • P Paul Hasler

      Writing a simple Win32 Console App to read the header of a WAV file. Aim was to test my understanding of File IO, but seem to have uncovered a problem with my understanding of strings instead when I got some unexpected output. :-O The code below results in output such as: RIFF RIFF╠╠╠╠¿ ↕ If char ch[4] defines ch as a character array of size 4, why does cout << ch appear to output additional bytes beyond the end of ch?

      int main(int argc, char *argv[])
      {
      char ch[4];
      register int i;
      if(argc!=2)
      {
      cout << "Usage: WavInfo \n";
      return 1;
      }
      ifstream in(argv[1], ios::in | ios::binary);
      if(!in)
      {
      cout << "Cannot open file.\n";
      return 1;
      }
      in.read(ch,4);
      for(i=0; i<4; i++){cout << ch[i];}
      cout << "\n";
      cout << ch << "\n";

      }

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

      C/C++ use the null-character to indicate the end of a string, so in memory, the string would be stored as 'R' 'I' 'F' 'F' '\0'. So to store 4 characters, you need a string char ch[5];

      P 1 Reply Last reply
      0
      • P Paul Hasler

        Writing a simple Win32 Console App to read the header of a WAV file. Aim was to test my understanding of File IO, but seem to have uncovered a problem with my understanding of strings instead when I got some unexpected output. :-O The code below results in output such as: RIFF RIFF╠╠╠╠¿ ↕ If char ch[4] defines ch as a character array of size 4, why does cout << ch appear to output additional bytes beyond the end of ch?

        int main(int argc, char *argv[])
        {
        char ch[4];
        register int i;
        if(argc!=2)
        {
        cout << "Usage: WavInfo \n";
        return 1;
        }
        ifstream in(argv[1], ios::in | ios::binary);
        if(!in)
        {
        cout << "Cannot open file.\n";
        return 1;
        }
        in.read(ch,4);
        for(i=0; i<4; i++){cout << ch[i];}
        cout << "\n";
        cout << ch << "\n";

        }

        G Offline
        G Offline
        guyee
        wrote on last edited by
        #3

        Strings have to be zero terminated. Replace

        char ch[4];

        with

        char ch[5];
        ch[4] = 0;

        or something similar to have a zero after the string data.

        P 1 Reply Last reply
        0
        • L Lost User

          C/C++ use the null-character to indicate the end of a string, so in memory, the string would be stored as 'R' 'I' 'F' 'F' '\0'. So to store 4 characters, you need a string char ch[5];

          P Offline
          P Offline
          Paul Hasler
          wrote on last edited by
          #4

          Thanks Thaddeus. Problem solved!

          1 Reply Last reply
          0
          • G guyee

            Strings have to be zero terminated. Replace

            char ch[4];

            with

            char ch[5];
            ch[4] = 0;

            or something similar to have a zero after the string data.

            P Offline
            P Offline
            Paul Hasler
            wrote on last edited by
            #5

            Thanks guyee. Problem solved!

            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