Problem with textfile

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

    Problem with textfile

    Hi NG

    This kode writes som text in a textfile:

    *************** ***************
    for (int j=0; j<length; j++)
    {
    ofstream session_fil("se ssion.txt");
    session_fil<<va lues[j]<<endl;
    }
    *************** ***************

    My problem is when i write 1,3,8 to the file only the 8 number will appear.

    Think it is because i dont jump to next line in the textfile when i write to
    it.

    How do i do that?

    Thanks
    Nize


  • Moonlit

    #2
    Re: Problem with textfile


    "Nize" <nize@x.xx> wrote in message
    news:3fd1d792$0 $95049$edfadb0f @dread11.news.t ele.dk...[color=blue]
    > Hi NG
    >
    > This kode writes som text in a textfile:
    >
    > *************** ***************
    > for (int j=0; j<length; j++)
    > {
    > ofstream session_fil("se ssion.txt");
    > session_fil<<va lues[j]<<endl;
    > }
    > *************** ***************[/color]

    You should create the stream outside the loop:

    ofstream session_fil("se ssion.txt");

    for (int j=0; j<length; j++)
    {
    session_fil<<va lues[j]<<endl;
    }

    Regards, Ron AF Greve
    [color=blue]
    >
    > My problem is when i write 1,3,8 to the file only the 8 number will[/color]
    appear.[color=blue]
    >
    > Think it is because i dont jump to next line in the textfile when i write[/color]
    to[color=blue]
    > it.
    >
    > How do i do that?
    >
    > Thanks
    > Nize
    >
    >[/color]


    Comment

    • Nize

      #3
      Re: Problem with textfile

      Thanks.

      If i start the program now 1,3,8 will be placed i my textfile. If i then run
      the program again i need to save the old data, so i will have the program to
      jump to the nextline.

      This i how i will have it to be:

      //First run of the program
      1
      3
      8
      //Second run of the program
      2
      4
      5

      Can you help again

      Thanks again
      [color=blue]
      > You should create the stream outside the loop:
      >
      > ofstream session_fil("se ssion.txt");
      >
      > for (int j=0; j<length; j++)
      > {
      > session_fil<<va lues[j]<<endl;
      > }
      >
      > Regards, Ron AF Greve
      >[/color]


      Comment

      • Jon Bell

        #4
        Re: Problem with textfile

        In article <3fd1dc29$0$950 07$edfadb0f@dre ad11.news.tele. dk>,
        Nize <nize@x.xx> wrote:[color=blue]
        >
        >If i start the program now 1,3,8 will be placed i my textfile. If i then run
        >the program again i need to save the old data, so i will have the program to
        >jump to the nextline.[color=green]
        >>
        >> ofstream session_fil("se ssion.txt");[/color][/color]

        ofstream session_fil ("session.tx t", ios_base::app);

        --
        Jon Bell <jtbellap8@pres by.edu> Presbyterian College
        Dept. of Physics and Computer Science Clinton, South Carolina USA

        Comment

        • Nize

          #5
          Re: Problem with textfile

          > >If i start the program now 1,3,8 will be placed i my textfile. If i then
          run[color=blue][color=green]
          > >the program again i need to save the old data, so i will have the program[/color][/color]
          to[color=blue][color=green]
          > >jump to the nextline.[color=darkred]
          > >>
          > >> ofstream session_fil("se ssion.txt");[/color][/color]
          >
          > ofstream session_fil ("session.tx t", ios_base::app);[/color]

          I then get the errors:

          error C2653: 'ios_base' : is not a class or namespace name
          error C2065: 'app' : undeclared identifier

          Can you help more :o)

          Thanks

          *************** ***********
          All my kode
          *************** ***********
          #include <iostream.h>
          #include <afxwin.h>
          #include <fstream.h>

          class Protokol
          {
          private:
          CString modtaget;
          public:
          Protokol(CStrin g modtag)
          {
          modtaget=modtag ;
          }

          void tilInt()
          {
          CString data=modtaget; //data = 138
          int length=data.Get Length(); // length=3

          int *values=new int[length];

          for (int i=0; i<length; i++)
          {
          char chardata=data.G etAt(i); // '1'
          int intdata= chardata-'0';//konvertering til int
          values[i]=intdata;
          cout<<values[i]<<endl;

          ofstream session_fil ("session.tx t", ios_base::app);
          session_fil<<va lues[i]<<endl;
          }

          ofstream session_fil("se ssion.txt");

          for (int j=0; j<length; j++)
          {
          session_fil<<va lues[j]<<endl;
          }
          }
          };


          Comment

          • Matthew Burgess

            #6
            Re: Problem with textfile

            On Sat, 6 Dec 2003 15:56:22 +0100
            "Nize" <nize@x.xx> wrote:
            [color=blue]
            > I then get the errors:
            >
            > error C2653: 'ios_base' : is not a class or namespace name
            > error C2065: 'app' : undeclared identifier[/color]
            [color=blue]
            > #include <iostream.h>[/color]
            #include <iostream>[color=blue]
            > #include <fstream.h>[/color]
            #include <fstream>

            The nonstandard headers you were including before are not namespace
            aware, use the standard versions instead.

            Regards,

            Matt.

            Comment

            • Nize

              #7
              Re: Problem with textfile

              > > #include <iostream.h>[color=blue]
              > #include <iostream>[color=green]
              > > #include <fstream.h>[/color]
              > #include <fstream>
              >
              > The nonstandard headers you were including before are not namespace
              > aware, use the standard versions instead.
              >
              > Regards,
              >
              > Matt.[/color]

              Ok, but then i get this error:

              error C2872: 'cout' : ambiguous symbol

              Thanks


              Comment

              • Matthew Burgess

                #8
                Re: Problem with textfile

                On Sat, 6 Dec 2003 16:29:03 +0100
                "Nize" <nize@x.xx> wrote:
                [color=blue][color=green][color=darkred]
                > > > #include <iostream.h>[/color]
                > > #include <iostream>[color=darkred]
                > > > #include <fstream.h>[/color]
                > > #include <fstream>[/color]
                >[color=green]
                > > The nonstandard headers you were including before are not namespace
                > > aware, use the standard versions instead.[/color]
                >[color=green]
                > > Regards,[/color]
                >[color=green]
                > > Matt.[/color]
                >
                > Ok, but then i get this error:
                >
                > error C2872: 'cout' : ambiguous symbol[/color]

                What C++ book are you using? You need to prefix the standard library
                provided libraries, functions and types with std:: (or use a "using
                namespace std;" declaration).

                Regards,

                Matt.

                Comment

                • Jon Bell

                  #9
                  Re: Problem with textfile

                  In article <20031206153311 .00001fc2.matth ew@linuxfromscr atch.org>,
                  Matthew Burgess <matthew@linuxf romscratch.org> wrote:[color=blue]
                  >On Sat, 6 Dec 2003 16:29:03 +0100
                  >"Nize" <nize@x.xx> wrote:[color=green]
                  >>
                  >> Ok, but then i get this error:
                  >>
                  >> error C2872: 'cout' : ambiguous symbol[/color]
                  >
                  >What C++ book are you using? You need to prefix the standard library
                  >provided libraries, functions and types with std:: (or use a "using
                  >namespace std;" declaration).[/color]

                  And note that most C++ books that are more than about five years old are
                  obsolete, because of issues like this. The current C++ standard was
                  finalized in 1997-98.

                  --
                  Jon Bell <jtbellap8@pres by.edu> Presbyterian College
                  Dept. of Physics and Computer Science Clinton, South Carolina USA

                  Comment

                  Working...