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. 9 8832
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/
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/
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/
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
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;
}
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;
}
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
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/
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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.)...
|
by: Buddhist[CHinA] |
last post by:
The text files are not only the .txt files, but also all ascii files.
Thx.
|
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...
| |
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...
|
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...
|
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();...
|
by: zizi2 |
last post by:
Hi,
how do I output multiple excel files from one source using vbscript?
Regards,
Noluthando
|
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...
|
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...
| |
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,...
|
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: 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...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |