Read file

This is a discussion on Read file within the C++ Programming forums, part of the General Programming Boards category; I was about to start building a program, but then I realised I didn't know how to read files. In ...

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

    Read file

    I was about to start building a program, but then I realised I didn't know how to read files. In this case I would have the path of the file and want to echo it into console.
    What is the comand?

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    depends how you want to do it
    fread()
    fstream

    There are more, but that should give you some building ground. Check out cplusplus.com for all the fread related functions.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    I've managed to shorten what I need to know. Looking at this from the tutorial, I have most of what I need.

    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";
      cin.get();    // wait for a keypress
      // b_file is closed implicitly here
    }
    However, I want to output a paragraph, meaning more than one string. Perhaps using for loops?

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Sure, you can do something like.

    Code:
    while (b_file >> str)
    {
       cout << str << endl;
    }

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    Thank you, I've gotten even closer now.

    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char str[10];
    
      //Opens for reading the file
      ifstream b_file ( "example" );
      //Reads strings sequentialy
      while (b_file >> str)
    {
      cout << str << endl;
    }
      //Should output all contents
      cout<< str <<"\n";
      cin.get();    // wait for a keypress
      // b_file is closed implicitly here
    }
    The only thing left to solve is that every word shows as a new line. How can I make it only start a new line when the file does?

  6. #6
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    getline - C++ Reference

    Thats what you want most likely.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    In my case, I do want exact replication.

    Edit: Solved.
    Last edited by Muscovy; 06-19-2009 at 10:06 PM.

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, 01:36 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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