By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,491 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,491 IT Pros & Developers. It's quick & easy.

input and output from multiple text files

P: n/a
Hi,

I am trying to open a bunch of files and get data from them into one
single file. I am sure this process can be automated if I name the
files that I want to open in a regular pattern (say 1.txt, 2.txt,
3.txt, ...) Has anyone done sth like this before? Is there a way to
achieve this easily? Please share.

Aug 1 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
malla wrote:
Hi,

I am trying to open a bunch of files and get data from them into one
single file. I am sure this process can be automated if I name the
files that I want to open in a regular pattern (say 1.txt, 2.txt,
3.txt, ...) Has anyone done sth like this before? Is there a way to
achieve this easily? Please share.
Hi,

Please post the code you have so far so that we may comment on it.

You have one basic algorithm in your post;
1. Determine the name of the files you want to open.
2. Open the resulting file.
3. Read and write appropriately.
4. Close all.
Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
Aug 1 '06 #2

P: n/a
Here is the code that I have so far. Its pretty simple, except it is
giving me an int to string conversion problems. The files that i have
in the folder are 1.txt through 4.txt.

#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
stringstream ss;
string str;

string fname;
ifstream file;
for(int i = 1; i < 5; i++){
ss << i;
ss >str;
string fname = str.append(".txt");
file.open(fname);
if(!file.is_open()){
cout<<"retard\n";
return 1;
}
else{
string str;
while(getline(file, str)) { // <---- look here!
cout << str << endl;
}
file.close();
}
}
return 0;
}


Peter Jansson wrote:
malla wrote:
Hi,

I am trying to open a bunch of files and get data from them into one
single file. I am sure this process can be automated if I name the
files that I want to open in a regular pattern (say 1.txt, 2.txt,
3.txt, ...) Has anyone done sth like this before? Is there a way to
achieve this easily? Please share.

Hi,

Please post the code you have so far so that we may comment on it.

You have one basic algorithm in your post;
1. Determine the name of the files you want to open.
2. Open the resulting file.
3. Read and write appropriately.
4. Close all.
Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
Aug 1 '06 #3

P: n/a
malla wrote:
Here is the code that I have so far. Its pretty simple, except it is
giving me an int to string conversion problems. The files that i have
in the folder are 1.txt through 4.txt.

#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
stringstream ss;
string str;

string fname;
ifstream file;
for(int i = 1; i < 5; i++){
ss << i;
ss >str;
string fname = str.append(".txt");
file.open(fname);
if(!file.is_open()){
cout<<"retard\n";
return 1;
}
else{
string str;
while(getline(file, str)) { // <---- look here!
cout << str << endl;
}
file.close();
}
}
return 0;
}
Hi,

My compiler only get the error on the line
file.open(fname);
Tat was easily fixed to
file.open(fname.c_str());

I do not understand why we should "look here". You read all lines from
the file and writes them into cout. Perhaps you should write all that
stuff on an ofstream instead?

Further, you should declare all variables where you need them. Like the
following;

#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
for(int i = 1; i < 5; i++){
ostringstream fname;
fname<<i<<".txt";
ifstream file(fname.str().c_str());
if(!file.is_open()){
cout<<"retard\n";
return 1;
}
else{
string str;
while(getline(file, str)) {
cout << str << endl;
}
file.close();
}
}
return 0;
}
By the way, please don't top-post.
Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
Aug 1 '06 #4

P: n/a

Peter Jansson wrote:
malla wrote:
Here is the code that I have so far. Its pretty simple, except it is
giving me an int to string conversion problems. The files that i have
in the folder are 1.txt through 4.txt.

#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
stringstream ss;
string str;

string fname;
ifstream file;
for(int i = 1; i < 5; i++){
ss << i;
ss >str;
string fname = str.append(".txt");
file.open(fname);
if(!file.is_open()){
cout<<"retard\n";
return 1;
}
else{
string str;
while(getline(file, str)) { // <---- look here!
cout << str << endl;
}
file.close();
}
}
return 0;
}

Hi,

My compiler only get the error on the line
file.open(fname);
Tat was easily fixed to
file.open(fname.c_str());

I do not understand why we should "look here". You read all lines from
the file and writes them into cout. Perhaps you should write all that
stuff on an ofstream instead?

Further, you should declare all variables where you need them. Like the
following;

#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
for(int i = 1; i < 5; i++){
ostringstream fname;
fname<<i<<".txt";
ifstream file(fname.str().c_str());
if(!file.is_open()){
cout<<"retard\n";
return 1;
}
else{
string str;
while(getline(file, str)) {
cout << str << endl;
}
file.close();
}
}
return 0;
}
By the way, please don't top-post.
Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
Thanks Peter,

I will use your suggestions. ("look here" was a comment made by someone
else for a different problem. sorry about that)

G

Aug 1 '06 #5

P: n/a

