Hi Group!!!!!!!!!!! I'm new here!!!!!!!!!!!!!!!

Discussion in 'C++' started by Prerna, Feb 22, 2006.

  1. Prerna

    Prerna Guest

    Hi There people!!!!!!!!!!!!!!!!
    I have just joined this group.
    I was wondering if someone of u could help me out with my problem.
    I have written a c++ program,where I need to write different data to
    different files,
    say for example,
    from an input file consisting of alphabets,I need to put 18 letters in
    one file,12 in another ,10 in another ,so on uptill 8 letters.
    I have written the program,and I am successfully able to write the
    output in a single file,but I can't get around doing it for multiple
    files.

    Please help me out.

    Thanking You,
    Prerna.
     
    Prerna, Feb 22, 2006
    #1
    1. Advertisements

  2. Prerna

    ma740988 Guest

    Post the code or a small subset of the area you're having issues with.
     
    ma740988, Feb 22, 2006
    #2
    1. Advertisements

  3. Prerna

    Jim Langston Guest

    Well, just open a new file and output to it. Where is it you're having
    problems? Show us what you have and can't figure out and we'll see where we
    can help you.
     
    Jim Langston, Feb 22, 2006
    #3
  4. Prerna

    Prerna Guest

    Hi there!!!!!!!!!1111
    I am sending my program.
    Just please let me know how can i write different data to different
    files.


    # include<iostream.h>
    # include<string.h>
    # include<fstream.h>

    class sequence{ //class for sequence of proteins
    char ch;
    char filename[20];
    int seq_len;
    int cnt;
    char seq[1000];
    int no_of_seq;
    int cnt_18,cnt_16,cnt_14,cnt_12,cnt_10,cnt_8;
    int cnt_39,cnt_37,cnt_35,cnt_33,cnt_31,cnt_29;
    public:sequence()
    {
    cnt_18,cnt_16,cnt_14,cnt_12,cnt_10,cnt_8=0;
    cnt_39,cnt_37,cnt_35,cnt_33,cnt_31,cnt_29=0;
    seq_len=0;
    cnt=0;
    no_of_seq=
    }
    void accept_filename();//accepting the filename
    void get_line_from_file();
    void reduced_flanking_sequences();
    };
    void sequence::accept_filename()
    {

    cout<<"enter the filename"<<endl;
    cin>>filename;
    cout<<"\n";
    }

    void sequence::get_line_from_file()
    {
    ifstream fin;
    fin.open(filename,ios::in);
    ofstream fout;
    fout.open("output.txt",ios::eek:ut);
    while(!fin.eof())
    {
    fin.getline(seq,46);
    for(int i=0;i<46;i++)
    {

    if(i==18) //printing out flanking sequences with 18 characters
    {
    cnt_18=i;
    for(int k=0;k<cnt_18;k++)
    {
    cout<<seq[k];
    fout<<seq[k];

    }
    cout<<"\t";
    fout<<"\t";
    cout<<seq[22];
    fout<<seq[22];
    cout<<"\t";
    fout<<"\t";
    for(int l=25;l<=42;l++)
    {
    cout<<seq[l];
    fout<<seq[l];
    }
    cout<<"\n";
    fout<<"\n";
    }

    if(i==16) //printing out flanking sequences with 16 characters
    {
    cnt_16=i;
    for(int l=0;l<cnt_16;l++)
    {
    cout<<seq[l];
    fout<<seq[l];
    }
    cout<<"\t";
    fout<<"\t";
    cout<<seq[22];
    fout<<seq[22];
    cout<<"\t";
    fout<<"\t";
    for(int m=25;m<=40;m++)
    {
    cout<<seq[m];
    fout<<seq[m];
    }
    cout<<"\n";
    fout<<"\n";

    }

    if(i==14) //printing out flanking sequences with 14 characters
    {
    cnt_14=i;
    for(int l=0;l<cnt_14;l++)
    {
    cout<<seq[l];
    fout<<seq[l];
    }
    cout<<"\t";
    cout<<"\t";
    fout<<"\t";
    fout<<"\t";
    cout<<seq[22];
    fout<<seq[22];
    cout<<"\t";
    fout<<"\t";
    for(int m=25;m<=38;m++)
    {
    cout<<seq[m];
    fout<<seq[m];
    }
    cout<<"\n";
    fout<<"\n";

    }


    if(i==12) //printing out flanking sequences with 12 characters
    {
    cnt_12=i;
    for(int l=0;l<cnt_12;l++)
    {
    cout<<seq[l];
    fout<<seq[l];
    }
    cout<<"\t";
    cout<<"\t";
    fout<<"\t";
    fout<<"\t";
    cout<<seq[22];
    fout<<seq[22];
    cout<<"\t";
    fout<<"\t";
    for(int m=25;m<=36;m++)
    {
    cout<<seq[m];
    fout<<seq[m];
    }
    cout<<"\n";
    fout<<"\n";

    }



    if(i==10) //printing out flanking sequences with 10
    characters.This needs to be printed in a separate file.
    {
    cnt_10=i;
    for(int l=0;l<cnt_10;l++)
    {
    cout<<seq[l];
    fout<<seq[l];
    }
    cout<<"\t";
    cout<<"\t";
    fout<<"\t";
    fout<<"\t";
    cout<<seq[22];
    fout<<seq[22];
    cout<<"\t";
    fout<<"\t";
    for(int m=25;m<=34;m++)
    {
    cout<<seq[m];
    fout<<seq[m];
    }
    cout<<"\n";
    fout<<"\n";
    }


    if(i==8) //printing out flanking sequences with 8 characters.These
    nedd to be printed in separate files
    {
    cnt_8=i;
    for(int l=0;l<cnt_8;l++)
    {
    cout<<seq[l];
    fout<<seq[l];
    }
    cout<<"\t";
    cout<<"\t";
    fout<<"\t";
    fout<<"\t";
    cout<<seq[22];
    fout<<seq[22];
    cout<<"\t";
    fout<<"\t";
    for(int m=25;m<=32;m++)
    {
    cout<<seq[m];
    fout<<seq[m];
    }
    cout<<"\n"
    fout<<"\n";
    }

    }

    for(i=0;i<46;i++)
    {
    seq=0;

    }
    /*for(int i=0;i<49;i++)
    {
    cout<<seq;
    seq=0;
    seq='\n';
    }
    seq='\0';*/
    //cout<<"\n";
    }
    fin.close();
    fout.close();
    }


    int main()
    {
    sequence s1;
    s1.accept_filename();
    s1.get_line_from_file();
    return 0;
    }







    Thanx a lot,
    Prerna.
     
    Prerna, Feb 22, 2006
    #4
  5. Prerna

    Gavin Deane Guest

    Have a look at FAQ 5.8 (the rest of the FAQ is worth reading too)

    http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

    Post the code following the guidelines in that link (notably, *minimal*
    and *complete* code) and you'll give yourself the best chance of
    getting the help you need.

    Gavin Deane
     
    Gavin Deane, Feb 22, 2006
    #5
  6. Prerna

    Gavin Deane Guest

    Please don't top-post. Put your reply below (or in line with when
    applicable) the message you are replying to. That makes it easier to
    follow the thread.
    I haven't analysed your code in detail as it's not a very minimal
    example, but ...
    It's not directly related to your question, but C++ does not have
    headers called <iostream.h> or <fstream.h>
    http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.4
    You want <iostream> and <fstream> for the standard library. Note that
    both those put all the names you are using (cout, ofstream etc. inside
    the std namespace).

    There is a header <string.h> for the C library string-as-array-of-char
    functions. There is also <string> which is the C++ library string
    class, a completely different thing. I don't know which you want. I may
    be missing something scanning your code, but you don't appear to be
    using anything from either, so no need for the include. If you do need
    one, prefer <string> and use std::string from the C++ standard library.

    This seems like a very bad choice of name. The function's job appears
    to be to read and process the entire file line by line. The name
    suggests it reads just one line, and suggests nothing about any
    processing at all. Something like sequence::process_from_file might be
    better.
    Here you have created an object of type ofstream called fout. If you
    create another one called another_fout, initialised with a different
    filename, e.g. "another_output.txt", you can use fout for the things
    you want to write to output.txt and another_fout for the things you
    want to write toanother_output.txt. Also, there is no need to construct
    and open the objects as separate steps. And ios::in and ios::eek:ut are
    assumed for ifstream and ofstream so you don't need to specify them.

    ifstream fin(filename);
    ofstream fout("output.txt");
    That's not the way eof works.
    http://www.parashift.com/c++-faq-lite/input-output.html
    You need to check for eof *after* input fails.

    <snip lots of code that outputs to cout and fout>

    You seem quite happy with send output to two different places, cout and
    fout. Just create ofstream objects for all the files you want to output
    to and send the appropriate output to the appropriate ofstream.

    <snip>

    Gavin Deane
     
    Gavin Deane, Feb 22, 2006
    #6
  7. Prerna

    Gavin Deane Guest

     
    Gavin Deane, Feb 22, 2006
    #7
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.