Sign in to follow this  

How do I output all contents of a file to the screen?

This topic is 4716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I've got it down good enough to create a file and store what I want in it. I have four books and all of them give this example with variations:

char prod_name[10];
char prod_id[10];


ofstream a_file("file1.txt",ios::out);
a_file << "Justin is a dumba$$ sometimes";
a_file.close();

ifstream b_file("file1.txt",ios::in);
b_file >> prod_name;
b_file >> prod_id;
cout << prod_name << "\t\t";
cout << prod_id << "\n";

b_file.close();

Please tell me how to get the entrire file contents to display on the screen. Thanks. BTW, I'd say you can tell this isn't a WIN32 app, but a console app.

Share this post


Link to post
Share on other sites

#include<fstream>
#include<iostream>
#include<ostream>

int main()
{
std::ifstream infile("myfile.txt");
std::cout << infile.rdbuf();
}




The above code uses something called a stream buffer. The member function rdbuf() returns a pointer to the stream buffer contained within infile. std::cout has an operator<< member function which can print the contents of a stream buffer.

Share this post


Link to post
Share on other sites
Quote:
Original post by RigidBody
*** Source Snippet Removed ***

The above code uses something called a stream buffer. The member function rdbuf() returns a pointer to the stream buffer contained within infile. std::cout has an operator<< member function which can print the contents of a stream buffer.


Thank you very much RigindBody. I tried this and maybe it's just because it's not enough explanation for a n00b like me. I was wondering if you could look at my code and see if there is anything weird in the record_entry function (or is it a class?). Please overlook the quality of the coding and if anyone would like to give me advice to clean it up and/or techniques to keep it cleaner and code it more simply... I'm all ears. I'm still at my original question because I tried what he said and I must not understand it because it didn't work.

Anyway same thing:


Please tell me how to get the entrire file contents to display on the screen.


This is my source:


#include <iostream.h>
#include <stdlib.h>
#include <process.h>
#include <fstream.h>
#include <ostream.h>


using namespace std;

void menu();
void inventory();
void utilities();
void intro();
void record_entry();
void reports();
void sales_monthly();
void sales_all();

int main()
{
menu();
return 0;
}

void intro()
{
cout << "Welcome to Inventoria \n\n"
<< "In this program you will be able to keep\ntrack of the products you have in inventory.\n\n\n";
}

void menu()
{
char menu;
system("cls");
intro();
cout << "****************** Main Menu ******************\n\n";
cout << "Please select an option.\n\n\n"
<< "I for Inventory\n\n"
<< "U for Utilities (Not Implemented Yet)\n\n"
<< "E to Exit \n";
cin >> menu;

switch (menu)
{

case 'i':
{
system("cls");
inventory();
break;
}
case 'u':
{
system("cls");
utilities();
break;
}
case 'e':
{
break;
}
default:
{
break;
}
}


}

void inventory()
{

int inventory;

cout << "Inventory Submenu\n\n\n";
cout << "Select an option\n\n";
cout << "1. Enter Records\n"
<< "2. What Is Out of Stock?\n"
<< "3. View Sold Out Items\n"
<< "4. View Reports\n"
<< "5. Main Menu\n\n";
cin >> inventory;

switch(inventory)
{
case 1:
{
system("cls");
record_entry();
break;
}
case 2:
{
break;
}
case 3:
{
break;
}
case 4:
{
system("cls");
reports();
break;
}

case 5:
{
system("cls");
menu();
break;
}

default:
{
break;
}

}


}

void record_entry()
{

char prod_name[10];
char prod_id[10];


ofstream a_file("file1.txt",ios::out);
a_file << "Justin\t";
a_file << "3743 ";

a_file.close();

ifstream b_file("file1.txt",ios::in);
b_file >> prod_name;
b_file >> prod_id;
cout << prod_name << "\t\t";
cout << prod_id << "\n";
b_file.close();

///////////////////////////////////// CODE FROM RigidBodyON GAMEDEV.NET Forums
std::ifstream infile("myfile.txt");
std::cout << infile.rdbuf();


system("pause");


}

void reports()
{
int report;
cout << "Report SubMenu\n";
cout << "1. Monthly Sales Report\n"
<< "2. Sales History\n"
<< "3. Back to Inventory Menu\n";
cin >> report;

switch(report)
{

case 1:
{
system("cls");
sales_monthly();
break;
}

case 2:
{
system("cls");
sales_all();
break;
}

case 3:
{
inventory();
break;
}

}

inventory();

}

void sales_monthly()
{
}

void sales_all()
{
}

void utilities()
{

cout << "Utilities Submenu\n\n\n";
menu();

}

Share this post


Link to post
Share on other sites
Get back to C

#include <stdio.h>
#include <conio.h>
#include <io.h>

int main()
{
// open file

FILE * f = fopen("your file name here", "rb");

if (f == 0) { return -1; }

// find out length, allocate memory, load into memory

long l = _filelength(_fileno(f));

char * m = malloc(l + 1);

if (m == 0) { fclose(f); return -1; }

if (fread(m, 1, l, f) != (size_t)l) { fclose(f); return -1; }

// close file

fclose(f);

// display on screen

printf("%s", m);

// free memory

free(m);

// wait for the user to hit a key before exiting

getch();

return 0;

}

Share this post


Link to post
Share on other sites
You're using the wrong header files. <iostream> and <fstream> (<ostream> isn't necessary), not <iostream.h> and <fstream.h>. The latter are pre-standard, and there's no guarantee as to their internal structures, or even interfaces. For full compliance, use <cstdlib> in place of <stdlib.h> (this is for C++, and places all the symbols in the file under the std namespace).

Using Standard C++ Headers
Using Standard C++ Library Headers
Standard C++ Library Headers
Standard C++ Library FAQ

Share this post


Link to post
Share on other sites
Quote:
Original post by JTekNet
Quote:
Original post by ZedFx
Get back to C


hehe I don't know C that's why I didn't "get back to it" ^_^


HOWEVER! If anyone would like to make that c++ source I'd be infinitely appreciative of them. And thank you ZedFx for the code snippet!

Share this post


Link to post
Share on other sites

This topic is 4716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this