473,394 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

woh do I read a file with std::istream ???

Hi
I simply don't understand how to read a simple file using
std::istream. How do I open a file for reading with istream??

Anne-Marte
Jul 23 '05 #1
3 5980
Anne-Marte wrote:
I simply don't understand how to read a simple file using
std::istream. How do I open a file for reading with istream??

Maybe std::ifstream will be better for you ?

--
SirMike
the code is my strength
http://www.sirmike.grudziadz.com
Jul 23 '05 #2
Anne-Marte wrote:
Hi
I simply don't understand how to read a simple file using
std::istream. How do I open a file for reading with istream??

Anne-Marte


Look up ifstream. (Note the 'f'.)
The generic method is:
1. Open the file.
2. Extract data from the stream.
3. Close the file when finished.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Jul 23 '05 #3

Anne-Marte wrote:
Hi
I simply don't understand how to read a simple file using
std::istream. How do I open a file for reading with istream??

Anne-Marte


try this:

#include <iostream>
#include <fstream>

using std::cout;
using std::endl;
using std::ifstream;
using std::streamsize;
using std::ios;

int main()
{
ifstream in("C:\\temp.txt"); // Opens file

char str[80]; // Sets up string buffer
while(in.good())
{
in.getline(str, 80); // Gets 80 chars and stores them in str
streamsize n = in.gcount(); // Counts how many chars have been read
cout << str << endl << n << endl; // Prints them to the screen
}

cout << "Bad bit set: " <<( in.rdstate( ) & ios::badbit ) << endl;
cout << "Fail bit set: " <<( in.rdstate( ) & ios::failbit ) << endl;
cout << "End of file bit set: " << ( in.rdstate( ) & ios::eofbit ) <<
endl;
cout << endl;

in.close(); // Closes file
return 0;
}

This will read your file in text mode. It will attempt to read all the
characters in a line until the new line character is found and then
transfer this to the buffer str. This buffer is defined as 80 lines
long so if the line is 80 characters or longer the stream will place 79
of the characters in the buffer str and add the NULL charcter '\0' at
the end. If a line is 80 characters or longer the failbit will be set.
This means it will exit from the loop. If you want to continue the
loop going then add this to the end of the while loop.

if (in.fail())
in.clear()

This will reset the stream to continue.

Hope this information helps. Please note there is more than one way to
read a file using ifstream.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: anonymous | last post by:
I cannot compile LiDIA mathematical library version 2.1pre5 with the gmp 4.1.2 multiprecision library because the gcc compiler ver 3.2.2 20030222 Red Hat Linux 9 complains about some structures in...
21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
4
by: Gernot Frisch | last post by:
Can I convert a std::string to a std::istream object? -- -Gernot int main(int argc, char** argv) {printf ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
6
by: Jason K | last post by:
Let me preface this by saying this obviously isn't a C++ *language* issue per se; rather probably an issue relating to quality of implementation, unless I'm just misusing iostream... I wrote a...
5
by: Jim Langston | last post by:
In one of my files I am outputting the value of a char into a human readable file. That is, char a = 123; std::ofstream CharFile( ("Players\\" + Name + ".char").c_str()); if ( CharFile.is_open()...
13
by: Gianni Mariani | last post by:
What I would like to do is read bytes from a stream, any number and any time. I would like it to wait until there are any bytes to read. I want the exact same functionality as cstdio's "fread"...
2
by: david.crow | last post by:
Given the following input file: bob 1 2 3 4 5 mary 2 3 4 5 6 7 susan 3 4 5 6 7 8 9 This code snippet does not read it correctly: class Student {
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
7
by: randysimes | last post by:
I have to overload this operator to cin a character array to eventually put the strings in lexicographical order. The is unknown and therefore will change accordingly. I do not know how to do...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.