input and output from multiple text files

Discussion in 'C++' started by malla, Aug 1, 2006.

  1. malla

    malla Guest

    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.
    malla, Aug 1, 2006
    #1
    1. Advertising

  2. 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/
    Peter Jansson, Aug 1, 2006
    #2
    1. Advertising

  3. malla

    malla Guest

    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, Aug 1, 2006
    #3
  4. 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, Aug 1, 2006
    #4
  5. malla

    malla Guest

    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, Aug 1, 2006
    #5
  6. malla

    malla Guest

    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, Aug 2, 2006
    #6
  7. malla

    malla Guest

    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;
    }
    malla, Aug 2, 2006
    #7
  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(".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/
    Peter Jansson, Aug 2, 2006
    #8
  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
    Frederick Gotham, Aug 2, 2006
    #9
  10. malla

    Eric Pruneau Guest

    > 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
    Eric Pruneau, Aug 9, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guy
    Replies:
    5
    Views:
    2,821
    brucie
    Dec 13, 2003
  2. David

    Input/Output and Files

    David, Jul 22, 2003, in forum: C Programming
    Replies:
    0
    Views:
    382
    David
    Jul 22, 2003
  3. Replies:
    4
    Views:
    952
    M.E.Farmer
    Feb 13, 2005
  4. Aalok
    Replies:
    1
    Views:
    300
    Tim Hagan
    Sep 17, 2004
  5. Ron Brennan
    Replies:
    5
    Views:
    313
    Dr John Stockton
    May 14, 2004
Loading...

Share This Page