File stream to char* help

This is a discussion on File stream to char* help within the C++ Programming forums, part of the General Programming Boards category; I'm trying to take data from a file stream and put it into a char*. (I know using strings is ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    File stream to char* help

    I'm trying to take data from a file stream and put it into a char*. (I know using strings is better.. but I'm kind of being forced to do so). And so I have the code below almost working like it should, but I get two extra characters at the end. How can I fix that?

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    
    int main()
    {
       std::ifstream dataFile("strings.txt"); // strings.txt contains "this is my string" or w/e
    
       char* str = new char[];
    
       int i = 0;
       while(!dataFile.eof())
       {
       	dataFile >> str[i];
    	i++;
       } 
    
       std::cout << str;
    
       return 0
    }
    I'm not really concerned about losing spaces, since in the end I just need the letters of the alphabet for something I would do later.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    That program should not even compile since you did not specify the size when using the new[] operator.

    Now, you should not be using eof() to control the loop: eof() only returns true after the EOF condition is detected, causing you to read one more time than you want to. Rather, you should write:
    Code:
    int i = 0;
    while (dataFile >> str[i])
    {
        i++;
    }
    or more simply:
    Code:
    for (int i = 0; dataFile >> str[i]; ++i);
    But here is a limitation: if you specify some size when using the new[] operator, you would then be able to read at most that number of characters. Unfortunately, you do not know beforehand how many characters will have to be read. Consequently, you need to track the size (as in number of characters read) and the capacity (as in the number of characters allocated), and expand the dynamic string when necessary (usually by increasing the capacity by some factor greater than 1 to create a new string, and then copying over the characters from the old string to the new string, and then destroying the old string so that you can work fully with the new string).

    Of course, remember to delete[] what you new[].
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    But see, when I specify how big I want my dynamic array to be, then I still get random characters at the end of it. And even more of them.

    Thank-you for the advice on how to set up the while loop though.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Location
    Halesowen, England
    Posts
    30
    After the while loop, you need to NULL terminate the char * string, like
    Code:
    str[i] = '\x0';

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by #2Pencil View Post
    But see, when I specify how big I want my dynamic array to be, then I still get random characters at the end of it. And even more of them.
    That's why you keep track of how much of the array is actually being used.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Quote Originally Posted by Jamdog View Post
    After the while loop, you need to NULL terminate the char * string, like
    Code:
    str[i] = '\x0';
    It worked! Thank-you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Replies: 9
    Last Post: 07-01-2002, 07:50 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21