473,581 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

input and output from multiple text files

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
9 8839
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
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(".tx t");
file.open(fname );
if(!file.is_ope n()){
cout<<"retard\n ";
return 1;
}
else{
string str;
while(getline(f ile, 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
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(".tx t");
file.open(fname );
if(!file.is_ope n()){
cout<<"retard\n ";
return 1;
}
else{
string str;
while(getline(f ile, 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_ope n()){
cout<<"retard\n ";
return 1;
}
else{
string str;
while(getline(f ile, 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

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(".tx t");
file.open(fname );
if(!file.is_ope n()){
cout<<"retard\n ";
return 1;
}
else{
string str;
while(getline(f ile, 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_ope n()){
cout<<"retard\n ";
return 1;
}
else{
string str;
while(getline(f ile, 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

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(".tx t");
file.open(fname );
if(!file.is_ope n()){
cout<<"retard\n ";
return 1;
}
else{
string str;
while(getline(f ile, 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_ope n()){
cout<<"retard\n ";
return 1;
}
else{
string str;
while(getline(f ile, 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(b uff, str)) {
cout << str << endl;
}
buff.close();
}
else{
cout<<"retard\n ";
}
}

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

Aug 2 '06 #6

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(".tx t");
file.open(fname );
if(!file.is_ope n()){
cout<<"retard\n ";
return 1;
}
else{
string str;
while(getline(f ile, 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_ope n()){
cout<<"retard\n ";
return 1;
}
else{
string str;
while(getline(f ile, 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(b uff, str)) {
cout << str << endl;
}
buff.close();
}
else{
cout<<"retard\n ";
}
}

int main()
{
SensorAnalysis sense;
ifstream file("1.txt");
sense.rBuff(fil e);
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(b uff, str)) {
cout << str << endl;
}
buff.close();
}
else{
cout<<"retard\n ";
}
}

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

Aug 2 '06 #7
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(b uff, 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(fil e);
*** 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
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(".tx t");
> file.open(fname );
> if(!file.is_ope n()){
> cout<<"retard\n ";
> return 1;
> }
> else{
> string str;
> while(getline(f ile, str)) { // <---- look here!
> cout << str << endl;
> }
> file.close();
> }
> }
> return 0;
>}
>

Hi,

My compiler only get the error on the line
file.open(f name);
Tat was easily fixed to
file.open(f name.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_ope n()){
cout<<"retard\n ";
return 1;
}
else{
string str;
while(getline(f ile, 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);
};

SensorAnalysi s::SensorAnalys is(){

}

void SensorAnalysis: :rBuff(ifstream buff){

cout<< "here\n";

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

int main()
{
SensorAnalysis sense;
ifstream file("1.txt");
sense.rBuff(fil e);
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(b uff, str)) {
cout << str << endl;
}
buff.close();
}
else{
cout<<"retard\n ";
}
}

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

What errors do you get from your compiler?

My compiler gives two errors:
Line 27: struct std::basic_istr eam<char, std::char_trait s<char
has no member named "close"
Line 36: no matching function for call to std::basic_istr eam<char,
std::char_trait s<char::basic_i stream(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(fil e), close the file.

Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
Aug 2 '06 #9
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3097
by: qbschmidt | last post by:
i can input and output to a text file as long as the file is in the same directory as my c++ program. but i cant figure out how to input/output to a file in another directory i.e. I:\c++projects please help. thanks
1
6573
by: Charlie | last post by:
Hello, I have data in an Access table that I would like to export to multiple HTML tables. I would like to split the data in the Access table (about 92,000 records) into multiple HTML tables/files to reduce download time and bandwidth usage on my web server. That way, the user can select a particular page to download instead of...
2
1730
by: Chris Murphy via DotNetMonster.com | last post by:
Hey all, just wondering if anyone can point me in the right direction. I'm developing a solution that allows a user to store multiple text-based content (like code snippets, notes, documents etc.) in one master document. I'm not exactly sure which is the most efficient method of going about this. Should I: a. Use a container file like a CAB...
5
3423
by: Buddhist[CHinA] | last post by:
The text files are not only the .txt files, but also all ascii files. Thx.
2
2706
by: ManningFan | last post by:
I'm running through a database in a loop, and each time the loop finishes I would like to export the values of some variables to new text files. In reality it's more complex than this, but this is the simplest way to state it. Is there a way to write to text files and save them as different names (presumably one of the variables dot "TXT")...
3
5679
emaghero
by: emaghero | last post by:
Hello all, I want to open multiple txt files with similar names in C++ I have attempted this with the following code //Create as many txt files as there are valid propagation constants //Propagation constants are elements of the vector beta for(int j=0;j<(int)(number_of_nodes);j++){ if(*(beta+j)>substrate*k && *(beta+i)<core*k){...
6
30664
by: borthouth | last post by:
Hi, I have just started using Python and I am slowly getting into it. I wanted to make a little script to merge all files in a directory into one. All of these files will be text files. I know I need to use os.path.walk() to walk through all the files, and if not os.path.isdir(filename) with open(filename,'r').read() to read all the files...
2
4182
by: as001 | last post by:
Hi, I'm writing a windows application in C# using VS 2003. I got stuck where it has to write multiple output text files. Here's my piece of code: for loop { Random r = new Random(); //use millisecond and random to avoid similar filename
0
1508
by: zizi2 | last post by:
Hi, how do I output multiple excel files from one source using vbscript? Regards, Noluthando
5
14633
by: maral | last post by:
Hi every one, this is my first post here! I'm using GATE toolkit for information retrieval and text analysis, but i really need java for some parts. I have managed to find a specific word in one text file, but i need to look for a specific word, in multiple text files. all of the files are ".txt" and they are in the same folder but each of...
0
7788
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...
0
8299
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...
1
7890
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...
0
8166
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...
0
6546
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5667
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...
0
3813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2298
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
1
1398
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.