file i/o help

This is a discussion on file i/o help within the C++ Programming forums, part of the General Programming Boards category; I know the basic file i/o structur, but I can't work out how to make it display more than one ...

  1. #1
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161

    file i/o help

    I know the basic file i/o structur, but I can't work out how to make it display more than one string on the page?

    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char str[10];
    
      //Creates an instance of ofstream, and opens example.txt
      ofstream a_file ( "example.txt" );
      // Outputs to example.txt through a_file
      a_file<<"This text will now be inside of example.txt";
      // Close the file stream explicitly
      a_file.close();
      //Opens for reading the file
      ifstream b_file ( "example.txt" );
      //Reads one string from the file
      b_file>> str;
      //Should output 'this'
      cout<< str <<"\n";
      // b_file is closed implicitly here
    }
    This is the code from cprog and it displays 'this'. But how do you display the whole thing
    Last edited by algi; 01-20-2005 at 11:17 AM.
    I started out with nothing and I still have most of it left.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    one quick and easy way would be to:

    Code:
    string ouput = "This text will now be inside of example.txt";
    
    a_file << output;
    Last edited by The Brain; 01-20-2005 at 11:57 AM.
    • "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

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    The problem was to read a string, not output a string.

    The simplest method to get an entire line is to use std::getline():
    Code:
    std::string input;
    std::ifstream b_file("example.txt");
    std::getline(b_file, input);  //get a whole line of text from the file
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161
    thanks
    I started out with nothing and I still have most of it left.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-31-2004, 12:21 AM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 09:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM

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