malla wrote:
Peter Jansson wrote:
malla wrote:
Here is the code that I have so far. Its pretty simple, except it is
giving me an int to string conversion problems. The files that i have
in the folder are 1.txt through 4.txt.
>
#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;
>
int main()
{
stringstream ss;
string str;
>
string fname;
ifstream file;
for(int i = 1; i < 5; i++){
ss << i;
ss >str;
string fname = str.append(".txt");
file.open(fname);
if(!file.is_open()){
cout<<"retard\n";
return 1;
}
else{
string str;
while(getline(file, str)) { // <---- look here!
cout << str << endl;
}
file.close();
}
}
return 0;
}
>
Hi,

My compiler only get the error on the line
file.open(fname);
Tat was easily fixed to
file.open(fname.c_str());

I do not understand why we should "look here". You read all lines from
the file and writes them into cout. Perhaps you should write all that
stuff on an ofstream instead?

Further, you should declare all variables where you need them. Like the
following;

#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
for(int i = 1; i < 5; i++){
ostringstream fname;
fname<<i<<".txt";
ifstream file(fname.str().c_str());
if(!file.is_open()){
cout<<"retard\n";
return 1;
}
else{
string str;
while(getline(file, str)) {
cout << str << endl;
}
file.close();
}
}
return 0;
}
By the way, please don't top-post.
Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/

Thanks Peter,

I will use your suggestions. ("look here" was a comment made by someone
else for a different problem. sorry about that)

G

Hi all,

Can someone tell me why this code is not working? Can we not pass a
stream object as an argument to a function?

#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;

class SensorAnalysis{
private:
public:
SensorAnalysis::SensorAnalysis();
void SensorAnalysis::rBuff(ifstream buff);
};

SensorAnalysis::SensorAnalysis(){

}

void SensorAnalysis::rBuff(ifstream buff){

cout<< "here\n";

if(buff.is_open()){
string str;
while(getline(buff, str)) {
cout << str << endl;
}
buff.close();
}
else{
cout<<"retard\n";
}
}

int main()
{
SensorAnalysis sense;
ifstream file("1.txt");
sense.rBuff(file);
return 0;
}

Aug 2 '06 #6

P: n/a

malla wrote:
malla wrote:
Peter Jansson wrote:
malla wrote:
Here is the code that I have so far. Its pretty simple, except it is
giving me an int to string conversion problems. The files that i have
in the folder are 1.txt through 4.txt.

#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
stringstream ss;
string str;

string fname;
ifstream file;
for(int i = 1; i < 5; i++){
ss << i;
ss >str;
string fname = str.append(".txt");
file.open(fname);
if(!file.is_open()){
cout<<"retard\n";
return 1;
}
else{
string str;
while(getline(file, str)) { // <---- look here!
cout << str << endl;
}
file.close();
}
}
return 0;
}

>
Hi,
>
My compiler only get the error on the line
file.open(fname);
Tat was easily fixed to
file.open(fname.c_str());
>
I do not understand why we should "look here". You read all lines from
the file and writes them into cout. Perhaps you should write all that
stuff on an ofstream instead?
>
Further, you should declare all variables where you need them. Like the
following;
>
#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
for(int i = 1; i < 5; i++){
ostringstream fname;
fname<<i<<".txt";
ifstream file(fname.str().c_str());
if(!file.is_open()){
cout<<"retard\n";
return 1;
}
else{
string str;
while(getline(file, str)) {
cout << str << endl;
}
file.close();
}
}
return 0;
}
>
>
By the way, please don't top-post.
>
>
Sincerely,
>
Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
Thanks Peter,

I will use your suggestions. ("look here" was a comment made by someone
else for a different problem. sorry about that)

G


Hi all,

Can someone tell me why this code is not working? Can we not pass a
stream object as an argument to a function?

#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;

class SensorAnalysis{
private:
public:
SensorAnalysis::SensorAnalysis();
void SensorAnalysis::rBuff(ifstream buff);
};

SensorAnalysis::SensorAnalysis(){

}

void SensorAnalysis::rBuff(ifstream buff){

cout<< "here\n";

if(buff.is_open()){
string str;
while(getline(buff, str)) {
cout << str << endl;
}
buff.close();
}
else{
cout<<"retard\n";
}
}

int main()
{
SensorAnalysis sense;
ifstream file("1.txt");
sense.rBuff(file);
return 0;
}

I just figured out what the problem with the last post was. However, I
have run into another problem. If I want to use istream rather than
ifstream, this program dowsnot work. any suggestions anyone. I want to
read my data in any given stream and not just through a file or
something. I changed my previous code to the following, which is not
working:

#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;

class SensorAnalysis{
private:
public:
SensorAnalysis::SensorAnalysis();
void SensorAnalysis::rBuff(istream &buff);
};

SensorAnalysis::SensorAnalysis(){
}

void SensorAnalysis::rBuff(istream &buff){
cout<< "here\n";
if(buff){
string str;
while(getline(buff, str)) {
cout << str << endl;
}
buff.close();
}
else{
cout<<"retard\n";
}
}

int main()
{
SensorAnalysis sense;
istream file("1.txt");
sense.rBuff(file);
return 0;
}

Aug 2 '06 #7

