Parameter not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • touqra
    New Member
    • Jun 2007
    • 3

    Parameter not working

    I have written a C++ as I learn C++ along, and the funny thing is that this C++ I wrote, just have to open a txt file, and reads it and cout the content.
    The txt file (new.txt) content is 12345.
    But the output that I got once compiled was 123. I can't figure out why 45 doesn't appear. If I revert to char str[12]; it outputs 12345. I dunno what's wrong with my code below:

    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;

    int main(){

    char *str;
    str = new char;

    fstream file;
    file.getline(st r, sizeof str);

    cout << str << endl;

    file.close();
    }
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by touqra
    I have written a C++ as I learn C++ along, and the funny thing is that this C++ I wrote, just have to open a txt file, and reads it and cout the content.
    The txt file (new.txt) content is 12345.
    But the output that I got once compiled was 123. I can't figure out why 45 doesn't appear. If I revert to char str[12]; it outputs 12345. I dunno what's wrong with my code below:

    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;

    int main(){

    char *str;
    str = new char;

    fstream file;
    file.getline(st r, sizeof str);

    cout << str << endl;

    file.close();
    }
    Hi,
    There are couple of problems in the code.
    1)You are not opening the file anywhere, but closing it
    2)You are allocating memory for only 1 byte and reading more than that.
    3)Th reason u are getting 123 is sizeof(str) is 4 bytes as it is a pointer and it reads 1 less than the size specified.
    Make those changes and u will get the expected result
    [code=cpp]
    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;

    int main(){

    char *str;
    str = new char[10];

    fstream file;
    file.open("ip_2 4.txt");
    file.getline(st r,9);

    cout << str <<endl;

    file.close();
    }
    [/code]

    Raghuram

    Comment

    • touqra
      New Member
      • Jun 2007
      • 3

      #3
      Originally posted by gpraghuram
      Hi,
      There are couple of problems in the code.
      1)You are not opening the file anywhere, but closing it
      2)You are allocating memory for only 1 byte and reading more than that.
      3)Th reason u are getting 123 is sizeof(str) is 4 bytes as it is a pointer and it reads 1 less than the size specified.
      Make those changes and u will get the expected result
      [code=cpp]
      #include <iostream>
      #include <fstream>
      #include <string>

      using namespace std;

      int main(){

      char *str;
      str = new char[10];

      fstream file;
      file.open("ip_2 4.txt");
      file.getline(st r,9);

      cout << str <<endl;

      file.close();
      }
      [/code]

      Raghuram

      oops...I forgot to write the file.open line when I typed this post.
      Initially, I read that new can be used to provide a varying memory size for the pointer it's refering to. So, that's why I wrote str = new char; How then should I use new ?

      If my code is only allocating one byte space for str, I don't understand how I could manage to cout 123. If it's one byte, shouldn't it cout 1 ? And why should a pointer read less one byte than the size of txt ?

      My initial thought is to, want to extend this code, such that, if I were to have a txt file with content say 1234567890, and everytime I want the code to only read two numbers at any time, cout, then discard the two numbers read to free the memory again, to read the next two numbers, and cout again. looping it.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by touqra
        oops...I forgot to write the file.open line when I typed this post.
        Initially, I read that new can be used to provide a varying memory size for the pointer it's refering to. So, that's why I wrote str = new char; How then should I use new ?

        If my code is only allocating one byte space for str, I don't understand how I could manage to cout 123. If it's one byte, shouldn't it cout 1 ? And why should a pointer read less one byte than the size of txt ?
        Let me add to what has been said.

        the coin.getline() assumes you are using a C-string. C-strings have a null terminator. Therefore, a memory allocation of 4 cn hold a string with 3 characters.

        You can cout 123 because your sizeof str reported 4 (the size of the char*).
        Of course, the 2 and the 3 and the null terminator are now in memory you don't own so your program will dribble along and then crash.

        Lastly, you should be using string objects. char* strings are used in C.

        [code=cpp]
        string theData; //our data goes here
        const int max = 10;
        char line[max];
        while (cin.get(line,m ax))
        {

        theData += line;
        //go back for more data
        }
        char x = cin.get(); //finally, eat the delim

        cout << theData;
        [/code]

        Comment

        Working...