input and output from multiple text files

M

malla

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.
 
P

Peter Jansson

malla said:
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/
 
M

malla

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;
}
 
P

Peter Jansson

malla said:
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/
 
M

malla

Peter said:
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
 
M

malla

malla said:
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;
}
 
M

malla

malla said:
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;
}
 
P

Peter Jansson

malla said:
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/
 
F

Frederick Gotham

Eric Pruneau posted:
*** 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
 
E

Eric Pruneau

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top