473,387 Members | 1,388 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,387 software developers and data experts.

read a file line by line in c++


I have a qestion about reading file in C++.

How can I read a file line by line (i.e. putting a line ends wtih \n in
a string)

I tried the following, it does read the file, but it does not read it
line by line.

ifstream infile (fileName.c_str());
string buffer;
while ( infile >> buffer) {
cout << "a line: " << buffer << endl;
//BlockData bd(buffer);
// bd.dump();
}

Is there a file reading library in c++?

Feb 13 '06 #1
5 22486
Piotr wrote:
I have a qestion about reading file in C++.

How can I read a file line by line (i.e. putting a line ends wtih \n in
a string)
See 'std::getline' function.
I tried the following, it does read the file, but it does not read it
line by line.

ifstream infile (fileName.c_str());
string buffer;
while ( infile >> buffer) {
cout << "a line: " << buffer << endl;
//BlockData bd(buffer);
// bd.dump();
}

Is there a file reading library in c++?


You're using it.

V
--
Please remove capital As from my address when replying by mail
Feb 13 '06 #2
Piotr wrote:
I have a qestion about reading file in C++.

How can I read a file line by line (i.e. putting a line ends wtih \n in
a string)

I tried the following, it does read the file, but it does not read it
line by line.

ifstream infile (fileName.c_str());
string buffer;
while ( infile >> buffer) {
cout << "a line: " << buffer << endl;
//BlockData bd(buffer);
// bd.dump();
}

Is there a file reading library in c++?

You'll want to take a look at std::getline

std::ifstream inFile(...);
std::string sLine;
....
while(std::getline(inFile, sLine))
{
//Do here whatever you need to do
}

-HappyHippy
Feb 13 '06 #3
"Piotr" <ra************@gmail.com> wrote in news:1139871268.318746.239530
@z14g2000cwz.googlegroups.com:

I have a qestion about reading file in C++.

How can I read a file line by line (i.e. putting a line ends wtih \n in
a string)

I tried the following, it does read the file, but it does not read it
line by line.

ifstream infile (fileName.c_str());
string buffer;
while ( infile >> buffer) {
Reads from infile until the first whitespace, so you're probably getting
your file, word by word. Look up std::getline().
cout << "a line: " << buffer << endl;
//BlockData bd(buffer);
// bd.dump();
}

Is there a file reading library in c++?

Feb 13 '06 #4
xPy
the lines also end with a '\r' try a ( '\n' | | '\r' ) and also include
somewhere the "infile.eof()"
use this if you are using buffer[i].get(infile) (reads the file letter
by letter) or else '\n' will not work...

Feb 14 '06 #5

Piotr wrote in message
<11**********************@z14g2000cwz.googlegroups .com>...

I have a qestion about reading file in C++.
How can I read a file line by line (i.e. putting a line ends wtih \n in
a string)
[snip code]
Is there a file reading library in c++?


The OP has been answered, but, I thought I'd throw this in and see what y'all
thought of it (..since y'all wrote it (if it looks like something you posted,
you may have)).

// -----------------------------------------------------------------
// Filer.h
// -----------------------------------------------------------------
#ifndef Filer_H
#define Filer_H
// ----------------------------------------------
#include <iostream>
#include <ostream> //std::endl
#include <string>
#include <vector>
// --------------------------------------
#include <stdexcept>
class MyError : public std::runtime_error { public:
MyError(const std::string &msg = "") : std::runtime_error(msg){}
};
// --------------------------------------

class Filer : public std::vector<std::string> {
public:
// Filer(){}
Filer(const char *filename){ Open(filename); }
private: // public: if I use Filer() Ctor, re-use instance, concat files.
void Open(const char *filename) throw(MyError){
std::ifstream in(filename);
if(!in){
throw MyError(__FILE__": Open SonOfABitch!");
} //if(!in)
for( std::string line; std::getline(in, line); ){ // [1]
push_back(line);
} //for()
return;
} //Open(const char*)
public:
void Write(std::ostream& out = std::cout){
if(!out){
throw MyError(__FILE__": Write SonOfABitch!");
} //if(!in)
for(const_iterator w = begin(); w != end(); ++w)
out << *w << std::endl;
return;
} //Write(ostream&)
};
// --------------------------------------
#endif //#ifndef Filer_H
// -----------------------------------------------------------------END

// -----------------------------------------------------------------
// FilerTest.cpp
// -----------------------------------------------------------------
#include <iostream>
#include <ostream> //std::endl
#include "Filer.h"

int main(){
using std::cout;
try{
cout<<"_____ Filer.h _____"<<std::endl;
Filer Afile("Filer.h");
//Filer Afile("ErrFiler.h"); // to test exception.
Afile.Write(cout);
cout<<"Afile.size()="<<Afile.size()<<" lines."<<std::endl;
cout<<"Afile.at(1)="<<Afile.at(1)<<std::endl;
Afile.clear();
cout<<"Afile.size()="<<Afile.size()<<" lines."<<std::endl;
cout<<"_____ Filer.h End _____"<<std::endl;
}
catch(MyError &x){
cout<<"MyError &x.what()="<< x.what() <<std::endl;
} //catch(MyError&)
catch(...){
cout<< "DID NOT CATCH MyError()" <<std::endl;
} //catch(MyError&)
return 0;
} //main()
// -----------------------------------------------------------------END

"Filer.h" is a work-in-progress, so, don't hold back on the crit's. <G>
I've heard it's not a good idea to inherit from std container classes, but,
this seems to work. Good || bad? Was that advice for some other condition?
One objective of "Filer.h" is: KISS!

[1] Andrew Koenig, Walter Brown.
--
Bob R
POVrookie
Feb 14 '06 #6

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

Similar topics

40
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
4
by: yo_mismo | last post by:
Hi everyone, I'm trying to read the first line of a file this way: .... .... .... .... new_line=0; while((read=read(fd, &info, sizeof(info))) > 0 && !new_line){ if (strcmp(&info, "\n") !=...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
34
by: Ross Reyes | last post by:
HI - Sorry for maybe a too simple a question but I googled and also checked my reference O'Reilly Learning Python book and I did not find a satisfactory answer. When I use readlines, what...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
0
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read...
11
by: waffle.horn | last post by:
Hi, if this makes sense i want to create a function that can be called so that it reads a single line from a file, then after using the information destroys it. Such that when the function is...
6
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
9
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying...
6
by: Sean Davis | last post by:
I have a large file that I would like to transform and then feed to a function (psycopg2 copy_from) that expects a file-like object (needs read and readline methods). I have a class like so: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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:
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.