473,804 Members | 3,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6078
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.l earn.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.tx t"); // 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
1812
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 the gmp.h header file: /usr/local/include/gmp.h:2073: declaration of C function `std::ostream& operator<<(std::ostream&, const __mpq_struct*)' conflicts with /usr/local/include/gmp.h:2072: previous declaration `std::ostream&...
21
2532
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 constructors class MyClass { Foo foo; // foo and bar require default constructors Bar bar; public:
4
18073
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
3010
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 function to count lines in a large file that start with a particular pattern. The file can contain large amounts of non-text crap, which may make some lines very long (so using getline() with a std::string isn't feasable). So I dug around looking...
5
2372
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() ) CharFile << (int) a; So the file has the character a stored as "123". That was the easy part, now comes the fun of reading it back into the char.
13
11003
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" but in a std::istream. It appeared at first that "readsome" would do exactly what I wanted but it appears not to work very well at all (at least with gcc!). When reading from a named pipe on gcc, it returns immediately - no errors, just...
2
7871
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
5720
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 * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
7
5672
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 this
0
9584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10583
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10337
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.