Read file as a variable

This is a discussion on Read file as a variable within the C++ Programming forums, part of the General Programming Boards category; I want to get the contents of a file, but the only way I know how to do that is ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    45

    Read file as a variable

    I want to get the contents of a file, but the only way I know how to do that is
    Code:
      infile.open ("filepath", ifstream::in);
    
      while (infile.good())
        cout << (char) infile.get();
        cout << "\n" ;
      infile.close();
    But that's if you want to output it. I want to use the contents of the file as a string for operations.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Code:
    std::vector< char > fileData;
    std::ifstream file( filename );
    for( int ch = file.get(); file; ch = file.get() )
        fileData.push_back( (char)ch );
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    I'm getting all kinds of errors. How do I impliment it?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Are you including the correct headers?
    What are the errors you get? Details!
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. read in csv file
    By gums in forum C Programming
    Replies: 5
    Last Post: 05-10-2007, 07:38 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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