P: n/a
I just figured out what the problem with the last post was. However, I
have run into another problem. If I want to use istream rather than
ifstream, this program dowsnot work. any suggestions anyone. I want to
read my data in any given stream and not just through a file or
something. I changed my previous code to the following, which is not
working:

#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;

class SensorAnalysis{
private:
*** this private is useless
public:
SensorAnalysis::SensorAnalysis();
void SensorAnalysis::rBuff(istream &buff);
*** why not just void rBuff(istream &buff);
};

SensorAnalysis::SensorAnalysis(){
}

void SensorAnalysis::rBuff(istream &buff){
cout<< "here\n";
if(buff){
string str;
while(getline(buff, str)) {
cout << str << endl;
}
buff.close();
*** close is not a member of istream get rid of that
}
else{
cout<<"retard\n";
}
}

int main()
{
SensorAnalysis sense;
istream file("1.txt");
*** ifstream here
sense.rBuff(file);
*** you can pass you ifstream here cause rBuff takes a reference to an
isteam which is the base class of ifstream.

*** close your file here
return 0;
}

eric
Aug 2 '06 #8

P: n/a
malla wrote:
malla wrote:
>>malla wrote:
>>>Peter Jansson wrote:

malla wrote:

>Here is the code that I have so far. Its pretty simple, except it is
>giving me an int to string conversion problems. The files that i have
>in the folder are 1.txt through 4.txt.
>
>#include <iostream>
>#include <sstream>
>#include <istream>
>#include <fstream>
>#include <string>
>using namespace std;
>
>int main()
>{
> stringstream ss;
> string str;
>
> string fname;
> ifstream file;
> for(int i = 1; i < 5; i++){
> ss << i;
> ss >str;
> string fname = str.append(".txt");
> file.open(fname);
> if(!file.is_open()){
> cout<<"retard\n";
> return 1;
> }
> else{
> string str;
> while(getline(file, str)) { // <---- look here!
> cout << str << endl;
> }
> file.close();
> }
> }
> return 0;
>}
>

Hi,

My compiler only get the error on the line
file.open(fname);
Tat was easily fixed to
file.open(fname.c_str());

I do not understand why we should "look here". You read all lines from
the file and writes them into cout. Perhaps you should write all that
stuff on an ofstream instead?

Further, you should declare all variables where you need them. Like the
following;

#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
for(int i = 1; i < 5; i++){
ostringstream fname;
fname<<i<<".txt";
ifstream file(fname.str().c_str());
if(!file.is_open()){
cout<<"retard\n";
return 1;
}
else{
string str;
while(getline(file, str)) {
cout << str << endl;
}
file.close();
}
}
return 0;
}
By the way, please don't top-post.
Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/

Thanks Peter,

I will use your suggestions. ("look here" was a comment made by someone
else for a different problem. sorry about that)

G


Hi all,

Can someone tell me why this code is not working? Can we not pass a
stream object as an argument to a function?

#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;

class SensorAnalysis{
private:
public:
SensorAnalysis::SensorAnalysis();
void SensorAnalysis::rBuff(ifstream buff);
};

SensorAnalysis::SensorAnalysis(){

}

void SensorAnalysis::rBuff(ifstream buff){

cout<< "here\n";

if(buff.is_open()){
string str;
while(getline(buff, str)) {
cout << str << endl;
}
buff.close();
}
else{
cout<<"retard\n";
}
}

int main()
{
SensorAnalysis sense;
ifstream file("1.txt");
sense.rBuff(file);
return 0;
}

I just figured out what the problem with the last post was. However, I
have run into another problem. If I want to use istream rather than
ifstream, this program dowsnot work. any suggestions anyone. I want to
read my data in any given stream and not just through a file or
something. I changed my previous code to the following, which is not
working:

#include <iostream>
#include <sstream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;

class SensorAnalysis{
private:
public:
SensorAnalysis::SensorAnalysis();
void SensorAnalysis::rBuff(istream &buff);
};

SensorAnalysis::SensorAnalysis(){
}

void SensorAnalysis::rBuff(istream &buff){
cout<< "here\n";
if(buff){
string str;
while(getline(buff, str)) {
cout << str << endl;
}
buff.close();
}
else{
cout<<"retard\n";
}
}

int main()
{
SensorAnalysis sense;
istream file("1.txt");
sense.rBuff(file);
return 0;
}
Hi,

What errors do you get from your compiler?

My compiler gives two errors:
Line 27: struct std::basic_istream<char, std::char_traits<char
has no member named "close"
Line 36: no matching function for call to std::basic_istream<char,
std::char_traits<char::basic_istream(const char [6])

The first one gives you a hint that you should not close the istream...
Rather, closing the file in main is better.

The second one gives you everything. You should be using
ifstream file("1.txt");

After sense.rBuff(file), close the file.

Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
Aug 2 '06 #9

P: n/a
Eric Pruneau posted:
>private:

*** this private is useless

Many people like to be explicit. Also, if the members are rearranged, it's
handy to have "private" before them already.

Another thing, it allows for testing if you want to write:

#define private public
#define protected public

--

Frederick Gotham
Aug 2 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.