Adding a File

This is a discussion on Adding a File within the C++ Programming forums, part of the General Programming Boards category; I want to add a whole text file into my program. I have tried strings but I want to keep ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    6

    Question Adding a File

    I want to add a whole text file into my program. I have tried strings but I want to keep the spaces but strings take the spaces as terminating characters. so how do I input an entire file into my program.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    One way is to read each line with getline and put the newlines back in.

    Code:
    std::string line, whole_file;
    while (std::getline(fin, line)) {
        whole_file += line;
        whole_line += '\n';
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jun 2007
    Location
    Qatar
    Posts
    39
    Dear,
    This program will took an input file "test" from the disk.This txt file should be in the same directory of C++ files.

    Code:
    Have a look at this program,
    #include <iostream.h>
    #include <fstream.h>
    void main()
    {
    const int max=80;
    char buffer[max];
    ifstream infile;
    infile.open("test.txt");
    while(infile)
    {
    infile.getline(buffer,max);
    cout<<buffer<<endl
    
    }
    }

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I method could be to use the getline( ) function, and place an entire line of text into a <string> object...

    Code:
    #include<string>
    #include<fstream>
    ...
    ...
    ...
    ifstream infile;
    string line_of_text;
    string entire_document;
    
    infile.open("c:\\myfile");
    
    while(getline(infile, line_of_text))
    {     
         entire_document += line_of_text;
         entire_document += '\n';
    }
    
    ...
    ...


    *Anon beat me to this post. It's basically the same idea.
    Last edited by The Brain; 06-24-2007 at 10:13 AM. Reason: give credit to the fast poster
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    6
    Thank You so much works great!

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    Another choice:
    Code:
    #include <fstream>
    #include <sstream>
    #include <string>
    
    int main()
    {
       std::ifstream fin(__FILE__); // Insert your filename instead of __FILE__ here.
       std::ostringstream oss;
       oss << fin.rdbuf();
       std::string fileContents = oss.str();
       // use fileContents
    }

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    903
    So that's how you do that. I've been trying all sorts of std::getline(delim = some_EOF) for any two streams. It's the buffers... thank you, Daved.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

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. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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