append output filename

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vj

    append output filename

    I am using Borland C++.

    I run a program which generates output data files, say 'Voltage.dat',
    'Current.dat'. I have a variable in my code (say N), and for various
    values of N (for each value of N), I generate these output data files.
    After running the program once for a given value of N (say N equal to
    1) and when the programme is finished, I manually append the generated
    filename with the value of N, such as 'Voltage_N1.dat ',
    'Current_N1.dat '. Then I go back to the code, make N=2, repeat the
    procedure and append the generated output files with '_N2' etc.

    Is there a simple way of appending the filenames automatically? In
    other words, I want to run the programme in a loop for various values
    of N (I know this bit i.e. how to make a loop:), and for each run, the
    files generated (such as 'Voltage_N ') is appended by the value of N
    i.e. 1, 2 and so on, followed by the extension '.dat'.
    I know after reading the help file that there are functions like
    strcpy and strcat, but couldn't figure out how to use them and their
    exact format. Also, I am not conversant with Classes, pointers in C++.
    (An easy to understand and basic reply with steps will be highly
    appreciated).

    Thankyou

  • tasneem.alsir@gmail.com

    #2
    Re: append output filename


    vj wrote:
    I am using Borland C++.
    >
    I run a program which generates output data files, say 'Voltage.dat',
    'Current.dat'. I have a variable in my code (say N), and for various
    values of N (for each value of N), I generate these output data files.
    After running the program once for a given value of N (say N equal to
    1) and when the programme is finished, I manually append the generated
    filename with the value of N, such as 'Voltage_N1.dat ',
    'Current_N1.dat '. Then I go back to the code, make N=2, repeat the
    procedure and append the generated output files with '_N2' etc.
    >
    Is there a simple way of appending the filenames automatically? In
    other words, I want to run the programme in a loop for various values
    of N (I know this bit i.e. how to make a loop:), and for each run, the
    files generated (such as 'Voltage_N ') is appended by the value of N
    i.e. 1, 2 and so on, followed by the extension '.dat'.
    I know after reading the help file that there are functions like
    strcpy and strcat, but couldn't figure out how to use them and their
    exact format. Also, I am not conversant with Classes, pointers in C++.
    (An easy to understand and basic reply with steps will be highly
    appreciated).
    >
    Thankyou

    Comment

    • tasneem.alsir@gmail.com

      #3
      Re: append output filename


      vj wrote:
      I am using Borland C++.
      >
      I run a program which generates output data files, say 'Voltage.dat',
      'Current.dat'. I have a variable in my code (say N), and for various
      values of N (for each value of N), I generate these output data files.
      After running the program once for a given value of N (say N equal to
      1) and when the programme is finished, I manually append the generated
      filename with the value of N, such as 'Voltage_N1.dat ',
      'Current_N1.dat '. Then I go back to the code, make N=2, repeat the
      procedure and append the generated output files with '_N2' etc.
      >
      Is there a simple way of appending the filenames automatically? In
      other words, I want to run the programme in a loop for various values
      of N (I know this bit i.e. how to make a loop:), and for each run, the
      files generated (such as 'Voltage_N ') is appended by the value of N
      i.e. 1, 2 and so on, followed by the extension '.dat'.
      I know after reading the help file that there are functions like
      strcpy and strcat, but couldn't figure out how to use them and their
      exact format. Also, I am not conversant with Classes, pointers in C++.
      (An easy to understand and basic reply with steps will be highly
      appreciated).
      >
      Thankyou

      Comment

      • SquareoftheHypotenuse

        #4
        Re: append output filename

        Hi,

        Try this....

        FILE fp1;
        char filename[30];

        for(int i=0;i<10;i++)
        {
        ......
        ......

        sprintf(filenam e,"Voltage_N%d" ,i);
        fp1 = fopen(filename, "w");

        //Do all the 'Writing to the File' here..

        fclose(fp1);
        }

        I guess this wud solve ur problem...



        On May 24, 10:05 am, vj <vijayjany...@g mail.comwrote:
        I am using Borland C++.
        >
        I run a program which generates output data files, say 'Voltage.dat',
        'Current.dat'. I have a variable in my code (say N), and for various
        values of N (for each value of N), I generate these output data files.
        After running the program once for a given value of N (say N equal to
        1) and when the programme is finished, I manually append the generated
        filename with the value of N, such as 'Voltage_N1.dat ',
        'Current_N1.dat '. Then I go back to the code, make N=2, repeat the
        procedure and append the generated output files with '_N2' etc.
        >
        Is there a simple way of appending the filenames automatically? In
        other words, I want to run the programme in a loop for various values
        of N (I know this bit i.e. how to make a loop:), and for each run, the
        files generated (such as 'Voltage_N ') is appended by the value of N
        i.e. 1, 2 and so on, followed by the extension '.dat'.
        I know after reading the help file that there are functions like
        strcpy and strcat, but couldn't figure out how to use them and their
        exact format. Also, I am not conversant with Classes, pointers in C++.
        (An easy to understand and basic reply with steps will be highly
        appreciated).
        >
        Thankyou

        Comment

        • Richard Heathfield

          #5
          Re: append output filename

          SquareoftheHypo tenuse said:
          Hi,
          >
          Try this....
          >
          FILE fp1;
          Not what you meant.
          char filename[30];
          >
          for(int i=0;i<10;i++)
          {
          .....
          .....
          >
          sprintf(filenam e,"Voltage_N%d" ,i);
          fp1 = fopen(filename, "w");
          >
          //Do all the 'Writing to the File' here..
          Not till you checked that the fopen succeeded.

          --
          Richard Heathfield
          "Usenet is a strange place" - dmr 29/7/1999

          email: rjh at the above domain, - www.

          Comment

          • vj

            #6
            Re: append output filename

            On May 24, 11:49 am, SquareoftheHypo tenuse
            <squareofthehyp oten...@gmail.c omwrote:
            Hi,
            >
            Try this....
            >
            FILE fp1;
            char filename[30];
            >
            for(int i=0;i<10;i++)
            {
            .....
            .....
            >
            sprintf(filenam e,"Voltage_N%d" ,i);
            fp1 = fopen(filename, "w");
            >
            //Do all the 'Writing to the File' here..
            >
            fclose(fp1);
            >
            }
            >
            I guess this wud solve ur problem...
            >
            On May 24, 10:05 am, vj <vijayjany...@g mail.comwrote:
            >
            >
            >
            I am using Borland C++.
            >
            I run a program which generates output data files, say 'Voltage.dat',
            'Current.dat'. I have a variable in my code (say N), and for various
            values of N (for each value of N), I generate these output data files.
            After running the program once for a given value of N (say N equal to
            1) and when the programme is finished, I manually append the generated
            filename with the value of N, such as 'Voltage_N1.dat ',
            'Current_N1.dat '. Then I go back to the code, make N=2, repeat the
            procedure and append the generated output files with '_N2' etc.
            >
            Is there a simple way of appending the filenames automatically? In
            other words, I want to run the programme in a loop for various values
            of N (I know this bit i.e. how to make a loop:), and for each run, the
            files generated (such as 'Voltage_N ') is appended by the value of N
            i.e. 1, 2 and so on, followed by the extension '.dat'.
            I know after reading the help file that there are functions like
            strcpy and strcat, but couldn't figure out how to use them and their
            exact format. Also, I am not conversant with Classes, pointers in C++.
            (An easy to understand and basic reply with steps will be highly
            appreciated).
            >
            Thankyou- Hide quoted text -
            >
            - Show quoted text -
            Hi SquareoftheHypo tenuse
            Thanks a lot to you (and all others who have shown interest) for a
            nice reply.
            It works amazingly, only thing is that initially I got an error, and
            as prompted by the error message, I rectified it by using "FILE *fp1;"
            instead of "FILE fp1;", and I am sure this is what you meant :)
            For the time being, I just tested this bit of code to see if the
            desired output files are generated (without actually writing any data
            into those files). And the output files are generated as desired !
            Could you please tell me what function (and its format) should I use
            to write data to files (i.e. fprintf?). Because I normally use a
            statement like "filename<<data 1<<data2<<endl; "
            Thankyou

            Comment

            • R. Scott Mellow

              #7
              Re: append output filename

              "vj" wrote
              SquareoftheHypo tenuse wrote:
              >Try this....
              >>
              >FILE fp1;
              >char filename[30];
              >>
              >for(int i=0;i<10;i++)
              >{
              >.....
              >.....
              >>
              >sprintf(filena me,"Voltage_N%d ",i);
              >fp1 = fopen(filename, "w");
              >>
              >//Do all the 'Writing to the File' here..
              >>
              >fclose(fp1);
              >>
              >}
              It works amazingly, only thing is that initially I got an error, and
              as prompted by the error message, I rectified it by using "FILE *fp1;"
              instead of "FILE fp1;", and I am sure this is what you meant :)
              For the time being, I just tested this bit of code to see if the
              desired output files are generated (without actually writing any data
              into those files). And the output files are generated as desired !
              Could you please tell me what function (and its format) should I use
              to write data to files (i.e. fprintf?). Because I normally use a
              statement like "filename<<data 1<<data2<<endl; "
              Here's my go:

              #include <ostream>
              #include <fstream>
              #include <sstream>
              #include <string>
              #include <cstdlib>

              using namespace std;

              int main(int ac, char** av)
              {
              int n = 1;
              int data1 = 14;
              double data2 = 3.0;

              if(ac 1)
              n = strtol(av[1],NULL,10);

              for(int i=0; i<n; ++i)
              {

              ostringstream oss;
              oss << "Filename_N " << i + 1;
              string fname(oss.str() );
              ofstream fout(fname.c_st r());

              if(fout)
              {
              fout << data1 << data2 << endl;
              }
              }

              return 0;
              }

              --
              Randy



              Comment

              • James Kanze

                #8
                Re: append output filename

                On May 24, 7:05 am, vj <vijayjany...@g mail.comwrote:
                I run a program which generates output data files, say 'Voltage.dat',
                'Current.dat'. I have a variable in my code (say N), and for various
                values of N (for each value of N), I generate these output data files.
                After running the program once for a given value of N (say N equal to
                1) and when the programme is finished, I manually append the generated
                filename with the value of N, such as 'Voltage_N1.dat ',
                'Current_N1.dat '. Then I go back to the code, make N=2, repeat the
                procedure and append the generated output files with '_N2' etc.
                Is there a simple way of appending the filenames automatically? In
                other words, I want to run the programme in a loop for various values
                of N (I know this bit i.e. how to make a loop:), and for each run, the
                files generated (such as 'Voltage_N ') is appended by the value of N
                i.e. 1, 2 and so on, followed by the extension '.dat'.
                I know after reading the help file that there are functions like
                strcpy and strcat, but couldn't figure out how to use them and their
                exact format. Also, I am not conversant with Classes, pointers in C++.
                (An easy to understand and basic reply with steps will be highly
                appreciated).
                std::ostringstr eam filename ;
                filename << "Voltage_" << N << ".dat" ;
                std::ofstream file( filename.str(). c_str() ) ;

                Avoid the functions strcpy and strcat; they are not very safe.
                If you are a beginner, it's much, much easier to use
                std::string (and std::vector, instead of T[]). The iostream
                functions are a bit more complicated, but still a great deal
                easier and safer to use than the FILE* mess inherited from C.

                --
                James Kanze (GABI Software) email:james.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                • Richard Heathfield

                  #9
                  Re: append output filename

                  James Kanze said:
                  On May 24, 7:05 am, vj <vijayjany...@g mail.comwrote:
                  <snip>
                  >I know after reading the help file that there are functions like
                  >strcpy and strcat, but couldn't figure out how to use them and their
                  >exact format. Also, I am not conversant with Classes, pointers in
                  >C++.
                  <snip>
                  Avoid the functions strcpy and strcat; they are not very safe.
                  In C, strcpy and strcat are both perfectly safe if used correctly. Do
                  the C++ versions of these functions have different semantics or
                  interfaces? </rhetorical>
                  If you are a beginner, it's much, much easier to use
                  std::string (and std::vector, instead of T[]). The iostream
                  functions are a bit more complicated, but still a great deal
                  easier and safer to use than the FILE* mess inherited from C.
                  I can't agree that iostreams are easier to use than FILE *. I believe
                  that you believe it, but it is certainly a matter of opinion.

                  --
                  Richard Heathfield
                  "Usenet is a strange place" - dmr 29/7/1999

                  email: rjh at the above domain, - www.

                  Comment

                  • James Kanze

                    #10
                    Re: append output filename

                    On May 25, 9:40 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                    James Kanze said:
                    On May 24, 7:05 am, vj <vijayjany...@g mail.comwrote:
                    <snip>
                    I know after reading the help file that there are functions like
                    strcpy and strcat, but couldn't figure out how to use them and their
                    exact format. Also, I am not conversant with Classes, pointers in
                    C++.
                    <snip>
                    Avoid the functions strcpy and strcat; they are not very safe.
                    In C, strcpy and strcat are both perfectly safe if used correctly.
                    Everything's perfectly safe "if used correctly". The question
                    is how difficult it is to use them correctly. Some functions,
                    like gets or sprintf, are impossible, or almost impossible to
                    use correctly. strcpy and strcat, while not as bad, require a
                    fair bit of care as well (and the C standard's committee has
                    proposed replacements, which are less dangerous). std::string
                    just works. Why use something that requires considerable care
                    and attention, when there is a simple solution which just works
                    available?
                    Do
                    the C++ versions of these functions have different semantics or
                    interfaces? </rhetorical>
                    C++ supports the C interfaces for reasons of C compatibility.
                    In C++, however, the type of a string is std::string, not
                    char[], and the functionality is supported with the = and the +=
                    operators. The result is something that is both easier to
                    write, and safer.
                    If you are a beginner, it's much, much easier to use
                    std::string (and std::vector, instead of T[]). The iostream
                    functions are a bit more complicated, but still a great deal
                    easier and safer to use than the FILE* mess inherited from C.
                    I can't agree that iostreams are easier to use than FILE *. I believe
                    that you believe it, but it is certainly a matter of opinion.
                    If the goal is a core dump, then printf is definitely easier
                    than anything in iostreams. But for anything useful, iostream's
                    beats FILE* hands down. How do you output a user defined type
                    using printf? How do you output to a socket? How do you read
                    skipping comments, or write putting a time stamp at the
                    beginning of each line?

                    In the end, FILE* isn't really even usable.

                    --
                    James Kanze (GABI Software) email:james.kan ze@gmail.com
                    Conseils en informatique orientée objet/
                    Beratung in objektorientier ter Datenverarbeitu ng
                    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                    Comment

                    • Richard Heathfield

                      #11
                      Re: append output filename

                      James Kanze said:
                      On May 25, 9:40 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                      >James Kanze said:
                      <snip>
                      Avoid the functions strcpy and strcat; they are not very safe.
                      >
                      >In C, strcpy and strcat are both perfectly safe if used correctly.
                      >
                      Everything's perfectly safe "if used correctly".
                      Well, gets() isn't (unless you count 'not used' as the only possible
                      example of 'used correctly').
                      The question is how difficult it is to use them correctly.
                      In the case of strcpy and strcat, it isn't terribly difficult.
                      Some functions,
                      like gets or sprintf, are impossible, or almost impossible to
                      use correctly.
                      sprintf is fairly easy to use correctly.
                      strcpy and strcat, while not as bad, require a
                      fair bit of care as well
                      Sure. Programming /does/ require care, and using API functions properly
                      is part of that care. Likewise, the STL requires a fair bit of care to
                      use correctly.
                      (and the C standard's committee has
                      proposed replacements, which are less dangerous).
                      I disagree. I will, however, accept that they have proposed replacements
                      which /they/ consider less dangerous.
                      std::string just works.
                      If used correctly, yes. If used incorrectly, however, it doesn't.
                      Why use something that requires considerable care
                      and attention, when there is a simple solution which just works
                      available?
                      For the same reason(s) that some people prefer manual transmission
                      gearboxes, even though it means they have to use the pedal (and indeed
                      use it correctly).
                      >Do
                      >the C++ versions of these functions have different semantics or
                      >interfaces? </rhetorical>
                      >
                      C++ supports the C interfaces for reasons of C compatibility.
                      Yes, because C compatibility is *important* to C++.
                      In C++, however, the type of a string is std::string, not
                      char[], and the functionality is supported with the = and the +=
                      operators. The result is something that is both easier to
                      write, and safer.
                      How can it be safer than "perfectly safe if used correctly"? And if it's
                      used incorrectly, clearly it is no safer than an incorrectly-used
                      strcpy.
                      If you are a beginner, it's much, much easier to use
                      std::string (and std::vector, instead of T[]). The iostream
                      functions are a bit more complicated, but still a great deal
                      easier and safer to use than the FILE* mess inherited from C.
                      >
                      >I can't agree that iostreams are easier to use than FILE *. I believe
                      >that you believe it, but it is certainly a matter of opinion.
                      >
                      If the goal is a core dump, then printf is definitely easier
                      than anything in iostreams.
                      For you, perhaps, but not for me. It is a matter of opinion.
                      But for anything useful, iostream's
                      beats FILE* hands down. How do you output a user defined type
                      using printf?
                      By writing a user-defined-type-specific function to do it, same as you
                      do in C++. Syntactic sugar doesn't mask this fact.
                      How do you output to a socket?
                      inetd
                      How do you read
                      skipping comments, or write putting a time stamp at the
                      beginning of each line?
                      Well, I use a line-capture routine of my own to read lines from a text
                      stream. If the stream I'm reading contains comments, the way in which I
                      skip them will depend on the comment format. For any comment format you
                      can already handle, I can come up with a dozen or more that you can't.
                      To add support for these, you'd have to cut code, whether you're using
                      iostreams or FILE *. As for putting a time stamp at the beginning of
                      each line, printf can handle that easily, when used in conjunction with
                      a little routine for formatting the time as a string in the format you
                      want. And, for every time format that you already support, I can easily
                      come up with a dozen that you don't. To add support for these, you have
                      to cut code, same as me.
                      >
                      In the end, FILE* isn't really even usable.
                      You're absolutely right, which is why it's so strange that I manage to
                      get excellent results with it. Perhaps I'm a genius. Do you think that
                      could be it? Or do you perhaps think it more likely that FILE * isn't
                      as hard to use as you make out?

                      --
                      Richard Heathfield
                      "Usenet is a strange place" - dmr 29/7/1999

                      email: rjh at the above domain, - www.

                      Comment

                      • Jim Langston

                        #12
                        Re: append output filename

                        "vj" <vijayjanyani@g mail.comwrote in message
                        news:1179983126 .637788.125470@ a26g2000pre.goo glegroups.com.. .
                        >I am using Borland C++.
                        >
                        I run a program which generates output data files, say 'Voltage.dat',
                        'Current.dat'. I have a variable in my code (say N), and for various
                        values of N (for each value of N), I generate these output data files.
                        After running the program once for a given value of N (say N equal to
                        1) and when the programme is finished, I manually append the generated
                        filename with the value of N, such as 'Voltage_N1.dat ',
                        'Current_N1.dat '. Then I go back to the code, make N=2, repeat the
                        procedure and append the generated output files with '_N2' etc.
                        >
                        Is there a simple way of appending the filenames automatically? In
                        other words, I want to run the programme in a loop for various values
                        of N (I know this bit i.e. how to make a loop:), and for each run, the
                        files generated (such as 'Voltage_N ') is appended by the value of N
                        i.e. 1, 2 and so on, followed by the extension '.dat'.
                        I know after reading the help file that there are functions like
                        strcpy and strcat, but couldn't figure out how to use them and their
                        exact format. Also, I am not conversant with Classes, pointers in C++.
                        (An easy to understand and basic reply with steps will be highly
                        appreciated).
                        You didn't specify for C or C++.

                        C (I'm a little rusty in C:
                        char FileName[100];
                        FileName[0] = '\0';
                        for ( int i = 0; i < 10; ++i )
                        {
                        strcat( FileName, "Voltage_N" );
                        strcat( FileName, itoa( i ) );
                        strcat( FileName, ".dat" );
                        // At this point FileName should contain "Voltage_N0.dat ",
                        "Voltage_N1.dat ", etc...

                        // Call function passing FileName or use here.
                        }

                        C++:
                        for ( int i = 0; i < 10; ++i )
                        {
                        std::stringstre am FileName;
                        FileName << "Voltage_N" << i << ".dat";
                        // at this point FileName.str() contains "Voltage_N0.dat ",
                        "Voltage_N1.dat ", etc...
                        }

                        There are many other ways to do it. itoa converts an interger to a c-style
                        string. I'm not positive it's available on all distrubutions (dont' know if
                        it's standard). If it doesnt' work for you, find a function/method/whatver
                        that does work for you. The C++ way is standard.


                        Comment

                        • Richard Heathfield

                          #13
                          Re: append output filename

                          Jim Langston said:
                          "vj" <vijayjanyani@g mail.comwrote in message
                          news:1179983126 .637788.125470@ a26g2000pre.goo glegroups.com.. .
                          >>I am using Borland C++.
                          <snip>
                          >exact format. Also, I am not conversant with Classes, pointers in
                          >C++.
                          <snip>
                          You didn't specify for C or C++.
                          He does kind of imply C++, though, both by his words and his choice of
                          crossposted group.
                          C (I'm a little rusty in C:
                          So I see. :-)

                          I won't try to correct yours, since you're probably not all that fussed
                          anyway, but for the OP's benefit, here's a simple, correct, C fragment:

                          char FileName[FILENAME_MAX] = {0};
                          int i = 0;

                          while(i < 10)
                          {
                          sprintf(FileNam e, "Voltage_N%d.da t", i++);
                          C++:
                          for ( int i = 0; i < 10; ++i )
                          {
                          std::stringstre am FileName;
                          FileName << "Voltage_N" << i << ".dat";
                          // at this point FileName.str() contains "Voltage_N0.dat ",
                          "Voltage_N1.dat ", etc...
                          }
                          >
                          There are many other ways to do it. itoa converts an interger to a
                          c-style string.
                          That isn't guaranteed.
                          I'm not positive it's available on all distrubutions
                          It isn't.
                          (dont' know if it's standard).
                          It isn't.
                          If it doesnt' work for you, find a
                          function/method/whatver
                          that does work for you.
                          See above.
                          The C++ way is standard.
                          So is the C way (above).

                          --
                          Richard Heathfield
                          "Usenet is a strange place" - dmr 29/7/1999

                          email: rjh at the above domain, - www.

                          Comment

                          Working...