Thread: Easy question..

  1. #1
    Registered User
    Join Date
    Jul 2008

    Easy question..

    im new to c++ so im not sure whats wrong with this but it wont run as it says buffer has not been initailized

    void fileRead()
    	std::ifstream file ("c:\\example.txt");
    	  char *buffer;, 100);
    	  std::cout<< buffer;

  2. #2
    Registered User
    Join Date
    Jan 2005
    buffer is a pointer, but it is not initialized. That means it could be pointing anywhere in memory, so when you try to read into it that will cause a problem.

    If you're new to C++, you probably should be using the C++ string class for strings. What are you trying to read from the file? Is it just text?

  3. #3
    Registered User
    Join Date
    Jul 2008
    from a file. I got that code straight from a book lol. whats the easiest way to read from a file and put that into a variable? what i have there looks easy but obviously doesnt work.

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    std::string buffer;
    file >> buffer;
    But the "best" approach depends on how and what kind of data you want to read.
    The above will read a word from the file.

    std::string buffer;
    file >> std::noskipws >> buffer;
    [Disclaimer: NOT TESTED]

    Should read a whole line.
    Or you can do
    std::getline(buffer, file);
    [Unless I mixed up the arguments.]

    Anyway, good luck, and avoid pointers (for now).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Join Date
    Dec 2007
    So what awful book had this exact text?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. easy Vector contructor question
    By noodle24 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2006, 07:43 PM
  2. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 03:59 PM
  3. This is hopefully an extremely easy question...
    By rachaelvictoria in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 12:36 AM
  4. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 10:43 PM
  5. Easy Question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2002, 12:19 PM
Website Security Test